我真的不明白为什么这个函数不能工作:
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秒,然后我得到了一个AV at 0x000000 address 0000000
有人能解释一下我哪里做错了吗?
发布于 2011-06-08 21:19:39
您应该在代码中使用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函数将它接收到的整个缓冲区填充为零。操作码设置无效的缓冲区大小(是实际大小的两倍),因此该函数将PathLocal变量后面的内存置零,从而产生AV。
发布于 2011-06-08 22:00:19
尽管这不能直接回答这个问题,但调用内置的RTL方法IOUtils.TPath.GetTempPath可能更好。
发布于 2011-06-08 21:20:01
如果您阅读GetTempPath接口的帮助文件,您将看到第一个参数是缓冲区的大小,单位为TCHAR。(即缓冲区中的字符数)
现在,您为函数提供了缓冲区中的字节数,这是字符数的两倍。
如下所示更改函数:
if GetTempPath(Length(PathLocal)-1, PathLocal)>0 then https://stackoverflow.com/questions/6279186
复制相似问题