gpt4 book ai didi

delphi - 将函数转换为delphi 2010(unicode)

转载 作者:行者123 更新时间:2023-12-03 18:39:39 31 4
gpt4 key购买 nike

如何将此函数转换为Delphi 2010(Unicode)?

function TForm1.GetTarget(const LinkFileName:String):String;
var
//Link : String;
psl : IShellLink;
ppf : IPersistFile;
WidePath : Array[0..260] of WideChar;
Info : Array[0..MAX_PATH] of Char;
wfs : TWin32FindData;
begin
if UpperCase(ExtractFileExt(LinkFileName)) <> '.LNK' Then
begin
Result:='NOT a shortuct by extension!';
Exit;
end;

CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, psl);
if psl.QueryInterface(IPersistFile, ppf) = 0 Then
Begin
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PAnsiChar(LinkFileName), -1, @WidePath, MAX_PATH);
ppf.Load(WidePath, STGM_READ);
psl.GetPath((@info), MAX_PATH, wfs, SLGP_UNCPRIORITY);
Result := info;

end
else
Result := '';
end;


谢谢

最佳答案

据我所知,ppf.Load应该能够直接将您的LinkFileName转换为PChar(现在为PWideChar)。删除MultiByteToWideChar行并使用PChar(LinkFileName)而不是复制到临时变量即可。

这将使代码如下所示:

function TForm1.GetTarget(const LinkFileName:String):String;
var
//Link : String;
psl : IShellLink;
ppf : IPersistFile;
//WidePath : Array[0..260] of WideChar;
Info : Array[0..MAX_PATH] of Char;
wfs : TWin32FindData;
begin
if UpperCase(ExtractFileExt(LinkFileName)) <> '.LNK' Then
begin
Result:='NOT a shortuct by extension!';
Exit;
end;

CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, psl);
if psl.QueryInterface(IPersistFile, ppf) = 0 Then
Begin
ppf.Load(PChar(LinkFileName), STGM_READ);
psl.GetPath((@info), MAX_PATH, wfs, SLGP_UNCPRIORITY);
Result := info;
end
else
Result := '';
end;


psl.GetPath在MSDN中被声明为使用LPTSTR,因此我相信您应该在不更改该部分的情况下获得Unicode版本。

关于delphi - 将函数转换为delphi 2010(unicode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1797423/

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