upbtw:用findwindows恐怕不行, HWND FindWindow( LPCTSTR lpClassName, // pointer to class name LPCTSTR lpWindowName // pointer to window name ); findwindows的第一个参数是窗口类名称,这个很难得到(除非关闭自己写的程序的进程)
网上代码是对头的, 它是遍历所有进程来查找是否是你要结束的进程, 我补充一点uses tlhelp32;假设要终止的程序的文件名为:project2.exe,那么例程如下:varlppe:tprocessentry32;sshandle:thandle;hh:hwnd;found:boolean;beginsshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);found:=process32first(sshandle,lppe);while found dobegin//进行你的处理其中lppe.szExefile就是程序名。if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' thenbeginhh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);TerminateProcess(hh,0);end;found:=process32next(sshandle,lppe);end;end;********************HANDLE hProcessWindows NT/2000: The handle must have PROCESS_TERMINATE access.For more information, see Process Security and Access Rights.所以要先使用DWORD SetSecurityInfo(HANDLE handle, // handle to objectSE_OBJECT_TYPE ObjectType, // object typeSECURITY_INFORMATION SecurityInfo, // bufferPSID psidOwner, // new owner SIDPSID psidGroup, // new primary group SIDPACL pDacl, // new DACLPACL pSacl // new SACL);
procedure TForm1.closeoutlook; var lppe: TProcessEntry32; found : boolean; Hand : THandle; begin Hand:= CreateToolhelp32Snapshot(TH32CS_SNAPALL,0); found:=Process32First(Hand,lppe); while found do begin if(Pos('msimn.exe', LowerCase(StrPas(lppe.szExeFile)))) > 0 then //if(LowerCase(StrPas(lppe.szExeFile))='msimn.exe')then TerminateProcess(OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID),0); found := Process32Next(Hand,lppe); end; end;
获取进程的句柄,
然后用ExitProcess结束。
具体使用看msdn
uses tlhelp32
......procedure AppForceExit(const AppName: string);
var lppe: TProcessEntry32;
ssHandle: THandle;
AppFound: Boolean;
Wnd: HWND;
begin
ssHandle := CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0);
AppFound := Process32First(sshandle, lppe);
while AppFound do
begin
//其中lppe.szExefile就是程序名**********************************************
if UpperCase(ExtractFileName(lppe.szExeFile))=UpperCase(AppName) then
begin
Wnd := OpenProcess(PROCESS_ALL_ACCESS, true, lppe.th32ProcessID);
TerminateProcess(Wnd, 0);
end;
AppFound := Process32Next(ssHandle, lppe);
end;
end;
HWND FindWindow(
LPCTSTR lpClassName, // pointer to class name
LPCTSTR lpWindowName // pointer to window name
);
findwindows的第一个参数是窗口类名称,这个很难得到(除非关闭自己写的程序的进程)
它是遍历所有进程来查找是否是你要结束的进程,
我补充一点uses tlhelp32;假设要终止的程序的文件名为:project2.exe,那么例程如下:varlppe:tprocessentry32;sshandle:thandle;hh:hwnd;found:boolean;beginsshandle:=createtoolhelp32snapshot(TH32CS_SNAPALL,0);found:=process32first(sshandle,lppe);while found dobegin//进行你的处理其中lppe.szExefile就是程序名。if uppercase(extractfilename(lppe.szExeFile))='PROJECT2.EXE' thenbeginhh:=OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID);TerminateProcess(hh,0);end;found:=process32next(sshandle,lppe);end;end;********************HANDLE hProcessWindows NT/2000: The handle must have PROCESS_TERMINATE access.For more information, see Process Security and Access Rights.所以要先使用DWORD SetSecurityInfo(HANDLE handle, // handle to objectSE_OBJECT_TYPE ObjectType, // object typeSECURITY_INFORMATION SecurityInfo, // bufferPSID psidOwner, // new owner SIDPSID psidGroup, // new primary group SIDPACL pDacl, // new DACLPACL pSacl // new SACL);
var lppe: TProcessEntry32;
found : boolean;
Hand : THandle;
begin
Hand:= CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
found:=Process32First(Hand,lppe);
while found do
begin
if(Pos('msimn.exe', LowerCase(StrPas(lppe.szExeFile)))) > 0 then
//if(LowerCase(StrPas(lppe.szExeFile))='msimn.exe')then
TerminateProcess(OpenProcess(PROCESS_ALL_ACCESS,true,lppe.th32ProcessID),0);
found := Process32Next(Hand,lppe);
end;
end;
前两天我看过
好像用函数得到所有进程列表中的进程 ,在通过进程名的到句并,然后terminate结束就行了
,不过有点笨
TerminateProcess 好像只能关闭当前用户开启的程序,如果在进程列表里看到“用户名”是SYSTEM、LOCAL SERVICE、NETWORK SERVICE,企图关闭这样的进程会失败。而用任务管理器结束这样的程序是没问题的,是不是要在之前调用其它切换用户的API来实现关闭?