gpt4 book ai didi

delphi - Embarcadero Delphi 和 Widechar : simple function that doesn't work

转载 作者:行者123 更新时间:2023-12-01 22:37:36 25 4
gpt4 key购买 nike

我真的不明白为什么这个功能不起作用:

function GetNomRepertoireTemporaire:WideString;
var
PathLocal : array[0..MAX_PATH+1] of WideChar;
begin
Result := '';
if GetTempPath(SizeOf(PathLocal)-1, PathLocal)>0 then
begin
Result := PathLocal;
end;
end;

当我这样调用它时:

var
t : wideString;
initialization
t := GetNomRepertoireTemporaire;

我等待大约 10 秒,然后在 0x000000 地址 0000000 处收到 AV

有人可以解释一下我做错了什么吗?

最佳答案

您应该在代码中使用 Length 而不是 SizeOf:

function GetNomRepertoireTemporaire:WideString;
var
PathLocal : array[0..MAX_PATH] of WideChar;
begin
Result := '';
if GetTempPath(Length(PathLocal), PathLocal)>0 then
begin
Result := PathLocal;
end;
end;

上面的代码假设您使用的是 Unicode Delphi 版本。正如 David 在评论中提到的,您可以更改函数以使其与 Unicode 和非 Unicode Delphi 兼容:

function GetNomRepertoireTemporaire:String;
var
PathLocal : array[0..MAX_PATH] of Char;
begin
Result := '';
if GetTempPath(Length(PathLocal), PathLocal)>0 then
begin
Result := PathLocal;
end;
end;
<小时/>

说明:GetTempPath 函数将其接收的整个缓冲区填充为零。 OP 代码设置了无效的缓冲区大小(实际大小的两倍),因此该函数将 PathLocal 变量后面的内存清零,从而导致 AV。

关于delphi - Embarcadero Delphi 和 Widechar : simple function that doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6279186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com