RT,最近写程序时候
已经获得QQ.exe的句柄,想利用TerminateProcess()关闭QQ.exe,却不能成功.
不知道是否跟操作系统有关,本人系统是:XP.
哪位仁兄指教一下,可能的问题,另请教一下还有其他关闭进程的函数没??谢谢赐教!
procedure TForm1.Button1Click(Sender: TObject);
var
hprocess:thandle;
hmodule1:hmodule;
cbneed,mbneed:dword;
procnum:integer;
lp:array[0..255] of dword;
lpm:array[0..255] of dword;
lpfilename:string;
i,j:integer;
s:String;
position:integer;
begin
setlength(lpfilename,512); //设置进程全路径长度
//枚举进程列表,存放在LP中
if enumprocesses(@lp,sizeof(lp),cbneed) then
begin
procnum:=strtoint(floattostr(cbneed/4)); //进程数量
for i:=0 to procnum-1 do begin
//打开进程
hprocess:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,FALSE,lp[i]);
if hprocess<>0 then begin
//枚举该进程中所有MODULE,其中第一个MODULE为主模块
if enumprocessmodules(hprocess,@lpm,sizeof(lpm),mbneed) then begin
//读出进程的文件名全路径
if GetModuleBaseName(hprocess,lpm[0],pchar(lpfilename),512)<>0 then begin
s:=lpfilename;
if GetNSubStringPos(1,'QQ.exe',S)=1 then
begin
terminateprocess(hprocess,0);
end;
end;
end;
end;
end;
end;
end;
已经获得QQ.exe的句柄,想利用TerminateProcess()关闭QQ.exe,却不能成功.
不知道是否跟操作系统有关,本人系统是:XP.
哪位仁兄指教一下,可能的问题,另请教一下还有其他关闭进程的函数没??谢谢赐教!
procedure TForm1.Button1Click(Sender: TObject);
var
hprocess:thandle;
hmodule1:hmodule;
cbneed,mbneed:dword;
procnum:integer;
lp:array[0..255] of dword;
lpm:array[0..255] of dword;
lpfilename:string;
i,j:integer;
s:String;
position:integer;
begin
setlength(lpfilename,512); //设置进程全路径长度
//枚举进程列表,存放在LP中
if enumprocesses(@lp,sizeof(lp),cbneed) then
begin
procnum:=strtoint(floattostr(cbneed/4)); //进程数量
for i:=0 to procnum-1 do begin
//打开进程
hprocess:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,FALSE,lp[i]);
if hprocess<>0 then begin
//枚举该进程中所有MODULE,其中第一个MODULE为主模块
if enumprocessmodules(hprocess,@lpm,sizeof(lpm),mbneed) then begin
//读出进程的文件名全路径
if GetModuleBaseName(hprocess,lpm[0],pchar(lpfilename),512)<>0 then begin
s:=lpfilename;
if GetNSubStringPos(1,'QQ.exe',S)=1 then
begin
terminateprocess(hprocess,0);
end;
end;
end;
end;
end;
end;
end;
hprocess:=OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION or PROCESS_VM_READ,FALSE,lp[i]);