帮忙找一个例程(190分)

  • 主题发起人 主题发起人 wenqy
  • 开始时间 开始时间
W

wenqy

Unregistered / Unconfirmed
GUEST, unregistred user!
我想打开和关闭一个外部程序,如pro1:=c/aa/bb.exe我以前曾看到一个例程<br>用CREATEPROCESS调用改程序,得到一个HANDLE,<br>然后TerminateProcess 。<br>这两天要用却怎么也搜不到,请指点!! &nbsp;<br><br>
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>st:TStartUpInfo;<br>pp:TProcessInformation;<br>ppp:Thandle;<br>begin<br>FillChar(st,sizeof(st),#0);<br>with st do<br>begin<br>&nbsp; cb:=sizeof(st);<br>&nbsp; dwFlags:=StartF_UsesTDHandles or STARTF_USESHOWWINDOW;<br>&nbsp; lptitle:=nil;<br>&nbsp; wShowWindow:=SW_Show;<br>end;<br>CreateProcess(PChar('c:/program files/microsoft office/office/winword.exe'),<br>&nbsp; &nbsp; &nbsp; nil,nil,nil,true,DETACHED_PROCESS,nil,nil,st,pp);<br>//关闭进程<br>{ &nbsp;ppp:=OpenProcess(PROCESS_ALL_ACCESS, FALSE,pp.dwProcessId );<br>&nbsp; TerminateProcess(ppp,0);}<br>end;<br><br>
 
给你个例子,哈哈摘自CORE API HELP,DFW就有下,快去吧。<br>Creating A Semaphore To Synchronize Multiple Processes<br><br>procedure TForm1.ShowProgress;<br>var<br>&nbsp; ICount: Integer; &nbsp; &nbsp;// general loop counter<br>begin<br>&nbsp; {wait for the semaphore, and get ownership}<br>&nbsp; WaitForSingleObject(SemaphoreHandle, INFINITE);<br><br>&nbsp; {display a visual indicator}<br>&nbsp; for ICount := 1 to 1000 do<br>&nbsp; begin<br>&nbsp; &nbsp; Gauge1.Progress := ICount;<br>&nbsp; end;<br><br>&nbsp; {release the semaphore}<br>&nbsp; ReleaseSemaphore(Form1.SemaphoreHandle, 1, nil);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; StartUpInfo: TStartUpInfo; &nbsp; &nbsp; &nbsp; &nbsp;// holds startup information<br>&nbsp; ProcessInfo: TProcessInformation; // holds process information<br>&nbsp; CurDir: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // holds the current directory<br>begin<br>&nbsp; {create the semaphore, nonsignaled}<br><br>&nbsp; SemaphoreHandle := CreateSemaphore(nil, 0, 2, 'MikesSemaphore');<br><br>&nbsp; {initialize the startup info structure}<br>&nbsp; FillChar(StartupInfo, SizeOf(TStartupInfo), 0);<br>&nbsp; with StartupInfo do<br>&nbsp; begin<br>&nbsp; &nbsp; cb := SizeOf(TStartupInfo);<br>&nbsp; &nbsp; dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; wShowWindow := SW_SHOWNORMAL;<br>&nbsp; end;<br><br>&nbsp; {launch the semaphore sibling program for the example}<br>&nbsp; CurDir := ExtractFilePath(ParamStr(0))+'ProjectOpenSemaphore.exe';<br><br>&nbsp; CreateProcess(PChar(CurDir), nil, nil, nil, False,<br>&nbsp; &nbsp; &nbsp; NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; OldValue: DWORD; &nbsp;// holds the previous semaphore count<br>begin<br>&nbsp; {release the semaphore}<br>&nbsp; ReleaseSemaphore(SemaphoreHandle, 2, @OldValue);<br><br>&nbsp; {start the visual indication}<br>&nbsp; ShowProgress;<br>end;<br><br>The Semaphore Sibling Program<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; ICount: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // general loop counter<br>&nbsp; SemaphoreHandle: THandle; &nbsp; &nbsp; &nbsp;// holds the semaphore handle<br>&nbsp; PrevCount: DWORD; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// holds the previous semaphore counter<br>begin<br>&nbsp; {Open a handle to the semaphore}<br>&nbsp; SemaphoreHandle := OpenSemaphore($00f0000 or $00100000 or $3, FALSE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'MikesSemaphore');<br><br>&nbsp; {wait to achieve ownership of the semaphore}<br>&nbsp; WaitForSingleObject(SemaphoreHandle, INFINITE);<br><br>&nbsp; {display a visual indication}<br>&nbsp; for ICount := 1 to 100000 do<br>&nbsp; begin<br>&nbsp; &nbsp; Gauge1.Progress := ICount;<br>&nbsp; end;<br><br>&nbsp; {release the semaphore}<br>&nbsp; ReleaseSemaphore(SemaphoreHandle, 1, @PrevCount);<br>end;<br>
 
在google中搜索CREATEPROCESS delphi
 
请告诉我在哪里下?我找不到
 
uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,<br>&nbsp; TlHelp32, Registry;<br><br><br>&nbsp; &nbsp; function FindApp(AppName: String): Integer;<br>&nbsp; &nbsp; function KillApp(ProcessId: Integer): Integer;<br>&nbsp; &nbsp; function IsRun(ProcessId: Integer): Boolean;<br>&nbsp; &nbsp; function IsBusy(ProcessId: Integer): Integer;<br>&nbsp; &nbsp; function RunApp(AppName, CmdLine: String; nCmdShow: Integer): Integer;<br><br>function TCcfRunMonitor2.FindApp(AppName: String): Integer;<br>var<br>&nbsp; PShot: THandle;<br>&nbsp; Pe: TProcessEntry32;<br>&nbsp; CanNext: Boolean;<br>&nbsp; ProcessId: Integer;<br>&nbsp; Found: Boolean;<br>&nbsp; function FindModule: Boolean;<br>&nbsp; var<br>&nbsp; &nbsp; MShot: THandle;<br>&nbsp; &nbsp; HasNext: Boolean;<br>&nbsp; &nbsp; Me: TModuleEntry32;<br>&nbsp; &nbsp; MfName: String;<br>&nbsp; begin<br>&nbsp; &nbsp; Result := False;<br>&nbsp; &nbsp; MShot := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ProcessId);<br>&nbsp; &nbsp; Me.dwSize := SizeOf(Me);<br>&nbsp; &nbsp; HasNext := Module32First(MShot, Me);<br>&nbsp; &nbsp; while HasNext do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; MfName := Me.szExePath;<br>&nbsp; &nbsp; &nbsp; if SameText(MfName, AppName) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Result := True;<br>&nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; HasNext := Module32Next(MShot, Me); &nbsp; &nbsp;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CloseHandle(MShot);<br>&nbsp; end;<br>begin<br>&nbsp; PShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br>&nbsp; Pe.dwSize := SizeOf(Pe);<br>&nbsp; ProcessId := 0;<br>&nbsp; Found := False;<br>&nbsp; CanNext := Process32First(PShot, Pe);<br>&nbsp; while CanNext do<br>&nbsp; begin<br>&nbsp; &nbsp; ProcessId := Pe.th32ProcessID;<br>&nbsp; &nbsp; if ProcessId &gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Found := FindModule;<br>&nbsp; &nbsp; &nbsp; if Found then Break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CanNext := Process32Next(PShot, Pe);<br>&nbsp; end;<br>&nbsp; CloseHandle(PShot);<br>&nbsp; if Found then Result := ProcessId else Result := 0;<br>end;<br><br>function TCcfRunMonitor2.GetServiceController: TServiceController;<br>begin<br>&nbsp; Result := ServiceController;<br>end;<br><br>function TCcfRunMonitor2.IsBusy(ProcessId: Integer): Integer;<br>var<br>&nbsp; Ph: THandle;<br>begin<br>&nbsp; Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br>&nbsp; if Ph &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; if WaitForInputIdle(Ph, 10) = WAIT_TIMEOUT then<br>&nbsp; &nbsp; &nbsp; Result := 1<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; Result := 0;<br>&nbsp; &nbsp; CloseHandle(Ph);<br>&nbsp; end<br>&nbsp; else Result := -1;<br>end;<br><br>function TCcfRunMonitor2.IsRun(ProcessId: Integer): Boolean;<br>var<br>&nbsp; Ph: THandle;<br>begin<br>&nbsp; Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br>&nbsp; if Ph &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := WaitForSingleObject(Ph, 0) = WAIT_TIMEOUT;<br>&nbsp; &nbsp; CloseHandle(Ph);<br>&nbsp; end<br>&nbsp; else Result := False;<br>end;<br><br>function TCcfRunMonitor2.KillApp(ProcessId: Integer): Integer;<br>var<br>&nbsp; Ph: THandle;<br>begin<br>&nbsp; Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br>&nbsp; if Ph &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := Integer(TerminateProcess(Ph, 2701));<br>&nbsp; &nbsp; CloseHandle(Ph);<br>&nbsp; end<br>&nbsp; else Result := 0;<br>end;<br><br>function TCcfRunMonitor2.RunApp(AppName, CmdLine: String;<br>&nbsp; nCmdShow: Integer): Integer;<br>var<br>&nbsp; Sti: TStartupInfo;<br>&nbsp; Psi: TProcessInformation;<br>begin<br>&nbsp; FillMemory(@Sti, SizeOf(Sti), 0);<br>&nbsp; Sti.wShowWindow := nCmdShow;<br>&nbsp; Sti.dwFlags := STARTF_USEFILLATTRIBUTE;<br>&nbsp; Sti.dwFillAttribute := FOREGROUND_INTENSITY or BACKGROUND_BLUE;<br>&nbsp; if CreateProcess(PChar(AppName), PChar(CmdLine),<br>&nbsp; &nbsp; nil, nil, False,<br>&nbsp; &nbsp; 0, nil, PChar(ExtractFilePath(AppName)),<br>&nbsp; &nbsp; Sti, Psi) then<br>&nbsp; begin<br>&nbsp; &nbsp; Result := Psi.dwProcessId;<br>&nbsp; end<br>&nbsp; else Result := 0;<br>end;<br>
 
