我在stackoverflow中找到了下一个功能代码。当我尝试将诸如“ C:\ teste.zip”之类的文件转换为URL格式时,结果字符串包含额外的尾随#0字符。我的结果是''file:/// C:/teste.zip#0'我在做什么错?预先感谢,路易斯
function FilePathToURL(const FilePath: string): string;
var
Buffer: string;
BufferSize: DWORD;
begin
Result := '';
BufferSize := Length('file:///' + FilePath + #0);
SetLength(Buffer, BufferSize);
if UrlCreateFromPath(PChar(FilePath), PChar(Buffer), @BufferSize, 0) = S_OK then
Result := Buffer;
end;
您正在BufferSize
以错误的方式计算期望值。根据MS文档,最大预期长度为INTERNET_MAX_URL_LENGTH
。
将Path转换为Url的正确代码是:
const
INTERNET_MAX_URL_LENGTH = 2048 + 32 + 3;
function FilePathToURL(const FilePath: string): string;
var
Buffer: array [0.. INTERNET_MAX_URL_LENGTH - 1] of Char;
BufferSize: DWORD;
begin
Result := '';
BufferSize := INTERNET_MAX_URL_LENGTH;
if UrlCreateFromPath(PChar(FilePath), @Buffer, @BufferSize, 0) = S_OK then
Result := Buffer;
end;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句