我正在尝试弄清楚如何使用 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;
输出是这样的:
预期输出是:
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 Setup 和 PathCombineW
。
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/