如何让EnumWindows 在找不到句柄时退出(100分)

  • 主题发起人 主题发起人 abcxca
  • 开始时间 开始时间
A

abcxca

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; 用CreateProcess创建了一个进程,并保留了进程ID号.<br>&nbsp; &nbsp;利用这ID号用EnumWindows函数配合EnumWindowsProc函找用枚举法找该进程的<br>窗口句柄时,发现如果没有找到时, 会出现死循环,如何令EnumWindows在找不到<br>句柄时退出.
 
&nbsp;高手在哪?
 
给出你的代码
 
to zw84611:<br>&nbsp; &nbsp;下面是代码,如果MyWinExec函数启动进程不成功而又没有判断CreateProcess<br>的返回状态时,就会出现EnumWindowsProc的不断重覆循环,如何解决?<br><br>var<br>&nbsp; &nbsp; &nbsp; Form1: TForm1;<br>&nbsp; &nbsp; &nbsp; &nbsp; abc:hwnd;<br>&nbsp; &nbsp; TagHwnd:HWND;<br><br>function EnumWindowsProc(AHWnd: HWnd;<br>&nbsp; LPARAM: lParam): boolean; stdcall;<br>var<br>&nbsp; WndCaption: array[0..254] of char;<br>&nbsp; WndClassName: array[0..254] of char;<br>&nbsp; dwProcessId:DWORD;<br>begin<br>&nbsp; GetWindowText(AHWnd, @WndCaption, 254); &nbsp;// 取窗口标题文字<br>&nbsp; GetClassName(AHWnd, @WndClassName, 254); &nbsp; // 取窗口类名<br>&nbsp; GetWindowThreadProcessId(AHWnd,@dwProcessId); &nbsp;// 取进程ID<br>&nbsp; &nbsp;if (dwProcessId = abc) then &nbsp; &nbsp; &nbsp;// if 进程id= 刚才创健的进程的id<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;TagHwnd := ahWnd; &nbsp;// 如果找到想找的窗口,就保存该进程的窗口句柄<br>&nbsp; &nbsp; &nbsp;result := FALSE;<br>&nbsp; &nbsp;end<br>else<br>&nbsp; &nbsp;result := &nbsp;TRUE;<br><br>&nbsp; with Form1.Memo1.Lines do<br>&nbsp; begin<br>&nbsp; &nbsp; Add(StrPas(WndCaption)); &nbsp; &nbsp;<br>&nbsp; &nbsp; Add(StrPas(WndClassName)); &nbsp; <br>&nbsp; &nbsp; add(inttostr(dwprocessid)); &nbsp;<br>&nbsp; &nbsp; add(inttostr(ahwnd)); &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; Add('-------');<br>&nbsp; end;<br>end;<br><br>//***** 创健一外程并取进程号放全量ABC中;<br>function Tform1.MyWinExec(FileName: Pchar; Visibility: integer): integer;<br>var<br>&nbsp; &nbsp;StartupInfo: TStartupInfo;<br>&nbsp; &nbsp;ProcessInfo: TProcessInformation;<br>&nbsp; &nbsp;str: string;<br>&nbsp; &nbsp;hwnd: Thandle;<br>begin<br>&nbsp; &nbsp;FillChar(StartupInfo, Sizeof(StartupInfo), #0);<br>&nbsp; &nbsp;StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; &nbsp;StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp;StartupInfo.wShowWindow := Visibility;<br>&nbsp; &nbsp;if not CreateProcess(nil,<br>&nbsp; &nbsp; &nbsp; FileName, { pointer to command line string }<br>&nbsp; &nbsp; &nbsp; nil, { pointer to process security attributes }<br>&nbsp; &nbsp; &nbsp; nil, { pointer to thread security attributes }<br>&nbsp; &nbsp; &nbsp; false, { handle inheritance flag }<br>&nbsp; &nbsp; &nbsp; 0,<br>&nbsp; &nbsp; &nbsp; nil, { pointer to new environment block }<br>&nbsp; &nbsp; &nbsp; nil, { pointer to current directory name }<br>&nbsp; &nbsp; &nbsp; StartupInfo, { pointer to STARTUPINFO }<br>&nbsp; &nbsp; &nbsp; ProcessInfo) then<br>&nbsp; &nbsp; &nbsp; Result := -1 { pointer to PROCESS_INF }<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; abc:= ProcessInfo.dwProcessId;<br><br>end;<br><br>//================================================================================<br><br>procedure TForm1.Button1Click(Sender: TObject); <br>&nbsp; var<br>&nbsp; i:hwnd;<br>&nbsp;begin<br>&nbsp; &nbsp;MyWinExec('cmd.exe', 1);<br>&nbsp; &nbsp;EnumWindows(@EnumWindowsProc, 0);<br>end;<br>
 
是不是可以这样:<br>&nbsp; if FindWindow(...) &lt;&gt; 0 then EnumWindows(@EnumWindowsProc, 0);
 
需要用到的三个API函数<br>DragAcceptFiles():初始化窗口使可以接受拖放<br>DragQueryFile()查询拖放的文件名<br>DragFinish()释放拖放文件使用的资源<br>《delphi高手突破中》有讲实现方法。
 

Similar threads

S
回复
0
查看
832
SUNSTONE的Delphi笔记
S
S
回复
0
查看
791
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部