windows - 如何在 Pascal Script/Inno Setup 中使用 WinAPI 中的 PathCombine()?

标签 windows winapi inno-setup pascalscript

我正在尝试弄清楚如何使用 Pascal Script/Inno Setup 中的 WinAPI 函数。我没有找到太多如何做到这一点的代码示例,而且我不是 Pascal 程序员。这是我到目前为止所做的:

导入函数

function PathCombine (
    pszPathOut : PChar;
    pszPathIn  : PChar;
    pszMore    : PChar
) : PChar;
  external '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="045465706c476b69666d6a614544576c687365746d2a606868" rel="noreferrer noopener nofollow">[email protected]</a> stdcall';

并像这样使用它:

function InitializeSetup(): Boolean;
var 
  a, b,c : PChar;
  s : string;
begin
   SetLength(s, 256); { soon it gets working I'll switch to use MAX_PATH instead of }
   a := 'C:';
   b := 'one\two';
   c := PathCombine(s, a, b);
   MsgBox(s, mbInformation, MB_OK);
end;

输出是这样的:

enter image description here

预期输出是:

C:\one\two

我很确定我正在访问内存中的垃圾值,但我不知道为什么,我该如何解决这个问题?

最佳答案

您没有指定您使用的是 Ansi 还是 Unicode 版本的 Inno Setup。

但这应该适用于任一版本:

function PathCombine(
   pszPathOut : PAnsiChar;
   pszPathIn  : PAnsiChar;
   pszMore    : PAnsiChar
) : PAnsiChar; external '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="247445504c674b49464d4a416564774c485345544d0a404848" rel="noreferrer noopener nofollow">[email protected]</a> stdcall';

function InitializeSetup(): Boolean;
var 
  a, b, c: AnsiString;
begin
   SetLength(c, 256); { soon it gets working I'll switch to use MAX_PATH instead of }
   a := 'C:';
   b := 'one\two';
   PathCombine(c, a, b);
   MsgBox(c, mbInformation, MB_OK);
   Result := True;
end;

尽管我强烈建议您改用 Unicode version of Inno SetupPathCombineW

function PathCombine(
   pszPathOut : string;
   pszPathIn  : string;
   pszMore    : string
) : Cardinal; external '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="065667726e45696b646f68635146556e6a7167766f28626a6a" rel="noreferrer noopener nofollow">[email protected]</a> stdcall';

function InitializeSetup(): Boolean;
var 
  a, b, c: string;
begin
   SetLength(c, 256); { soon it gets working I'll switch to use MAX_PATH instead of }
   a := 'C:';
   b := 'one\two';
   PathCombine(c, a, b);
   MsgBox(c, mbInformation, MB_OK);
   Result := True;
end;

请注意,Inno Setup 缺少 PWideChar 类型。虽然它可以将 string 编码(marshal)至 LPTSTR (PWideChar) 函数参数,但它无法编码(marshal) LPTSTR 返回值。所以我使用了 Cardinal 作为返回类型。它的大小与指针(指向字符)相同,因此堆栈将匹配。而我们实际上并不需要返回值。

关于windows - 如何在 Pascal Script/Inno Setup 中使用 WinAPI 中的 PathCombine()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31666876/

相关文章:

Windows批处理文件从文本文件中提取环境变量

c# - 托盘应用程序允许系统在注销或退出时终止我的程序

c++ - 如何在 64 位版本的 Windows 中调用 SetWindowLong()?

windows - 使用 Delphi 在 Windows 7 中检测用户何时锁定/解锁屏幕

inno-setup - 从 [Run] Afterinstall 调用 [code] 中的两个过程

inno-setup - 是否可以在Inno Setup中更改卸载图标?

.net - 如何在没有管理员权限的情况下初始化Windows套接字库

c++ - CMake - 尝试构建项目时遇到 undefined reference

c# - Windows 上等效的 UpdateSystemActivity 是什么

inno-setup - Inno Setup 预处理器可以使用 FileOpen 读取文件,但不能使用 ReadIni