读书人

用SHGetFileInfo乱码了解决方案

发布时间: 2013-12-15 22:17:18 作者: rapoo

用SHGetFileInfo乱码了

type LINK_FILE_INFO=record
FileName:array[0..MAX_PATH] of Char; //目标文件名
WorkDirectory:array[0..MAX_PATH] of char; //工作目录或者起始目录
IconLocation:array[0..MAX_PATH] of char; //图标文件名
IconIndex:integer; //图标索引
Arguments:array[0..MAX_PATH] of char; //程序运行的参数
Description:array[0..CCH_MAXNAME] of char; //快捷方式的描述
RelativePath:array[0..255] of char; //相对目录,只能设置
ShowState:integer; //运行时的窗口状态
HotKey:word; //快捷键
end;
function LinkFileInfo(const lnkFileName:string;var info:LINK_FILE_INFO;const bSet:boolean):boolean;

var

hr:hresult;

psl:IShelllink;

wfd:win32_find_data;

ppf:IPersistFile;

lpw:pwidechar;

buf:pwidechar;

begin

result:=false;

getmem(buf,MAX_PATH);

try

if SUCCEEDED(CoInitialize(nil)) then

if (succeeded(cocreateinstance(clsid_shelllink,nil,clsctx_inproc_server,IID_IShellLinkA,psl))) then

begin

hr:=psl.QueryInterface(iPersistFile,ppf);

if succeeded(hr) then

begin

lpw:=stringtowidechar(lnkfilename,buf,MAX_PATH);

hr := ppf.Load(lpw, STGM_READ);

if succeeded(hr) then

begin

hr := psl.Resolve(0, SLR_NO_UI);

if succeeded(hr) then

begin

if bSet then

begin

psl.SetArguments(info.Arguments);

psl.SetDescription(info.Description);

psl.SetHotkey(info.HotKey);

psl.SetIconLocation(info.IconLocation,info.IconIndex);

psl.SetPath(info.FileName);

psl.SetShowCmd(info.ShowState);

psl.SetRelativePath(info.RelativePath,0);

psl.SetWorkingDirectory(info.WorkDirectory);

result:=succeeded(psl.Resolve(0,SLR_UPDATE));

end

else

begin

psl.GetPath(info.FileName,MAX_PATH, wfd,SLGP_SHORTPATH );

psl.GetIconLocation(info.IconLocation,MAX_PATH,info.IconIndex);

psl.GetWorkingDirectory(info.WorkDirectory,MAX_PATH);

psl.GetDescription(info.Description,CCH_MAXNAME);

psl.GetArguments(info.Arguments,MAX_PATH);

psl.GetHotkey(info.HotKey);

psl.GetShowCmd(info.ShowState);

result:=true;

end;

end;

end;


end;

end;

finally

freemem(buf);

end;

end;



运行函数获取快捷方式信息,showmessage文件名乱码
[解决办法]
字符编码的问题吧。双字节与单字节。
[解决办法]
注意字符串编码格式,SHGetFileInfo 和 SHGetFileInfoW 分别是用的 Ansi 和 unicode 字符串。
不要混用了。
[解决办法]
编码不一样吧……

读书人网 >.NET

热点推荐