如何处理[notepad.exe进程事件],存在提示[已运行],不存在[运行]一次
如何处理[notepad.exe进程事件],存在提示[已运行],不存在[运行]一次
声明一下,我在一个 窗体中加了一个 timer1时钟控件和BitBtn1按扭控件
timer1时钟控
procedure TFrmMain.Timer1Timer(Sender: TObject);
var
aps:array[1..1024] of DWORD;
hProcess, cb:cardinal;
procCnt, hModule:DWORD;
i:integer;
ret:LongBool;
moduleFileName:array[1..100] of ansichar;
begin
EnumProcesses( @aps, sizeof( aps ), cb );
procCnt := cb div sizeof( DWORD );
for i:=1 to procCnt do
begin
hProcess := OpenProcess( PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, aps[i] );
if hProcess = 0 then continue;
ret := EnumProcessModules( hProcess, @hModule, sizeof(hModule), cb );
if not ret then continue;
GetModuleBaseName( hProcess, hModule, @moduleFileName, sizeof( moduleFileName ) );
//ShowMessage(moduleFileName);
if ( ( lstrcmpi( @moduleFileName, 'notepad.exe' ) = 0 ) or
( lstrcmpi( @moduleFileName, 'NOTEPAD.EXE' ) = 0 ) ) then
begin
Timer1.Enabled:=false; //关闭
MessageBox( 0, '已经运行', 'notepad.exe', MB_OK );
Break; //退出for循环
end;
{//此处需要 修改
Timer1.Enabled:=false; //关闭
//运行 notepad.exe
//引用shellapi.pas单元:uses ShellAPI;
ShellExecute(handle,'open', 'notepad.exe', nil, nil, SW_SHOWNORMAL);
Break; //退出for循环
end; }
end;
Timer1.Enabled:=false; //关闭
end;
还请各位多多赐教
[解决办法]
function CheckProcess(fileName: string): Boolean;
var
isOK:Boolean;
ProcessHandle:Thandle;
ProcessStruct:TProcessEntry32;
Function KillProcessID(ProcessID:Cardinal): boolean;
var
H:THandle;
begin
H:=OpenProcess(Process_All_Access, true,ProcessID);
Result:=TerminateProcess(H,0);
end;
begin
Result := False;
try
ProcessHandle:=createtoolhelp32snapshot(Th32cs_snapprocess,0);
processStruct.dwSize:=sizeof(ProcessStruct);
isOK:=process32first(ProcessHandle,ProcessStruct);
while isOK do
begin
if fileName = ProcessStruct.szExeFile then
begin
Result := True;
Exit;
end;
isOK := process32next(ProcessHandle, ProcessStruct);
end;
finally
CloseHandle(ProcessHandle);
end;
end;