ショートカット(.lnk)のリンク先の取得について

ActiveBasicでのプログラミングでわからないこと、困ったことなどがあったら、ここで質問してみましょう(質問を行う場合は、過去ログやWeb上であらかじめ問題を整理するようにしましょう☆)。
返信する
メッセージ
作成者
かえる

ショートカット(.lnk)のリンク先の取得について

#1 投稿記事 by かえる »

今回で二回目の質問になります、かえると申します。

そのようなAPI等があるのでしょうか?
また、ない場合はどのような方法を使えばよいのでしょうか?

おかしな質問で申し訳ないのですが、よろしくお願い致します。
hira
記事: 203
登録日時: 2005年5月31日(火) 20:14
お住まい: 兵庫県
連絡する:

Re: ショートカット(.lnk)のリンク先の取得について

#2 投稿記事 by hira »

実は、AB過去ログ「BackSearchAB」にあります(^^;;
「2169-ショートカット」スレのイグトランスさんの投稿を一部変更して掲載します。
※COMコンポーネントを使うので、少々ややこしいかもしれません

コード: 全て選択

Const STGM_READ      = 0
Const STGM_WRITE     = 1
Const STGM_READWRITE = 2

Enum SLGP_FLAGS
    SLGP_SHORTPATH      = &h0001,
    SLGP_UNCPRIORITY    = &h0002,
    SLGP_RAWPATH        = &h0004,
End Enum

Class IShellLink
	Inherits IUnknown
Public
	Virtual Function GetPath(pszFile As BytePtr, cchMaxPath As Long, pfd As *WIN32_FIND_DATA, fFlags As DWord) As DWord

	Virtual Function GetIDList(ByRef ppidl As VoidPtr) As DWord
	Virtual Function SetIDList(pidl As VoidPtr) As DWord

	Virtual Function GetDescription(pszName As BytePtr, cchMaxName As Long) As DWord
	Virtual Function SetDescription(pszName As BytePtr) As DWord

	Virtual Function GetWorkingDirectory(pszDir As BytePtr, cchMaxPath As Long) As DWord
	Virtual Function SetWorkingDirectory(pszDir As BytePtr) As DWord

	Virtual Function GetArguments(pszArgs As BytePtr, cchMaxPath As Long) As DWord
	Virtual Function SetArguments(pszArgs As BytePtr) As DWord

	Virtual Function GetHotkey(ByRef pwHotkey As Word) As DWord
	Virtual Function SetHotkey(ByVal wHotkey As Word) As DWord

	Virtual Function GetShowCmd(ByRef piShowCmd As Long) As DWord
	Virtual Function SetShowCmd(ByVal iShowCmd As Long) As DWord

	Virtual Function GetIconLocation(pszIconPath As BytePtr, cchIconPath As Long, ByRef piIcon As Long) As DWord
	Virtual Function SetIconLocation(pszIconPath As BytePtr, iIcon As Long) As DWord

	Virtual Function SetRelativePath(pszPathRel As BytePtr, dwReserved As DWord) As DWord

	Virtual Function Resolve(hwnd As DWord, fFlags As DWord) As DWord

	Virtual Function SetPath(pszFile As BytePtr) As DWord
End Class

Dim CLSID_ShellLink = [
	&h00021401,
	0,
	0,
	[&hC0, 0, 0, 0, 0, 0, 0, &h46]
]As GUID

Dim IID_IShellLink = [
	&h000214EE,
	0,
	0,
	[&hC0, 0, 0, 0, 0, 0, 0, &h46]
]As GUID

Class IPersist
	Inherits IUnknown
Public
	Virtual Function GetClassID(pClassID As VoidPtr) As DWord
End Class

Class IPersistFile
	Inherits IPersist
Public
	Virtual Function IsDirty() As DWord
	Virtual Function Load(pszFileName As WordPtr, dwMode As DWord) As DWord
	Virtual Function Save(pszFileName As WordPtr, fRemember As DWord) As DWord
	Virtual Function SaveCompleted(pszFileName As WordPtr) As DWord
	Virtual Function GetCurFile(ByRef ppszFileName As WordPtr) As DWord
End Class

Dim IID_IPersistFile = [
	&h0000010b,
	0,
	0,
	[&hC0, 0, 0, 0, 0, 0, 0, &h46]
]As GUID

Dim pszLink As BytePtr
pszLink = "C:\WINDOWS\スタート メニュー\プログラム\アクセサリ\メモ帳.lnk" 'ショートカットのファイル名
Dim szPath[MAX_PATH] As Byte

Dim psl As *IShellLink

CoInitialize(NULL)

' IShellLink オブジェクトを作成しポインタを取得する
If CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, VarPtr(psl)) = S_OK Then
	Dim ppf As *IPersistFile

	' IPersistFile インターフェイスの取得
	If psl->QueryInterface(VarPtr(IID_IPersistFile), VarPtr(ppf)) = S_OK Then
		Dim wsz[MAX_PATH] As Word
		MultiByteToWideChar(CP_ACP, 0, pszLink, -1, wsz, MAX_PATH)

		' ショートカットのロード
		If ppf->Load(wsz, STGM_READ) = S_OK Then
			' リンク先を取得する
			' ここでpsl->Get~を呼ぶといろいろなショートカットファイルの情報が得られます。
			psl->GetPath(szPath, MAX_PATH, NULL, SLGP_UNCPRIORITY)

			MessageBox(NULL, szPath, "", MB_OK)
		End If

		ppf->Release()
	End If
	psl->Release()
End If

CoUninitialize()
かえる

Re: ショートカット(.lnk)のリンク先の取得について

#3 投稿記事 by かえる »

hiraさん返信ありがとうございます。
「BackSearchAB」にあったとは…ろくに調べもせずすみませんでした。
ショートカットの情報取得だけでここまですごい事になるんですね。

本当にありがとうございました。
返信する