关于createprocess()的用法。怎么没人回答!????????? (50分)

  • 主题发起人 主题发起人 homliu
  • 开始时间 开始时间
H

homliu

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp; &nbsp;我要启动f:盘下的Cpuid.exe文件,并获取ProcessInfo,使用CreateProcess()方法,<br>但返回值为false,是参数设置有问题吗?如果是,能否对各参数作一下解释。谢谢。<br>var<br>&nbsp; CreateProcessResult:bool;<br>&nbsp; StartupInfo:tstartupinfo;<br>&nbsp; ProcessInfo:tprocessinformation;<br>begin<br>&nbsp; CreateProcessResult := CreateProcess(nil,'f://Cpuid.exe',nil,nil,true,0,nil,nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StartupInfo,ProcessInfo);
 
CreateProcessResult := CreateProcess'f:/cpuid.exe','f:/Cpuid.exe',nil,nil,true,0,nil,nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StartupInfo,ProcessInfo);
 
l213:<br>&nbsp; 仍然返回false.
 
zeromem(@startupinfo);<br>//zeromem(@processinfo);
 
给你个例子,哈哈摘自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><br>
 
以下是语法部分:<br>Syntax<br>CreateProcess(<br>lpApplicationName: PChar; {pointer to the name of the application}<br>lpCommandLine: PChar; {pointer to the command line of the application}<br>lpProcessAttributes, {pointer to process security attributes}<br>lpThreadAttributes: PSecurityAttributes; {pointer to the thread security attributes}<br>bInheritHandles: BOOL; {inheritance flag}<br><br>dwCreationFlags: DWORD; {creation flag}<br>lpEnvironment: Pointer; {pointer to environment block}<br>lpCurrentDirectory: PChar; {pointer to the current directory}<br>const lpStartupInfo: TStartupInfo; {pointer to a TStartupInfo data structure}<br>var lpProcessInformation: TProcessInformation {pointer to a TProcessInformation data structure}<br>): BOOL; {returns TRUE or FALSE}<br><br>Description<br>This function will create a new process and its primary thread. The primary thread created will have an initial stack size that is specified in the header of the executable, and the thread will begin execution at the executable image's entry point. The entry point of a Delphi executable is set by choosing Project|Options, clicking on the Linker page, an modifying the image base setting. The default value for the image base should never need to be modified.<br><br>Parameters<br>lpApplicationName: A pointer to a null terminated string that specifies the name of the executable. If this parameter is set to NIL, the lpCommandLine parameter must contain the path and executable name (i.e. C:/Windows/Wordpad.exe Readme.txt). Under Window NT, this parameter should be set to NIL and the lpCommandLine parameter should be used to specify the executable name.<br><br>lpCommandLine: A null terminated string that specifies the command line for the executable. If this parameter is set to NIL, the lpApplicationName parameter can be used for the command line of the application. If neither of these parameters are set to NIL, the lpApplicationName parameter will indicate the path and name of the application, and the lpCommandLine parameter will indicate the command line of the application. If the lpApplicationName parameter is set to NIL, the first space delimited portion of the lpCommandLine parameter will be the application name. If the EXE extension is not given, it will be appended unless there is a (.) in the filename or the filename contains the path.<br><br>lpProcessAttributes: A pointer to a TSecurityAttributes structure that specifies the security descriptor for the process. If this parameter is set to NIL, the process will have the default security descriptor and is not inheritable. See the CreateFile function for a description of this parameter.<br><br>lpThreadAttributes: A pointer to a TSecurityAttributes structure that specifies the security descriptor for the thread of the process. If this parameter is set to NIL, the thread will have the default security descriptor and is not inheritable. See the CreateFile function for a description of this parameter.<br><br>bInheritHandles: Indicates if the new process will inherit handles opened by the calling process. If this parameter is set to TRUE, the created process will inherit all the open handles of the calling process. The inherited handles will have the same value and access privileges as the original handles.<br><br>dwCreationFlags: Specifies the creation of the process and control of the priority class. Priority class defaults to NORMAL_PRIORITY_CLASS. If the creating process has a priority class of IDLE_PRIORITY_CLASS, the child default priority class will be IDLE_PRIORITY_CLASS.<br><br>lpEnvironment: A pointer to an environment block for the new process. If this parameter is set to NIL, the new process will use the environment of the calling process. Please see the GetEnvironmentStrings function for more information.<br><br>lpCurrentDirectory: A null terminated string specifying the current drive and directory for the new process. If this parameter is set to NIL, the new process will have the same current directory as the calling process.<br><br>lpStartupInfo: A pointer to a TStartupInfo structure that specifies how the main window of the new process should appear. Please see the GetStartupInfo function for more information.<br><br>lpProcessInformation: A variable of type TProcessInformation that receives information about the new process. The TProcessInformation data structure is defined as:<br><br>TProcessInformation = record<br> hProcess: THandle; {the process handle}<br> hThread: THandle; {a handle to the primary thread}<br><br>dwProcessId: DWORD; {a global process identifier}<br> dwThreadId: DWORD; {a global thread identifier}<br>end;<br><br> hProcess: A handle to the newly created process.<br><br> hThread: A handle to the primary thread of the newly created process.<br><br> dwProcessId: A global process identifier used to identify a process.<br><br> dwThreadId: A global thread identifier use to identify a thread.<br><br>Return Value<br>If the function succeeds, it returns TRUE; otherwise it returns FALSE. To get extended error information, call the GetLastError function.<br><br>
 
