可以用CreateProcess生成后台运行的程序吗?(100分)

  • 主题发起人 主题发起人 那锦渤
  • 开始时间 开始时间

那锦渤

Unregistered / Unconfirmed
GUEST, unregistred user!
要求保留当前窗体的活动状态,不失去焦点,必须用CreateProcess
 
试试下面的代码<br>var<br>&nbsp; StartupInfo: TStartupInfo;<br>&nbsp; ProcessInfo: TProcessInformation;<br>begin<br>&nbsp; FillChar(StartupInfo, SizeOf(StartupInfo), #0);<br>&nbsp; StartupInfo.cb := SizeOf(StartupInfo);<br>&nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;<br>&nbsp; StartupInfo.wShowWindow := SW_HIDE;<br>&nbsp; Assert(CreateProcess(nil, 'notepad.exe', nil, nil, False, 0, nil, nil,<br>&nbsp; &nbsp; StartupInfo, ProcessInfo));<br>end;
 
还是不行,以下是我的代码。<br>function TMainForm.SetServerStatus(Action: TActionType): boolean;<br>var<br>&nbsp; ProcessEntry: TProcessEntry32;<br>&nbsp; CompleteFlg : boolean;<br>&nbsp; ProcessHand, HProcess : THandle;<br>&nbsp; StartUpInfo: TStartUpInfo;<br>&nbsp; ProcessInfo: TProcessInformation;<br>begin<br>&nbsp; result:= false;<br>&nbsp; ProcessHand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);<br>&nbsp; CompleteFlg := Process32First(ProcessHand, ProcessEntry);<br>&nbsp; ProcessEntry.dwSize:= SizeOf(ProcessEntry);<br>&nbsp; if Action in [ActFind, ActTerminate] then<br>&nbsp; while CompleteFlg do<br>&nbsp; begin<br>&nbsp; &nbsp; if ProcessEntry.szExeFile= GetServerName then<br>&nbsp; &nbsp; case Action of<br>&nbsp; &nbsp; &nbsp; ActFind:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Result:= true;<br>&nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; ActTerminate:<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; HProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessEntry.th32ProcessID);<br>&nbsp; &nbsp; &nbsp; &nbsp; TerminateProcess(HProcess,0);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CompleteFlg:= Process32Next(ProcessHand, ProcessEntry);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; 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:= SW_HIDE;}<br>&nbsp; &nbsp; if not CreateProcess(nil, Pchar(ServerPos), nil, nil, false, CREATE_NEW_CONSOLE or<br>&nbsp; &nbsp; &nbsp; NORMAL_PRIORITY_CLASS, nil, nil, StartUpInfo, PROCESSINFO) then<br>&nbsp; &nbsp; &nbsp; Application.MessageBox(Pchar('无法运行' + ServerPos), '错误!', MB_OK);<br>&nbsp; end;<br>end;<br>
 
你用createprocess生成程序之后,然后用registerServiceProcess隐藏进程。<br>function RegisterServiceProcess(dwProcessID, dwType: Integer): Integer;stdcall;<br>&nbsp; &nbsp;external 'KERNEL32.DLL';<br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; RegisterServiceProcess(ProcessID, 1);//隐藏<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; RegisterServiceProcess(ProcessID, 0);//出现<br>end;
 
接受答案了.
 
后退
顶部