如何使用CreatePrecess函数启动一个应用程序?(100分)

Y

yylg

Unregistered / Unconfirmed
GUEST, unregistred user!
用老的WinEXEC函数调用程序得不到该程序的句柄,我想使用createprocess函数,可出现了参数方面的问题(好像使用API,参数匹配问题总是很多)。<br>当勉强凑成合法的参数之后,却发现应用程序根本没有被调用,请教高手,能否给我一个能够直接通过的例子,如调用c:/windows/sol.exe 。
 
不明白了?<br><br>uses ShellAPI;<br><br>ShellExecute(GaetDeskTopWindow,'Open','c:/windows/sol.exe ',nil,nil,0);
 
<br>program DelMe;<br><br>{$APPTYPE CONSOLE}<br><br>uses<br>&nbsp; Windows, SysUtils;<br>var &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; szSrc, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br>&nbsp; szDest &nbsp; &nbsp;: array[0..MAX_PATH] of char; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; szCmdLine : string;<br>&nbsp; hFile &nbsp; &nbsp; : THandle;<br>&nbsp; hProcess &nbsp;: THandle;<br>&nbsp; SI &nbsp; &nbsp; &nbsp; &nbsp;: TStartupInfo;<br>&nbsp; PI &nbsp; &nbsp; &nbsp; &nbsp;: TProcessInformation;<br>&nbsp; nTimes &nbsp; &nbsp;: DWord;<br>begin<br>if ParamCount = 0 then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp;GetModuleFileName(0, szSrc, MAX_PATH);<br>&nbsp; &nbsp;GetTempPath(MAX_PATH, szDest);<br>&nbsp; &nbsp;GetTempFileName(szDest, 'Tmp', 0, szDest);<br>&nbsp; &nbsp;CopyFile(szSrc, szDest, FALSE); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 将当前可执行文件复制一个副本<br><br>&nbsp; &nbsp;hFile := CreateFile(szDest, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // pointer to name of the file<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// access (read-write) mode<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_SHARE_READ, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// share mode<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to security attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OPEN_EXISTING, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// how to create<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_FLAG_DELETE_ON_CLOSE, &nbsp;// file attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle to file with attributes to copy<br><br>&nbsp; &nbsp;hProcess := OpenProcess(SYNCHRONIZE, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// access flag<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TRUE, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle inheritance flag<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GetCurrentProcessId); &nbsp; // process identifier<br><br><br>&nbsp; &nbsp;szCmdLine := Format('%s %d "%s"', [szDest, hProcess, szSrc]); //格式化命令行参数<br><br>&nbsp; &nbsp;FillChar(SI, SizeOf(SI), 0);<br>&nbsp; &nbsp;SI.cb := SizeOf(SI);<br><br>&nbsp; &nbsp;CreateProcess(nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to name of executable module<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PChar(szCmdLine), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // pointer to command line string<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to process security attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to thread security attributes<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TRUE, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handle inheritance flag<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// creation flags<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to new environment block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to current directory name<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SI, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // pointer to STARTUPINFO<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PI); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pointer to PROCESS_INFORMATION<br><br>&nbsp; &nbsp;CloseHandle(hProcess);<br>&nbsp; &nbsp;CloseHandle(hFile);<br>&nbsp; &nbsp;end<br>else<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp;hProcess := THANDLE(StrToInt(ParamStr(1)));<br>&nbsp; &nbsp;WaitForSingleObject(hProcess, INFINITE); &nbsp; &nbsp; &nbsp; &nbsp;//等待主进程结束<br>&nbsp; &nbsp;CloseHandle(hProcess);<br><br>&nbsp; &nbsp;hFile := FileOpen(ParamStr(2), fmOpenReadWrite);<br>&nbsp; &nbsp;if hFile &gt; 0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; FileSeek(hFile, $80, 0);<br>&nbsp; &nbsp; &nbsp; FileRead(hFile, nTimes, SizeOf(nTimes));<br>&nbsp; &nbsp; &nbsp; Inc(nTimes);<br>&nbsp; &nbsp; &nbsp; FileSeek(hFile, $80, 0);<br>&nbsp; &nbsp; &nbsp; FileWrite(hFile, nTimes, SizeOf(nTimes));<br>&nbsp; &nbsp; &nbsp; FileClose(hFile);<br>&nbsp; &nbsp; &nbsp; Writeln('我已经运行'+IntToStr(nTimes)+'次了');<br>&nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp;end;<br>end.
 
用“createprocess”搜索一下以前的已答问题吧,答案太多了!!!
 
来晚了!唉!
 
把返回值为tseug的例子裁剪了一下,现在应该通俗易懂了吧:)<br><br>按WinExec函数同样的参数执行一个命令,执行成功则返回进程句柄,否则返回0;<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;
 
使用ShellExecute函数<br>ShellExecute(Handle,'open',PChar(Edit1.Text),'','',SW_SHOWNORMAL); <br><br>ShellExecute(Handle,'open', 'c:/doc/bar.doc' ,'','',SW_SHOWNORMAL);
 
多人接受答案了。
 
顶部