如何传递应用程序的返回值 Delphi / Windows SDK/APIhttp://www.delphi2007.net/DelphiAPI/html/delphi_20061127133508166.html
A.exe要调用B.exe,B需要返回一些值给A使用,请问B的返回值,在应用程序B中应该怎么写呀?同时A调用了B后,如何读取他的返回值呀?请各位大侠帮帮,谢谢!
沙发
//方案1 消息机制
把A的窗体句柄传递给B
//...
public
procedure WMUSER10(var Msg: TMessage); message WM_USER + 10;
end;
//...
implementation
//...
procedure TForm1.Button1Click(Sender: TObject);
begin
WinExec(PChar(ParamStr(0) + ' ' + IntToStr(Handle)), SW_SHOW);
end;
procedure TForm1.WMUSER10(var Msg: TMessage);
begin
Caption := IntToStr(Msg.WParam); //接收消息
end;
B中处理后发送给A的窗体
procedure TForm2.Button1Click(Sender: TObject);
begin
SendMessage(StrToIntDef(ParamStr(1), 0), WM_USER + 10, 12345, 0);
end;
ParamStr(0)是我做测试的,用的时候换成B的文件名(注意路径)
方案2 利用CreateProcess()Api函数, 推荐
function ExecProcess(mCommandLine: string): Longword;
var
vStartupInfo: TStartupInfo;
vProcessInformation: TProcessInformation;
begin
FillChar(vStartupInfo, SizeOf(TStartupInfo), 0);
FillChar(vProcessInformation, SizeOf(TProcessInformation), 0);
vStartupInfo.cb := SizeOf(TStartupInfo);
vStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
vStartupInfo.wShowWindow := SW_NORMAL;
if CreateProcess(nil, PChar(mCommandLine), nil, nil, False,
CREATE_NEW_PROCESS_GROUP or NORMAL_PRIORITY_CLASS,
nil, nil, vStartupInfo, vProcessInformation) then
begin
WaitForSingleObject(vProcessInformation.hProcess, INFINITE);
GetExitCodeProcess(vProcessInformation.hProcess, Result);
CloseHandle(vProcessInformation.hThread);
CloseHandle(vProcessInformation.hProcess);
end else Result := 0;
end; { ExecProcess }
procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := IntToStr(ExecProcess('C:\temp\B.exe')); // 调用B
end;
B工程中最后一句加上你要返回的值即可:
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
ExitCode := 1234; //《〈〈〈〈〈〈〈〈〈〈
end.
学习