可能先要设置一下<br>&nbsp; StartupInfo:tstartupinfo;<br>&nbsp; ProcessInfo:tprocessinformation;<br>例如:<br>function WinExecAndWait32(FileName:String; Visibility : integer):integer;<br>var<br>&nbsp; zAppName:array[0..512] of char;<br>&nbsp; zCurDir:array[0..255] of char;<br>&nbsp; WorkDir:String;<br>&nbsp; StartupInfo:TStartupInfo;<br>&nbsp; ProcessInfo:TProcessInformation;<br>&nbsp; //MyResult:DWORD;<br><br>&nbsp; hStdIn:THandle; // standard input handle<br>&nbsp; //inputBuffer:INPUT_RECORD; &nbsp;//buffer to hold a single console input record<br>&nbsp; irMacroBuf:INPUT_RECORD; // array of input events<br>&nbsp; dwBytesWritten : DWORD;<br><br>begin<br>&nbsp; SetConsoleTitle('s');<br>&nbsp; StrPCopy(zAppName,FileName);<br>&nbsp; //MainForm.Caption := zappname;<br>&nbsp; GetDir(0,WorkDir);<br>&nbsp; StrPCopy(zCurDir,WorkDir);<br>&nbsp; FillChar(StartupInfo,Sizeof(StartupInfo),#0);<br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; hStdIn:= StartupInfo.hStdInput;<br>&nbsp; StartupInfo.hStdInput:= hStdIn;<br>&nbsp; StartupInfo.lpTitle:=@zAppName;<br>&nbsp; StartupInfo.dwFlags := StartupInfo.dwFlags or STARTF_USESTDHANDLES;<br><br>&nbsp; WriteConsoleInput(hStdIn,irMacroBuf,1,dwBytesWritten);<br><br>&nbsp; StartupInfo.dwFlags := StartupInfo.dwFlags or STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow := Visibility;<br>&nbsp; if not CreateProcess(nil,<br>&nbsp; &nbsp; zAppName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ pointer to command line string }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to process security attributes }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to thread security attributes }<br>&nbsp; &nbsp; false, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { handle inheritance flag }<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ creation flags }<br>&nbsp; &nbsp; NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to new environment block }<br>&nbsp; &nbsp; nil, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to current directory name }<br>&nbsp; &nbsp; StartupInfo, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { pointer to STARTUPINFO }<br>&nbsp; &nbsp; ProcessInfo) then Result := -1 { pointer to PROCESS_INF }<br><br>&nbsp; else begin<br>&nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess,INFINITE);<br>&nbsp; &nbsp; Result:=1;<br>&nbsp; end;<br>end;
 
&nbsp; &nbsp;我在提出问题前已经对delphi help on line 和其他资料都仔细参考过,<br>也试过你们的代码,但还是返回false,我现在也不知道原因在哪里,我只好放弃了。<br>&nbsp; &nbsp; 很感谢你们!
 
zw84611:<br>&nbsp; &nbsp;特别感谢,我今天早上再按你的方式调整了一下,运行成功。
 
后退
顶部