Q&A

  • Lnk파일의 실행파일명을 알아낼려면
윈도우의 단축아이콘에서 아이콘이 가리키는 실행파일명을

얻어내는 방법을 알려주세요.

SHGetFileInfo 를 쓰는 것인지...

3  COMMENTS
  • Profile
    김영대 1999.07.30 00:42
    유시니 께서 말씀하시기를...

    > 윈도우의 단축아이콘에서 아이콘이 가리키는 실행파일명을

    > 얻어내는 방법을 알려주세요.

    > SHGetFileInfo 를 쓰는 것인지...



    unit Unit1;



    interface



    uses

    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    StdCtrls, Registry, ShlObj,

    {$IFDEF VER90}Ole2,{$ENDIF}{$IFDEF VER100}ComObj, ActiveX,{$ENDIF} CommCtrl;



    type

    TForm1 = class(TForm)

    Button1: TButton;

    Memo1: TMemo;

    procedure Button1Click(Sender: TObject);

    private

    { Private declarations }

    public

    { Public declarations }

    end;



    type

    PShellLinkInfoStruct = ^TShellLinkInfoStruct;

    TShellLinkInfoStruct = record

    FullPathAndNameOfLinkFile: array[0..MAX_PATH] of char;

    FullPathAndNameOfFileToExecute: array[0..MAX_PATH] of char;

    ParamStringsOfFileToExecute: array[0..MAX_PATH] of char;

    FullPathAndNameOfWorkingDirectroy: array[0..MAX_PATH] of char;

    Description: array[0..MAX_PATH] of char;

    FullPathAndNameOfFileContiningIcon: array[0..MAX_PATH] of char;

    IconIndex: integer;

    HotKey: word;

    ShowCommand: integer;

    FindData: TWIN32FINDDATA;

    end;



    var

    Form1: TForm1;



    implementation

    {$R *.DFM}



    procedure GetLinkInfo(lpShellLinkInfoStruct: PShellLinkInfoStruct);

    var

    ShellLink: IShellLink;

    PersistFile: IPersistFile;

    {$IFDEF VER90}

    WidePath: array[0..MAX_PATH] of WideChar;

    {$ENDIF}

    {$IFDEF VER100}

    MyObject: IUnknown;

    {$ENDIF}

    begin

    {$IFDEF VER90}

    CoInitialize(nil);

    if Failed(CoCreateInstance(CLSID_ShellLink,

    nil,

    CLSCTX_INPROC_SERVER,

    {$IFDEF VER90}

    IID_IShellLink,

    {$ENDIF}

    {$IFDEF VER100}

    IID_IShellLinkA,

    {$ENDIF}

    ShellLink)) then

    begin



    CoUninitialize;

    Exit;

    end;



    if Failed(ShellLink.QueryInterface(IID_IPersistFile,

    PersistFile)) then

    begin

    ShellLink.Release;

    CoUninitialize;

    Exit;

    end;



    MultiByteToWideChar(CP_ACP,

    0,

    lpShellLinkInfoStruct^.FullPathAndNameOfLinkFile,

    -1,

    WidePath,

    MAX_PATH + 1);

    if Failed(PersistFile.Load(WidePath, 0)) then

    begin

    PersistFile.Release;

    ShellLink.Release;

    CoUninitialize;

    Exit;

    end;



    ShellLink.GetPath(

    lpShellLinkInfoStruct^.FullPathAndNameOfFileToExecute,

    sizeof(lpShellLinkInfoStruct^.FullPathAndNameOfLinkFile),

    lpShellLinkInfoStruct^.FindData,

    SLGP_UNCPRIORITY);

    ShellLink.GetDescription(

    lpShellLinkInfoStruct^.Description,

    sizeof(lpShellLinkInfoStruct^.Description));

    ShellLink.GetArguments(

    lpShellLinkInfoStruct^.ParamStringsOfFileToExecute,

    sizeof(lpShellLinkInfoStruct^.ParamStringsOfFileToExecute));

    ShellLink.GetWorkingDirectory(

    lpShellLinkInfoStruct^.FullPathAndNameOfWorkingDirectroy,

    sizeof(lpShellLinkInfoStruct^.FullPathAndNameOfWorkingDirectroy));

    ShellLink.GetIconLocation(

    lpShellLinkInfoStruct^.FullPathAndNameOfFileContiningIcon,

    sizeof(lpShellLinkInfoStruct^.FullPathAndNameOfFileContiningIcon),

    lpShellLinkInfoStruct^.IconIndex);

    ShellLink.GetHotKey(lpShellLinkInfoStruct^.HotKey);

    ShellLink.GetShowCmd(lpShellLinkInfoStruct^.ShowCommand);

    PersistFile.Release;

    ShellLink.Release;

    CoUninitialize;

    {$ENDIF}

    {$IFDEF VER100}

    MyObject := CreateComObject(CLSID_ShellLink);

    ShellLink := MyObject as IShellLink;

    PersistFile := MyObject as IPersistFile;

    PersistFile.Load(

    PWChar(

    WideString(lpShellLinkInfoStruct^.FullPathAndNameOfLinkFile)),

    0);

    ShellLink.GetPath(

    lpShellLinkInfoStruct^.FullPathAndNameOfFileToExecute,

    sizeof(lpShellLinkInfoStruct^.FullPathAndNameOfLinkFile),

    lpShellLinkInfoStruct^.FindData,

    SLGP_UNCPRIORITY);

    ShellLink.GetDescription(

    lpShellLinkInfoStruct^.Description,

    sizeof(lpShellLinkInfoStruct^.Description));

    ShellLink.GetArguments(

    lpShellLinkInfoStruct^.ParamStringsOfFileToExecute,

    sizeof(lpShellLinkInfoStruct^.ParamStringsOfFileToExecute));

    ShellLink.GetWorkingDirectory(

    lpShellLinkInfoStruct^.FullPathAndNameOfWorkingDirectroy,

    sizeof(lpShellLinkInfoStruct^.FullPathAndNameOfWorkingDirectroy));

    ShellLink.GetIconLocation(

    lpShellLinkInfoStruct^.FullPathAndNameOfFileContiningIcon,

    sizeof(lpShellLinkInfoStruct^.FullPathAndNameOfFileContiningIcon),

    lpShellLinkInfoStruct^.IconIndex);

    ShellLink.GetHotKey(lpShellLinkInfoStruct^.HotKey);

    ShellLink.GetShowCmd(lpShellLinkInfoStruct^.ShowCommand);

    {$ENDIF}

    end;



    procedure TForm1.Button1Click(Sender: TObject);

    var

    LinkInfo: TShellLinkInfoStruct;

    SFolder: pItemIDList;

    SpecialPath: array[0..MAX_PATH] of Char;

    begin

    // 바탕화면 폴더의 실제 디렉토리명을 구해온다

    SHGetSpecialFolderLocation(Form1.Handle, CSIDL_DESKTOP, SFolder);

    SHGetPathFromIDList(SFolder, SpecialPath);

    StrCat(SpecialPath, '탐색기.lnk'); // *.lnk 파일을 지정



    FillChar(LinkInfo, sizeof(LinkInfo), #0);

    StrCopy(LinkInfo.FullPathAndNameOfLinkFile, SpecialPath);

    GetLinkInfo(@LinkInfo);

    Memo1.Clear;

    Memo1.Lines.Add(LinkInfo.FullPathAndNameOfLinkFile);

    Memo1.Lines.Add(LinkInfo.FullPathAndNameOfFileToExecute);

    Memo1.Lines.Add(LinkInfo.ParamStringsOfFileToExecute);

    Memo1.Lines.Add(LinkInfo.FullPathAndNameOfWorkingDirectroy);

    Memo1.Lines.Add(LinkInfo.Description);

    Memo1.Lines.Add(LinkInfo.FullPathAndNameOfFileContiningIcon);

    Memo1.Lines.Add(IntToStr(LinkInfo.IconIndex));

    Memo1.Lines.Add(IntToStr(LoByte(LinkInfo.HotKey)));

    Memo1.Lines.Add(IntToStr(HiByte(LinkInfo.HotKey)));

    Memo1.Lines.Add(IntToStr(LinkInfo.ShowCommand));

    Memo1.Lines.Add(LinkInfo.FindData.cFileName);

    Memo1.Lines.Add(LinkInfo.FindData.cAlternateFileName);

    end;



    end.



  • Profile
    유시니 1999.08.06 11:45
    김영대 께서 말씀하시기를...

    > 유시니 께서 말씀하시기를...

    > > 윈도우의 단축아이콘에서 아이콘이 가리키는 실행파일명을

    > > 얻어내는 방법을 알려주세요.

    > > SHGetFileInfo 를 쓰는 것인지...

    >

    ... 너무길어서 생략...

    델파이4 버전에서는 에러가 납니다.

    그러나 델파이3 버전에서는 에러가 않나네요...

  • Profile
    유시니 1999.07.30 01:47
    김영대 께서 말씀하시기를...

    > 유시니 께서 말씀하시기를...

    > > 윈도우의 단축아이콘에서 아이콘이 가리키는 실행파일명을

    > > 얻어내는 방법을 알려주세요.

    > > SHGetFileInfo 를 쓰는 것인지...

    >



    답변 고맙습니다. 그런데 아래의 ========>>PersistFile: IPersistFile;

    에서 컴파일시 에러가 발생합니다. 어떻게 해야하는지...

    uses절에 뭔가를 추가해야 할것 같은데...



    --------------------------------------------------------------------

    implementation

    {$R *.DFM}



    procedure GetLinkInfo(lpShellLinkInfoStruct: PShellLinkInfoStruct);

    var

    ShellLink: IShellLink;

    =========>> PersistFile: IPersistFile;

    {$IFDEF VER90}

    WidePath: array[0..MAX_PATH] of WideChar;

    {$ENDIF}

    {$IFDEF VER100}

    MyObject: IUnknown;

    {$ENDIF}