看清问题了吗,好象不应该这么复杂吧
 
是啊,只用其中的这一点就够了,<br>function TCcfRunMonitor2.RunApp(AppName, CmdLine: String;<br>&nbsp;nCmdShow: Integer): Integer;<br>var<br>&nbsp;Sti: TStartupInfo;<br>&nbsp;Psi: TProcessInformation;<br>begin<br>&nbsp;FillMemory(@Sti, SizeOf(Sti), 0);<br>&nbsp;Sti.wShowWindow := nCmdShow;<br>&nbsp;Sti.dwFlags := STARTF_USEFILLATTRIBUTE;<br>&nbsp;Sti.dwFillAttribute := FOREGROUND_INTENSITY or BACKGROUND_BLUE;<br>&nbsp;if CreateProcess(PChar(AppName), PChar(CmdLine),<br>&nbsp; &nbsp;nil, nil, False,<br>&nbsp; &nbsp;0, nil, PChar(ExtractFilePath(AppName)),<br>&nbsp; &nbsp;Sti, Psi) then<br>&nbsp;begin<br>&nbsp; &nbsp;Result := Psi.dwProcessId;<br>&nbsp;end<br>&nbsp;else Result := 0;<br>end;<br><br>function TCcfRunMonitor2.KillApp(ProcessId: Integer): Integer;<br>var<br>&nbsp;Ph: THandle;<br>begin<br>&nbsp;Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br>&nbsp;if Ph &lt;&gt; 0 then<br>&nbsp;begin<br>&nbsp; &nbsp;Result := Integer(TerminateProcess(Ph, 2701));<br>&nbsp; &nbsp;CloseHandle(Ph);<br>&nbsp;end<br>&nbsp;else Result := 0;<br>end;<br>
 
