启动其他可执行文件
首先,我们需要一个能够启动其他可执行文件的函数,下面是我非常通用的示例,您可以根据需要使用它来启动几乎任何其他可执行文件:
function LaunchExecutableFile(const ExecutableFilePath, Parameters: string; const ShowCmd: Integer): Boolean;
begin
Result := Winapi.ShellAPI.ShellExecute(Application.MainFormHandle, 'open', PChar(StringFunctions.DoubleQuoteStr(ExecutableFilePath)), PChar(Parameters), nil, ShowCmd) > 32;
end;
注意事项:
我特意添加了Winapi.ShellAPI... 等可选命名空间,以便您准确了解这些函数的定义位置。
定义了32个错误码,这就是为什么如果ShellExecute的结果大于32,函数返回True。
-
我定义了函数DoubleQuoteStr,因为如果路径中有空格,系统会在每个空格分隔的文件中查找文件,因此路径错误。这是一个非常简单的功能,完全是可选的,只是优化。这个也是通用的函数如下:
function DoubleQuoteStr(S: string): string;
begin
if (S = '') or (S = '"')
then S := '""'
else begin
if S[1] <> '"' then S := '"' + S;
if S[System.Length(S)] <> '"' then S := S + '"';
end;
Result := S;
end;
遗憾的是,我仍然不确定第一个 ShellExecute HWND 参数,更具体地说,如果我的通用方法是正确的,请随时纠正我!
检测 64 位系统
其次,我们需要一个能够检测 64 位系统的函数,更具体地说,如果可执行文件在 WOW64 下运行。
function IsWow64Process: Boolean;
type
TIsWow64Process = function(AHandle: DWORD; var AIsWow64: BOOL): BOOL; stdcall;
var
hIsWow64Process: TIsWow64Process;
hKernel32: DWORD;
IsWow64: BOOL;
begin
Result := False;
hKernel32 := Winapi.Windows.LoadLibrary('kernel32.dll');
if hKernel32 = 0 then Exit;
try
@hIsWow64Process := Winapi.Windows.GetProcAddress(hKernel32, 'IsWow64Process');
if not System.Assigned(hIsWow64Process) then Exit;
IsWow64 := False;
if hIsWow64Process(Winapi.Windows.GetCurrentProcess, IsWow64) then
Result := IsWow64;
finally
Winapi.Windows.FreeLibrary(hKernel32);
end;
end;
注意事项:
如您所见,函数从中加载库kernel32.dll 和函数IsWow64Process。
每个安全措施都应该到位,以便返回正确的结果。
运行程序
最后,我们需要调整我们的dpr文件。
在变量部分,添加:
var
{$IFNDEF WIN64}
App64: string;
{$ENDIF}
将您的主要begin - end 部分包含在另一个begin - end 中。
并在开头添加这样的内容:
{$IFNDEF WIN64}
App64 := System.SysUtils.ChangeFileExt(Application.ExeName, '64.exe');
if not (ProcessFunctions.IsWow64Process and System.SysUtils.FileExists(App64) and
ProcessFunctions.LaunchExecutableFile(App64, '', SW_SHOWNORMAL)) then
{$ENDIF}