关于createprocess()的用法。总返回false.(50分)

H

homliu

Unregistered / Unconfirmed
GUEST, unregistred user!
我要启动f:盘下的Cpuid.exe文件,并获取ProcessInfo,使用CreateProcess()方法,
但返回值为false,是参数设置有问题吗?如果是,能否对各参数作一下解释。谢谢。
var
CreateProcessResult:bool;
StartupInfo:tstartupinfo;
ProcessInfo:tprocessinformation;
begin
CreateProcessResult := CreateProcess(nil,'f://Cpuid.exe',nil,nil,true,0,nil,nil,
StartupInfo,ProcessInfo);
 
首先你必须明确一点:正确使用CreateProcess()函数执行外部命令必须结合WaitForSingle()函数。
同时注意调用SetEnvironmentVariable()来设置相关的环境变量。

函数原型是:
BOOL CreateProcess(
LPCTSTR lpApplicationName, // name of executable module
LPTSTR lpCommandLine, // command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
BOOL bInheritHandles, // handle inheritance option
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new environment block
LPCTSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFO lpStartupInfo, // startup information
LPPROCESS_INFORMATION lpProcessInformation // process information
);
第一个参数是可执行文件的全路径。第二个参数是其所带的命令行,
第三和第四个参数是分别是进程和线程的安全属性,如为null , 则继承父亲。
第五个表示是否继承父亲打开的句斌,第六个是一些创建标志具体可察看msdn
其余的可察看msdn。

给出一个示例:
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);

CreateProcess( para, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi )
只要把para改成你的可执行文件名字!!

成功是返回true, 失败返回false

再给你贴个例子:
这个 process Execute 封装了createProcess()win32API 函数。用于调用另一个
应用程序。commanline参数指定了要调用的应用程序,另一个参数包含了SW_XXX
常量,用于控制窗口的显示方式,有SW_HIDE,SW_SHOW,SW_SHOWNRMAL等。
具体请见帮助。
function ProcessExecute(CommandLine: TCommandLine; cShow: Word): Integer;
{ This method encapsulates the call to CreateProcess() which creates
a new process and its primary thread. This is the method used in
Win32 to execute another application, This method requires the use
of the TStartInfo and TProcessInformation structures. These structures
are not documented as part of the Delphi 4 online help but rather
the Win32 help as STARTUPINFO and PROCESS_INFORMATION.

The CommandLine parameter specifies the pathname of the file to
execute.

The cShow parameter specifies one of the SW_XXXX constants which
specifies how to display the window. This value is assigned to the
sShowWindow field of the TStartupInfo structure. }
var
Rslt: LongBool;
StartUpInfo: TStartUpInfo; // documented as STARTUPINFO
ProcessInfo: TProcessInformation; // documented as PROCESS_INFORMATION
begin
{ Clear the StartupInfo structure }
FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
{ Initialize the StartupInfo structure with required data.
Here, we assign the SW_XXXX constant to the wShowWindow field
of StartupInfo. When specifying a value to this field the
STARTF_USESSHOWWINDOW flag must be set in the dwFlags field.
Additional information on the TStartupInfo is provided in the Win32
online help under STARTUPINFO. }
with StartupInfo do
begin
cb := SizeOf(TStartupInfo); // Specify size of structure
dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
wShowWindow := cShow
end;

{ Create the process by calling CreateProcess(). This function
fills the ProcessInfo structure with information about the new
process and its primary thread. Detailed information is provided
in the Win32 online help for the TProcessInfo structure under
PROCESS_INFORMATION. }
Rslt := CreateProcess(PChar(CommandLine), nil, nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);
{ If Rslt is true, then the CreateProcess call was successful.
Otherwise, GetLastError will return an error code representing the
error which occurred. }
if Rslt then
with ProcessInfo do
begin
{ Wait until the process is in idle. }
WaitForInputIdle(hProcess, INFINITE);
CloseHandle(hThread); // Free the hThread handle
CloseHandle(hProcess);// Free the hProcess handle
Result := 0; // Set Result to 0, meaning successful
end
else Result := GetLastError; // Set result to the error code.
end;

调用:
var WERetVal: Word;

WERetVal := ProcessExecute(FCommandLine, sw_ShowNormal);
if WERetVal <> 0 then begin
raise Exception.Create('Error executing program. Error Code:; '+
IntToStr(WERetVal));
end;
都是以前的帖子里的,呵呵。
 
全文检索 CreateProcess
可以找到有关这方面的贴子
好多
 
我在提出问题前已经对delphi help on line 和其他资料都仔细参考过,
也试过你们的代码,但还是返回false,我现在也不知道原因在哪里,我只好放弃了。
很感谢你们!
 

Similar threads

S
回复
0
查看
987
SUNSTONE的Delphi笔记
S
S
回复
0
查看
805
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部