300分求解,如何执行一个外部程序,并得到Handle,以便SetForegroundWindow.(300分)

  • 主题发起人 主题发起人 newnob
  • 开始时间 开始时间
N

newnob

Unregistered / Unconfirmed
GUEST, unregistred user!
我找到一个函数<br>function MyWinExec(const CmdLine: string; CmdShow: DWord): THandle;<br>var<br>&nbsp; SI :TStartupInfo;<br>&nbsp; PI :TProcessInformation;<br>begin<br>&nbsp; FillChar(si,SizeOf(si),0);<br>&nbsp; si.cb:=SizeOf(si);<br>&nbsp; si.dwFlags:=STARTF_USESHOWWINDOW;<br>&nbsp; si.wShowWindow:=CmdShow;<br>&nbsp; if CreateProcess(nil,PChar(CmdLine),nil,nil,false,0,nil,nil,si,pi) then<br>&nbsp; &nbsp; Result:=pi.hProcess<br>&nbsp; else<br>&nbsp; &nbsp; Result:=0;<br>end;<br><br>调用MyWinExec('C:/WINNT/system32/notepad.exe', SW_Show );但无效。怎么办?
 
看看侦测注册表的那个程序, 有源代码的, 里面有.
 
kkyy:<br>&nbsp; 是什么?在哪里有?
 
300分没有要吗?
 
给你看一个例子,就是著名的REGMON的源代码中的<br>在RegMon中双击注册码键,它能自动用Regedit打开并跳转到相应的键<br>是这样实现的, 先用FindWindow查找看当前有没有打开的RegEdit<br> regeditMainHwnd = FindWindow( "RegEdit_RegEdit", NULL );<br>如果没有,则用ShellExecute打开一个RegEdit实例<br> if ( regeditMainHwnd == NULL ) &nbsp;{<br> SHELLEXECUTEINFO info;<br> memset( &amp;info, 0, sizeof info );<br> info.cbSize = sizeof info;<br> info.fMask = SEE_MASK_NOCLOSEPROCESS; <br> info.lpVerb = "open"; <br> info.lpFile = "regedit.exe"; <br> info.nShow = SW_SHOWNORMAL; <br> ShellExecuteEx( &amp;info );<br>等待RegEdit启动完毕<br> WaitForInputIdle( info.hProcess, INFINITE );<br>找到它的主窗口句柄<br> regeditMainHwnd = FindWindow( "RegEdit_RegEdit", NULL );<br> } <br><br> if( regeditMainHwnd == NULL ) {<br><br> MessageBox( hWnd, "Regmon was unable to launch Regedit.",<br> APPNAME, MB_OK|MB_ICONERROR );<br> return;<br> }<br>把RegEdit的主窗口显示在最前面<br>&nbsp; &nbsp; ShowWindow( regeditMainHwnd, SW_SHOW );<br> SetForegroundWindow( regeditMainHwnd );<br>到这里就已经达到你的目的了。<br>接下来它要做的就是根据主窗口句柄找到RegEdit中TreeView的Handle,然后<br>再一步步展开,实现跳转到指定的键。<br>
 
你的代码没有问题,可以执行
 
pi.hProcess 是实例句柄,不是窗口句柄,是不能直接在SetForegroundWindow中使用的。<br>你想要获得的是所执行程序主窗口的句柄。<br>&nbsp;
 
关注这个问题
 
怎么我看不到任何人的解答?
 
HINSTANCE ShellExecute(<br><br>&nbsp; &nbsp; HWND hwnd, // handle to parent window<br>&nbsp; &nbsp; LPCTSTR lpOperation, // pointer to string that specifies operation to perform<br>&nbsp; &nbsp; LPCTSTR lpFile, // pointer to filename or folder name string<br>&nbsp; &nbsp; LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters <br>&nbsp; &nbsp; LPCTSTR lpDirectory, // pointer to string that specifies default directory<br>&nbsp; &nbsp; INT nShowCmd // whether file is shown when opened<br>&nbsp; &nbsp;);<br><br>这个函数的返回值就是打开程序的句柄<br>Return Values<br>If the function succeeds, the return value is the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application.<br>
 
pi.hProcess 是实例句柄,不是窗口句柄,是不能直接在SetForegroundWindow中使用的。
 
在向其他程序窗口中某一个控件里发送文本时,我做了这么一个函数,<br>但执行时连窗口都找不到,大家看看,想想办法。<br><br><br>function ShowChildWinID( WinCaption:string; CtrlClass:string ):boolean;<br>var<br>&nbsp; cnt: integer;<br><br>&nbsp; function EnumChildProc(Hwnd:THandle;lParam:LParam):boolean;<br>&nbsp; var<br>&nbsp; &nbsp; WindowCaption,WindowClass:array[0..254] of Char;<br>&nbsp; begin<br>&nbsp; &nbsp; GetClassName(Hwnd,WindowClass,255);<br>&nbsp; &nbsp; if Pos('CtrlClass',UpperCase(StrPas(WindowClass))) &gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Inc(cnt);<br>&nbsp; &nbsp; &nbsp; SendMessage(Hwnd,WM_SETTEXT,0,LongInt(PChar(IntToStr(cnt))));<br>&nbsp; &nbsp; &nbsp; //if cnt = ChildWinID then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; //SendMessage(Hwnd,WM_SETTEXT,0,LongInt(PChar(s)));<br>&nbsp; &nbsp; &nbsp;//这里是为了向固定的ChildWinID显示S &nbsp; &nbsp;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result := True;<br>&nbsp; end;<br><br>&nbsp; function EnumWindowsProc(Hwnd:THandle;lParam:LParam):boolean;<br>&nbsp; var<br>&nbsp; &nbsp; WindowCaption:array[0..254] of Char;<br>&nbsp; &nbsp; WinCapStr:string;<br>&nbsp; begin<br>&nbsp; &nbsp; GetWindowText(Hwnd,WindowCaption,255);<br>&nbsp; &nbsp; if StrPas(WindowCaption)= WinCaption then<br>&nbsp; &nbsp; WinCapStr := StrPas(WindowCaption);<br>&nbsp; &nbsp; if WinCapStr = WinCaption then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; cnt := 0;<br>&nbsp; &nbsp; &nbsp; EnumChildWindows(Hwnd,@EnumChildProc,0);<br>&nbsp; &nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result := True;<br>&nbsp; end;<br><br>begin<br>&nbsp; Enumwindows(@EnumWindowsProc,0);<br>end;<br><br>///////////////////////////////////////////////////////////////////////////<br>调用方法<br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; ShowChildWinID('FormCustom','Edit');<br>end;
 
你现在究竟是想解决哪个问题呢? [?][^]
 
后退
顶部