如何关闭正在运行的程序 如何关闭一个正在运行的程序呢?还有如何判断? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先通过findwindow找到窗体句柄,然后发送WM_Close消息var h:HWND;begin h := FindWindow(nil,'这里写窗体标题'); if h <> 0 then SendMessage(h,WM_CLOSE,0,0);end; 循环所有进程,取得相同名的进程,得到进程句柄,关闭killtask('cmd.exe');//结束进程function TForm1.KillTask(ExeFileName: string): Integer;const PROCESS_TERMINATE = $0001;var ContinueLoop: BOOL; FSnapshotHandle: THandle; FProcessEntry32: TProcessEntry32;begin Result := 0; FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); FProcessEntry32.dwSize := SizeOf(FProcessEntry32); ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); while Integer(ContinueLoop) <> 0 do begin if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then Result := Integer(TerminateProcess( OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0)); ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); end; CloseHandle(FSnapshotHandle);end;//提权function TForm1.EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;var TP: TOKEN_PRIVILEGES; Dummy: Cardinal;begin TP.PrivilegeCount := 1; LookupPrivilegevalue(nil, pchar(PrivName), TP.Privileges[0].Luid); if bEnable then TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED else TP.Privileges[0].Attributes := 0; AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy); Result := GetLastError = ERROR_SUCCESS;end;转自 http://blog.csdn.net/mailreboot/archive/2007/12/18/1946866.aspx unit U_KillPrg;interfaceuses Windows,SysUtils,Tlhelp32;function KillTask(ExeFileName: string): Integer; //关闭进程function EnableDebugPrivilege: Boolean; //提升权限 function FindProcessId(ExeFileName: string):THandle; //查找进程(找到了不就是正在运行了?)implementationfunction FindProcessId(ExeFileName: string):THandle;var ContinueLoop:BOOL; FSnapshotHandle:THandle; FProcessEntry32:TProcessEntry32;begin result:=0; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); FProcessEntry32.dwSize:=Sizeof(FProcessEntry32); ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); while integer(ContinueLoop)<>0 do begin if UpperCase(FProcessEntry32.szExeFile)=UpperCase(ExeFileName) then begin result:=FProcessEntry32.th32ProcessID; break; end; ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32); end; CloseHandle (FSnapshotHandle);end;function KillTask(ExeFileName: string): Integer;const PROCESS_TERMINATE = $0001;var ContinueLoop: boolean; FSnapshotHandle: THandle; FProcessEntry32: TProcessEntry32;begin Result := 0; FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); FProcessEntry32.dwSize := SizeOf(FProcessEntry32); ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); while Integer(ContinueLoop) <> 0 do begin if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) = UpperCase(ExeFileName))) then Result := Integer(TerminateProcess( OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0)); ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32); end; CloseHandle(FSnapshotHandle);end;//对于服务程序会提示“拒绝访问”,其实只要程序拥有DEBUG权限即可function EnableDebugPrivilege: Boolean; function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean; var TP: TOKEN_PRIVILEGES; Dummy: Cardinal; begin TP.PrivilegeCount := 1; LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid); if bEnable then TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED else TP.Privileges[0].Attributes := 0; AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy); Result := GetLastError = ERROR_SUCCESS; end;var hToken: Cardinal;begin OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken); result:=EnablePrivilege(hToken, 'SeDebugPrivilege', True); CloseHandle(hToken);end;end. 如何用ADO+INI文件动态连接数据库(MS SQL) 求下列数的CRC16_standard和CRC16_ccitt值。 fastreport和分页相关的问题 微软面试100题之一。。。。。。。。。。。。。 争救,读取32位带alpha通道的tga文件 如何知道用户切换到我的应用程序。 求《Delphi 7项目开发实践》每个项目实例的简要介绍 如何传参?? MTS/COM+组件做好后如何发布呀? delphi有没有字符或整数转二进制数的函数? 这样的情况用正则怎么正确匹配并替换? 请问在webbrowser中如何使用bmp资源文件?(分不够可以加)
h:HWND;
begin
h := FindWindow(nil,'这里写窗体标题');
if h <> 0 then
SendMessage(h,WM_CLOSE,0,0);
end;
killtask('cmd.exe');//结束进程
function TForm1.KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
//提权
function TForm1.EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable:
Boolean): Boolean;
var
TP: TOKEN_PRIVILEGES;
Dummy: Cardinal;
begin
TP.PrivilegeCount := 1;
LookupPrivilegevalue(nil, pchar(PrivName), TP.Privileges[0].Luid);
if bEnable then
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
TP.Privileges[0].Attributes := 0;
AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);
Result := GetLastError = ERROR_SUCCESS;
end;转自 http://blog.csdn.net/mailreboot/archive/2007/12/18/1946866.aspx
Windows,SysUtils,Tlhelp32;function KillTask(ExeFileName: string): Integer; //关闭进程
function EnableDebugPrivilege: Boolean; //提升权限
function FindProcessId(ExeFileName: string):THandle; //查找进程(找到了不就是正在运行了?)implementationfunction FindProcessId(ExeFileName: string):THandle;
var
ContinueLoop:BOOL;
FSnapshotHandle:THandle;
FProcessEntry32:TProcessEntry32;
begin
result:=0;
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while integer(ContinueLoop)<>0 do
begin
if UpperCase(FProcessEntry32.szExeFile)=UpperCase(ExeFileName) then
begin
result:=FProcessEntry32.th32ProcessID;
break;
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle (FSnapshotHandle);
end;function KillTask(ExeFileName: string): Integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: boolean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32); while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
Result := Integer(TerminateProcess(
OpenProcess(PROCESS_TERMINATE,
BOOL(0),
FProcessEntry32.th32ProcessID),
0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;//对于服务程序会提示“拒绝访问”,其实只要程序拥有DEBUG权限即可
function EnableDebugPrivilege: Boolean;
function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;
var
TP: TOKEN_PRIVILEGES;
Dummy: Cardinal;
begin
TP.PrivilegeCount := 1;
LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);
if bEnable then
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else TP.Privileges[0].Attributes := 0;
AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);
Result := GetLastError = ERROR_SUCCESS;
end;
var
hToken: Cardinal;
begin
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);
result:=EnablePrivilege(hToken, 'SeDebugPrivilege', True);
CloseHandle(hToken);
end;end.