哎,我写的也挺烦人的,不知道对不对,好像有个地方错了,就是如何将一个string转换为一个pwidechar的问题,就这个地方,我也没时间来改了,哪个高手改一下?
uses activex,comobj,shlobj;function GetLinkFileName(filename:string):string;
var
hr:hresult;
psl:IShelllink;
szdescription,szgetpath:array[0..max_path] of char;
wfd:win32_find_data;
ppf:IPersistFile;
lpw:pwidechar;
buf:pwidechar;
begin
result:='';
buf:=nil;
hr:=cocreateinstance(clsid_shelllink,nil,clsctx_inproc_server,IID_IShellLinkA,psl);
if (succeeded(hr)) then
begin
hr:=psl.QueryInterface(iPersistFile,ppf);
if succeeded(hr) then
begin
lpw:=stringtowidechar(filename,buf,255);
hr := ppf.Load(lpw, STGM_READ);
if succeeded(hr) then
begin
hr := psl.Resolve(0, SLR_NO_UI);
if succeeded(hr) then
begin
hr := psl.GetPath(szGetPath,MAX_PATH, wfd,SLGP_SHORTPATH );
if succeeded(hr) then
result:=szgetpath;
end;
end;
ppf._Release;
end;
end;
end;
uses activex,comobj,shlobj;function GetLinkFileName(filename:string):string;
var
hr:hresult;
psl:IShelllink;
szdescription,szgetpath:array[0..max_path] of char;
wfd:win32_find_data;
ppf:IPersistFile;
lpw:pwidechar;
buf:pwidechar;
begin
result:='';
buf:=nil;
hr:=cocreateinstance(clsid_shelllink,nil,clsctx_inproc_server,IID_IShellLinkA,psl);
if (succeeded(hr)) then
begin
hr:=psl.QueryInterface(iPersistFile,ppf);
if succeeded(hr) then
begin
lpw:=stringtowidechar(filename,buf,255);
hr := ppf.Load(lpw, STGM_READ);
if succeeded(hr) then
begin
hr := psl.Resolve(0, SLR_NO_UI);
if succeeded(hr) then
begin
hr := psl.GetPath(szGetPath,MAX_PATH, wfd,SLGP_SHORTPATH );
if succeeded(hr) then
result:=szgetpath;
end;
end;
ppf._Release;
end;
end;
end;
shellexecute(handle,'open','start.exe','c:\windows\desktop\aaa.lnk','',sw_hide);
哎,我自己修改好了,两个问题都搞定了,不过好像有内存泄漏...嘻嘻,那个_Release不注释掉但是又会有问题,烦哪!算了。收工!哦,对了,对于第二个问题,还可以在取得Filename之后使用Winexec('Filename',sW_show)来运行!
调用方法:
var
buf:pchar;
.........
try
getmem(buf,Max_path);
if GetLinkFilename('c:\windows\desktop\delphi~1.lnk',buf) then
showmessage(buf);
finally
freemem(buf);
end;
////////////////////////////////////////////////////
function GetLinkFileName(filename:string;var aa:pchar):boolean;
var
hr:hresult;
psl:IShelllink;
// szgetpath:array[0..max_path] of char;
wfd:win32_find_data;
ppf:IPersistFile;
lpw:pwidechar;
buf:pwidechar;
begin
result:=false;
getmem(buf,1024);
try
hr:=cocreateinstance(clsid_shelllink,nil,clsctx_inproc_server,IID_IShellLinkA,psl);
if (succeeded(hr)) then
begin
hr:=psl.QueryInterface(iPersistFile,ppf);
if succeeded(hr) then
begin
lpw:=stringtowidechar(filename,buf,255);
hr := ppf.Load(lpw, STGM_READ);
if succeeded(hr) then
begin
hr := psl.Resolve(0, SLR_NO_UI);
if succeeded(hr) then
begin
hr := psl.GetPath(aa,MAX_PATH, wfd,SLGP_SHORTPATH );
if succeeded(hr) then
result:=true;
// ppf._Release;
end;
// ppf._Release;
end;
end;
end;
finally
freemem(buf);
end;
end;
cocreateinstance(clsid_shelllink,nil,clsctx_inproc_server,IID_IShellLinkA,psl);
就出错了,请你指点一下,不胜感激!
hr := ppf.Load(lpw, STGM_READ);
也不行