delphi如何实现防止多次启动外部程序的功能? 我现在要写一个程序,按一个button,启动金山词霸.再按这个的时候系统要提示金山词霸已经运行. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是的,这个我明白,但是我不知道如何能获取要启动程序的handle EnumProcess或用快照枚举进程,判断要启动的进程是否在列表中。 使用Windows的API FindWindow FindWindow找到窗口PostMessage(句柄,WM_close,0,0); 在主窗体创建时候调用一下函数:uses TLHelp32;//根据应用名字结束指定模块路径的程序,结束所有//ModuleFullPath --程序路径,如: c:\test\test.exe//AppName --程序名, 如:test.exe,可省略不写 function KillProcessByFullPath(ModuleFullPath: string; AppName: string = ''): integer;var lppe: TPROCESSENTRY32; found: boolean; ProcessList: THandle; ModuleList: Thandle; pm: TMODULEENTRY32; h: Thandle; a: DWORD; ModuleName: string; AppProcessId: DWORD; // 线程IDbegin Result := 0; AppProcessId := GetCurrentProcessId; if Length(AppName) = 0 then AppName := ExtractFileName(ModuleFullPath); lppe.dwSize := sizeof(TPROCESSENTRY32); pm.dwSize := sizeof(TMODULEENTRY32); ProcessList := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); found := Process32First(ProcessList, lppe); while found do //进程列表 begin if StrIComp(PChar(StrPas(lppe.szExeFile)), PChar(AppName)) = 0 then //应用程序名字是否相同? begin ModuleList := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, lppe.th32ProcessID); found := module32first(ModuleList, pm); while found do //模块列表 begin ModuleName := StrPas(pm.szexepath); ModuleName := copy(ModuleName, max(1, pos(':', ModuleName) - 1), MaxInt); if StrIComp(pchar(ModuleName), pchar(ModuleFullPath)) = 0 then //是否包含要找的模块 begin if AppProcessId <> lppe.th32ProcessID then begin h := openprocess(PROCESS_ALL_ACCESS, TRUE, lppe.th32ProcessID); GetExitCodeProcess(h, a); if Integer(TerminateProcess(h, a)) <> 0 then inc(Result); end; break; end; found := module32next(ModuleList, pm); end; closehandle(ModuleList); end; found := Process32Next(ProcessList, lppe); end; closehandle(ProcessList);end; 使用Dos外部命令:TaskList 把它保存到一个文本文件中,然后读取该文本文件中是否有金山词霸这个进程,如果有,就不再运行。 散分。。。。 关于word模板的问题 如何输出一个数组各元素的所有排列 QRRichText 翻页问题 初学。请多都指教(50) 怎么在程序运行时,动态创建五十个label组件? 在fastport中要进行添加、保存,按钮要放到哪里 dbgrid的问题! TUpdateSQL的两个菜鸟问题 谁能介绍一下Delphi6中Web App Debugger工具的使用方法,由相关的文章吗? listview文字显示在图标下面,并且分行显示!!! 窗体共用
PostMessage(句柄,WM_close,0,0);
uses
TLHelp32;
//根据应用名字结束指定模块路径的程序,结束所有
//ModuleFullPath --程序路径,如: c:\test\test.exe
//AppName --程序名, 如:test.exe,可省略不写 function KillProcessByFullPath(ModuleFullPath: string; AppName: string = ''): integer;
var
lppe: TPROCESSENTRY32;
found: boolean;
ProcessList: THandle;
ModuleList: Thandle;
pm: TMODULEENTRY32;
h: Thandle;
a: DWORD;
ModuleName: string;
AppProcessId: DWORD; // 线程ID
begin
Result := 0;
AppProcessId := GetCurrentProcessId;
if Length(AppName) = 0 then
AppName := ExtractFileName(ModuleFullPath);
lppe.dwSize := sizeof(TPROCESSENTRY32);
pm.dwSize := sizeof(TMODULEENTRY32);
ProcessList := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
found := Process32First(ProcessList, lppe);
while found do //进程列表
begin
if StrIComp(PChar(StrPas(lppe.szExeFile)), PChar(AppName)) = 0 then //应用程序名字是否相同?
begin
ModuleList := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, lppe.th32ProcessID);
found := module32first(ModuleList, pm);
while found do //模块列表
begin
ModuleName := StrPas(pm.szexepath);
ModuleName := copy(ModuleName, max(1, pos(':', ModuleName) - 1), MaxInt);
if StrIComp(pchar(ModuleName), pchar(ModuleFullPath)) = 0 then //是否包含要找的模块
begin
if AppProcessId <> lppe.th32ProcessID then
begin
h := openprocess(PROCESS_ALL_ACCESS, TRUE, lppe.th32ProcessID);
GetExitCodeProcess(h, a);
if Integer(TerminateProcess(h, a)) <> 0 then
inc(Result);
end;
break;
end;
found := module32next(ModuleList, pm);
end;
closehandle(ModuleList);
end;
found := Process32Next(ProcessList, lppe);
end;
closehandle(ProcessList);
end;