G goodbobi Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-03 #1 我想在我的程序启动后能运行另外一个应用程序(如:sol.exe)并得到该应用程序(sol.exe)的句柄(因为我要在我的应用程序中通过句柄控制它(sol.exe)).或有其它更好的方法.谢谢!
晶 晶晶 Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-04 #3 得到一个程序的句柄到是很容易,但真正要控制一个程序是不简单的,依靠API只是可以控制程序的一些基本的东西,至于全面控制程序我想还是要依靠COM来实现吧,因为它才是真正的进程透明的技术!就象我们使用的很多Office服务器对象一样,那才是真正控制了一个程序的运行呢!
得到一个程序的句柄到是很容易,但真正要控制一个程序是不简单的,依靠API只是可以控制程序的一些基本的东西,至于全面控制程序我想还是要依靠COM来实现吧,因为它才是真正的进程透明的技术!就象我们使用的很多Office服务器对象一样,那才是真正控制了一个程序的运行呢!
G goodbobi Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-04 #4 TO:Another_eYes<br> 能帮忙写点再具体一点吗?谢谢!<br>TO:晶晶<br> 我目前已经能控制程序了,但是在启动和得到句柄是觉得不是很理想.<br>我的做法是,先启动那个被调用的应用程序,然后再根据被调用窗口的标题得到该窗口的句柄.不知道你有何高见?<br>
TO:Another_eYes<br> 能帮忙写点再具体一点吗?谢谢!<br>TO:晶晶<br> 我目前已经能控制程序了,但是在启动和得到句柄是觉得不是很理想.<br>我的做法是,先启动那个被调用的应用程序,然后再根据被调用窗口的标题得到该窗口的句柄.不知道你有何高见?<br>
H hongxing_dl Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-04 #5 试一下下面的例子吧:<br>var piProcInfoGPSROCESS_INFORMATION;<br>调用:<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> Timer1.Enabled := True ;<br> EstablishProcess;<br>end;<br>procedure TForm1.EstablishProcess;<br>Var<br> siStartupInfo:STARTUPINFO;<br> saProcess,saThread:SECURITY_ATTRIBUTES;<br> fSuccess:boolean;<br>begin<br> fSuccess:=false;<br> ZeroMemory(@siStartupInfo,sizeof(siStartupInfo));<br> siStartupInfo.cb:=sizeof(siStartupInfo);<br> saProcess.nLength:=sizeof(saProcess);<br> saProcess.lpSecurityDescriptor:=PChar(nil);<br> saProcess.bInheritHandle:=true;<br> saThread.nLength:=sizeof(saThread);<br> saThread.lpSecurityDescriptor:=PChar(nil);<br> saThread.bInheritHandle:=true;<br> fSuccess:=CreateProcess(<br> PChar(nil),PChar(FileListBox1.FileName),@saProcess,@saThread,<br> false,CREATE_DEFAULT_ERROR_MODE,Pchar(nil),<br> Pchar(nil),siStartupInfo,piProcInfoGPS);<br> if( not fSuccess)then<br> Memo1.Lines.Add('Create Process '+FileListBox1.FileName+' fail.')<br> else<br> Memo1.Lines.Add('Create Process '+FileListBox1.FileName+' success.')<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);//监测该程序是否已经退出<br>Var<br> dwExitCodeWORD;<br> fprocessExit:boolean;<br>Begin<br> dwExitCode:=0;<br> fprocessExit:=false;<br> fprocessExit:=GetExitCodeProcess<br> (piProcInfoGPS.hProcess,dwExitCode);<br> if (fprocessExit and (dwExitCode <> STILL_ACTIVE))then begin<br> Memo1.Lines.Add('['+FileListBox1.FileName+']进程终止');<br> CloseHandle(piProcInfoGPS.hThread);<br> CloseHandle(piProcInfoGPS.hProcess);<br> end;<br>end;
试一下下面的例子吧:<br>var piProcInfoGPSROCESS_INFORMATION;<br>调用:<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> Timer1.Enabled := True ;<br> EstablishProcess;<br>end;<br>procedure TForm1.EstablishProcess;<br>Var<br> siStartupInfo:STARTUPINFO;<br> saProcess,saThread:SECURITY_ATTRIBUTES;<br> fSuccess:boolean;<br>begin<br> fSuccess:=false;<br> ZeroMemory(@siStartupInfo,sizeof(siStartupInfo));<br> siStartupInfo.cb:=sizeof(siStartupInfo);<br> saProcess.nLength:=sizeof(saProcess);<br> saProcess.lpSecurityDescriptor:=PChar(nil);<br> saProcess.bInheritHandle:=true;<br> saThread.nLength:=sizeof(saThread);<br> saThread.lpSecurityDescriptor:=PChar(nil);<br> saThread.bInheritHandle:=true;<br> fSuccess:=CreateProcess(<br> PChar(nil),PChar(FileListBox1.FileName),@saProcess,@saThread,<br> false,CREATE_DEFAULT_ERROR_MODE,Pchar(nil),<br> Pchar(nil),siStartupInfo,piProcInfoGPS);<br> if( not fSuccess)then<br> Memo1.Lines.Add('Create Process '+FileListBox1.FileName+' fail.')<br> else<br> Memo1.Lines.Add('Create Process '+FileListBox1.FileName+' success.')<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);//监测该程序是否已经退出<br>Var<br> dwExitCodeWORD;<br> fprocessExit:boolean;<br>Begin<br> dwExitCode:=0;<br> fprocessExit:=false;<br> fprocessExit:=GetExitCodeProcess<br> (piProcInfoGPS.hProcess,dwExitCode);<br> if (fprocessExit and (dwExitCode <> STILL_ACTIVE))then begin<br> Memo1.Lines.Add('['+FileListBox1.FileName+']进程终止');<br> CloseHandle(piProcInfoGPS.hThread);<br> CloseHandle(piProcInfoGPS.hProcess);<br> end;<br>end;
G goodbobi Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-04 #6 to:hongxing_dl<br>我照您这样做只能得到进程的句柄,并没有得到启动主窗口的句柄呀.我如何用Button控制这个窗口的关闭呢.<br>还望赐教
边 边尘浪子 Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-05 #7 你所谓的控制是指什么呢?<br><br>不同的程序,不同的控制方法啊~<br><br>如游戏,你可以控制它的某个数据达到某个效果、普通的窗口程序,你也可以得到handle来控制。。。。
薄 薄荷 Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-06 #9 你的窗体有标题吗? 最简单就是 FindWindow(nil,'标题');
G goodbobi Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-06 #10 有标题,但我不想能过标题得到窗口的句柄,原因是我可能同时打开两个相同的窗口,那幺得到的句柄就有可能不正确了.因为我只想控制我启动的那个窗口.其它的窗口我不要控制它.
H hongxing_dl Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-06 #11 可以这样终止进程:<br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>Var<br> dwExitCodeWORD;<br> fprocessExit:boolean;<br>Begin<br> if not timer1.Enabled then<br> timer1.Enabled:=true;<br> dwExitCode:=0;<br> fprocessExit:=false;<br> fprocessExit:=GetExitCodeProcess<br> (piProcInfoGPS.hProcess,dwExitCode);<br> if (not fprocessExit or (dwExitCode = STILL_ACTIVE)) then<br> begin<br> TerminateThread(piProcInfoGPS.hThread,dwExitCode);<br> TerminateProcess(piProcInfoGPS.hProcess,dwExitCode);<br> CloseHandle(piProcInfoGPS.hThread);<br> CloseHandle(piProcInfoGPS.hProcess);<br> end;<br>end;
可以这样终止进程:<br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>Var<br> dwExitCodeWORD;<br> fprocessExit:boolean;<br>Begin<br> if not timer1.Enabled then<br> timer1.Enabled:=true;<br> dwExitCode:=0;<br> fprocessExit:=false;<br> fprocessExit:=GetExitCodeProcess<br> (piProcInfoGPS.hProcess,dwExitCode);<br> if (not fprocessExit or (dwExitCode = STILL_ACTIVE)) then<br> begin<br> TerminateThread(piProcInfoGPS.hThread,dwExitCode);<br> TerminateProcess(piProcInfoGPS.hProcess,dwExitCode);<br> CloseHandle(piProcInfoGPS.hThread);<br> CloseHandle(piProcInfoGPS.hProcess);<br> end;<br>end;
H hurryfq Unregistered / Unconfirmed GUEST, unregistred user! 2003-11-14 #12 var<br>destHandle:Hwnd; <br> <br>WinExec(Pchar('要启动的程序路径'),SW_HIDE);<br>destHandle:=GetCurrentProcess;<br>Postmessage(desthandle,WM_KEYDOWN,88,0);
var<br>destHandle:Hwnd; <br> <br>WinExec(Pchar('要启动的程序路径'),SW_HIDE);<br>destHandle:=GetCurrentProcess;<br>Postmessage(desthandle,WM_KEYDOWN,88,0);