这儿看看!!(100分)

  • 主题发起人 主题发起人 Winder_handle
  • 开始时间 开始时间
W

Winder_handle

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是一个能让程序连续执行的函数(即:等待一个完后继续执行下一个程序....),但怎么
不行呢?请好心人指点。谢谢!!
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 := -1 { pointer to PROCESS_INF }

else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);//这句有错
end;
end;

 
改为
function WinExecAndWait32(FileName:String; Visibility : integer):DWORD;

-----
http://www.8421.org
 
怎么?没有人愿意帮忙吗?
 
改了没,还不行?
 
改了函数不能通过!
 
帮我呀,我一向给分的,不管能不能解决!
 
function WinExecAndWait32(FileName:String; Visibility : integer):DWord;
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
dw: DWord;
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 := -1 { pointer to PROCESS_INF }

else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,dw);//这句有错
Result := dw;
end;
end;
 
else begin //这句有错
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,dw);
Result := dw;
end;
end;
 
调试好的:

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;
dw: DWord;
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 := -1 { pointer to PROCESS_INF }

else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,dw);//这句有错
Result := dw;
end;
end;
 
谢谢,我先试试
 
to:qdyoung
帮忙帮到底,函数是通过了,但没有达到我要连续执行的目的(即:等待一个程序执行完毕后执行下一个程序)
先谢了。
 
下面的方法我没试过,你试试看,记得告诉我结果哟!
********************* 方法一 **********************
procedure TForm1.Button1Click(Sender: TObject);
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
ShowMessage('Ready to launch NotePad...');
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
CreateProcess( nil, 'NOTEPAD.EXE', nil, nil, False, 0, nil, nil,
StartupInfo, ProcessInfo);
with ProcessInfo do
begin
CloseHandle(hThread);
WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
end;
ShowMessage('NotePad has terminated.');
end;


********************* 方法二 **********************

procedure TfrmMain.Button1Click(Sender: TObject);
var cmdStr:pchar;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
cmdstr:=pchar('c:/windows/Notepad.exe');
{建立进程并等待其结束}

fillchar(StartupInfo,sizeof(StartupInfo),0);
CreateProcess(nil,cmdstr,nil,nil,false,0,nil,nil,StartupInfo,Pro
cessInfo);
With ProcessInfo do begin
CloseHandle(hThread);
WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
end;
end;
 
我都是用自己写的这个函数,没有问题
function Run(sCommandLine: string): Boolean;
var
lpStartupInfo: TStartupInfo;
lpProcessInformation: TProcessInformation;
begin
FillChar(lpStartupInfo, Sizeof(TStartupInfo), #0);
lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
lpStartupInfo.wShowWindow := SW_SHOW;
Result := CreateProcess(nil, PChar(sCommandLine),
nil, nil, True, NORMAL_PRIORITY_CLASS, nil, nil,
lpStartupInfo, lpProcessInformation) and
(WaitForSingleObject(lpProcessInformation.hProcess, 3600000) <> WAIT_FAILED);
end;
 
to:qdyoung 再次谢谢你,让我试试,你也帮我试试,好吗?最好给出调用的代码
 
to:BDFan
下面是我用你的第一种方法的具体调用:
procedure WaitAndRun(filename: string);
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
ShowMessage('Ready to launch NotePad...');
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
CreateProcess( nil, pchar(filename), nil, nil, False, 0, nil, nil,
StartupInfo, ProcessInfo);
with ProcessInfo do
begin
CloseHandle(hThread);
WaitForSingleObject(hProcess, INFINITE);
CloseHandle(hProcess);
end;
ShowMessage('NotePad has terminated.');
end;
procedure TForm1.Button1Click(Sender: TObject);
var f:string;
begin
f:='E:/setup/mapX/MapXtremeNT/MapXtremeNT/SETUP';
WaitAndRun(f);
f:='E:/setup/mapX/MapX_v45/SETUP';
WaitAndRun(f);
end;
不过,执行很迟钝,也不稳定,再次向你请教,谢谢;
(吃完饭结帖)
 
多人接受答案了。
 
后退
顶部