得到Window快捷方式的具体路径

来源:互联网 发布:黑马程序员19期 编辑:程序博客网 时间:2024/05/20 17:10

Uses ActiveX, ShlObj;

function GetLinkFile(const LnkFileName: String; var FileName: String): Boolean;
var
    hr: HRESULT;
    psl: IShelllink;
    wfd: WIN32_FIND_DATA;
    ppf: IPersistFile;
    lpw: pWideChar;
    buf: pWideChar;
    InternalFileName: Array[0..MAX_PATH] of Char;
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
                        psl.GetPath(InternalFileName, MAX_PATH, wfd, SLGP_SHORTPATH);
                        FileName := InternalFileName;
                        Result := True;
                    end;
                end;
            end;
        end;
    finally
        FreeMem(buf);
    end;
end;

原创粉丝点击