to zw84611:<br> 下面是代码,如果MyWinExec函数启动进程不成功而又没有判断CreateProcess<br>的返回状态时,就会出现EnumWindowsProc的不断重覆循环,如何解决?<br><br>var<br> Form1: TForm1;<br> abc:hwnd;<br> TagHwnd:HWND;<br><br>function EnumWindowsProc(AHWnd: HWnd;<br> LPARAM: lParam): boolean; stdcall;<br>var<br> WndCaption: array[0..254] of char;<br> WndClassName: array[0..254] of char;<br> dwProcessId
WORD;<br>begin<br> GetWindowText(AHWnd, @WndCaption, 254); // 取窗口标题文字<br> GetClassName(AHWnd, @WndClassName, 254); // 取窗口类名<br> GetWindowThreadProcessId(AHWnd,@dwProcessId); // 取进程ID<br> if (dwProcessId = abc) then // if 进程id= 刚才创健的进程的id<br> begin<br> TagHwnd := ahWnd; // 如果找到想找的窗口,就保存该进程的窗口句柄<br> result := FALSE;<br> end<br>else<br> result := TRUE;<br><br> with Form1.Memo1.Lines do<br> begin<br> Add(StrPas(WndCaption)); <br> Add(StrPas(WndClassName)); <br> add(inttostr(dwprocessid)); <br> add(inttostr(ahwnd)); <br> Add('-------');<br> end;<br>end;<br><br>//***** 创健一外程并取进程号放全量ABC中;<br>function Tform1.MyWinExec(FileName: Pchar; Visibility: integer): integer;<br>var<br> StartupInfo: TStartupInfo;<br> ProcessInfo: TProcessInformation;<br> str: string;<br> hwnd: Thandle;<br>begin<br> FillChar(StartupInfo, Sizeof(StartupInfo), #0);<br> StartupInfo.cb := Sizeof(StartupInfo);<br> StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br> StartupInfo.wShowWindow := Visibility;<br> if not CreateProcess(nil,<br> FileName, { pointer to command line string }<br> nil, { pointer to process security attributes }<br> nil, { pointer to thread security attributes }<br> false, { handle inheritance flag }<br> 0,<br> nil, { pointer to new environment block }<br> nil, { pointer to current directory name }<br> StartupInfo, { pointer to STARTUPINFO }<br> ProcessInfo) then<br> Result := -1 { pointer to PROCESS_INF }<br> else<br> abc:= ProcessInfo.dwProcessId;<br><br>end;<br><br>//================================================================================<br><br>procedure TForm1.Button1Click(Sender: TObject); <br> var<br> i:hwnd;<br> begin<br> MyWinExec('cmd.exe', 1);<br> EnumWindows(@EnumWindowsProc, 0);<br>end;<br>