来晚了!
 
不知咋搞的老是出错,函数声明<br>function FindApp(AppName: String): Integer;<br>&nbsp; &nbsp;function KillApp(ProcessId: Integer): Integer;<br>&nbsp; &nbsp;function IsRun(ProcessId: Integer): Boolean;<br>&nbsp; &nbsp;function IsBusy(ProcessId: Integer): Integer;<br>&nbsp; &nbsp;function RunApp(AppName, CmdLine: String; nCmdShow: Integer): Integer;<br>应该写在那里.试了一下,怎么提示多处错误!!<br>我以前编的函数,不用声明,直接带上参数用就行了.<br>谁能简化一下<br>我只想打开和关闭一个外部程序,如pro1:=c/aa/bb.exe<br>
 
声明放在 implementation 之前<br>函数实现放在其之后<br><br>声明:<br>function .RunApp(AppName, CmdLine: String;<br>nCmdShow: Integer): Integer;<br>function KillApp(ProcessId: Integer): Integer;<br><br><br>实现:<br>function .RunApp(AppName, CmdLine: String;<br>nCmdShow: Integer): Integer;<br>var<br>Sti: TStartupInfo;<br>Psi: TProcessInformation;<br>begin<br>FillMemory(@Sti, SizeOf(Sti), 0);<br>Sti.wShowWindow := nCmdShow;<br>Sti.dwFlags := STARTF_USEFILLATTRIBUTE;<br>Sti.dwFillAttribute := FOREGROUND_INTENSITY or BACKGROUND_BLUE;<br>if CreateProcess(PChar(AppName), PChar(CmdLine),<br>&nbsp; nil, nil, False,<br>&nbsp; 0, nil, PChar(ExtractFilePath(AppName)),<br>&nbsp; Sti, Psi) then<br>begin<br>&nbsp; Result := Psi.dwProcessId;<br>end<br>else Result := 0;<br>end;<br><br>function KillApp(ProcessId: Integer): Integer;<br>var<br>Ph: THandle;<br>begin<br>Ph := OpenProcess(PROCESS_ALL_ACCESS, false, ProcessId);<br>if Ph &lt;&gt; 0 then<br>begin<br>&nbsp; Result := Integer(TerminateProcess(Ph, 2701));<br>&nbsp; CloseHandle(Ph);<br>end<br>else Result := 0;<br>end;<br>
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
889
DelphiTeacher的专栏
D
D
回复
0
查看
857
DelphiTeacher的专栏
D
D
回复
0
查看
808
DelphiTeacher的专栏
D
后退
顶部