我打算将我的 Delphi (XE6) GUI 应用程序的 32 位和 64 位版本捆绑在一个 Zip 存档中。
注意:在这个问题中,我们假设用户提取了整个存档,即没有直接从存档管理器运行可执行文件。
所以,让我们假设我们已经提取了存档并有两个文件:
program.exe
program64.exe
明确地说,命名约定是 32 位版本的名称中除了程序名称之外没有任何其他内容,而 64 位版本具有完全相同的程序名称和64
后缀。
如果用户在 64 位 Windows 机器上运行 32 位版本,我希望它检测到它,并关闭自身并运行 64 位版本。
在我自己的答案中,我将记录我在使用代码期间的所有发现,随时添加更好的答案或替代方案,如果它真的做出了一些贡献,我肯定会赞成。也许更重要的是,请对您发现错误的特定部分发表评论。
首先,我们需要一个能够启动其他可执行文件的函数,下面是我非常通用的示例,您可以根据需要使用它来启动几乎任何其他可执行文件:
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 个错误代码,这就是为什么函数True
在结果ShellExecute
大于 时返回的原因32
。
我定义了 function 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 位系统的函数,更具体地说,如果可执行文件在 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}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句