在windows下面之行一个程序实际上是创建一个进程,使用CreateProcess函数.过程是这样的:
执行起来的程序实际上是windows shell调用CreateProcess函数来激活的.所以,你所执行的程序就是shell的子进程.但是,shell通过调用CreateProcess是自动切断你的程序与shell只见的这种联系.
下面的代码是非常流行的.
例子为:
s:=apppath+'mini_fpc/ppc386 -WR '+apppath+'mini_fpc/'+MyDll+'.pp';
WinExecAndWait32(s,0);
function WinExecAndWait32(FileName:String; Visibility :integer):integer;
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,
zAppName, { pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then Result :=DWORD(-1) { pointer to PROCESS_INF }
else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,DWORD(Result));
end;
end;