最近寫一個(gè)開(kāi)發(fā)輔助工具,在這個(gè)過(guò)程要做一個(gè)類似文件快捷方式中查找目標(biāo)的功能,先查MSDN98,大家不要見(jiàn)笑,我現(xiàn)在一直都用它,沒(méi)有相應(yīng)的API,后又. Net 2003中的MSDN,找到了可以實(shí)現(xiàn)該功能的API,SHOpenFolderAndSelectItems()函數(shù),它的原型如下:
(具體用法參考MSDN)“Opens a Microsoft? Windows? Explorer window with specified items in a particular folder selected.”
HRESULT SHOpenFolderAndSelectItems(
???? LPCITEMIDLIST pidlFolder,
???? UINT cidl,
???? LPCITEMIDLIST *apidl,
???? DWORD dwFlags
); 但是,它需要Windows XP及上,若在Win2000或Win98如何實(shí)現(xiàn)它呢?于是我就上網(wǎng)搜索,幾經(jīng)周折最終搜到的一篇文章,但它只是利用工具通過(guò)反匯編Windows API函數(shù)得到的代碼,可能可以實(shí)現(xiàn)與快捷方式相同的對(duì)話框(我沒(méi)有試過(guò)),但其代碼可讀性非常差,我只能參考一下大概的流程,他提到一個(gè)非常重要的一點(diǎn),那就是使用一個(gè)未公開(kāi)的API函數(shù)SHGetIDispatchForFolder,它可幫助我打開(kāi)文件夾。好不多說(shuō)了,下面是關(guān)鍵的部分:
查找目標(biāo)功能,分為兩個(gè)步驟,首先打開(kāi)或找到目標(biāo)文件所在的文件夾,其次在打開(kāi)的文件夾中選中相應(yīng)的項(xiàng)目(即文件)。在說(shuō)這個(gè)步驟之前,先認(rèn)識(shí)一下,下面兩個(gè)結(jié)構(gòu)
typedef struct _SHITEMID {
???? USHORT cb;
???? BYTE??? abID[1];
} SHITEMID, * LPSHITEMID;
typedef const SHITEMID?? * LPCSHITEMID;
typedef struct _ITEMIDLIST {
???? SHITEMID mkid;
} ITEMIDLIST, * LPITEMIDLIST;
typedef const ITEMIDLIST * LPCITEMIDLIST;
這兩個(gè)結(jié)構(gòu)的數(shù)據(jù)保存的是項(xiàng)目定義符列表(僅是字面翻譯),這個(gè)結(jié)構(gòu)所表示的文件夾及文件除了正常的,還包括一些特殊的文件夾及文件(如目錄,我的電腦等),SHGetIDispatchForFolder函數(shù)正是用它的做為參數(shù),可以打開(kāi)一些特殊的文件夾。SHGetIDispatchForFolder函數(shù)的原型是 :??????? HRESULT (WINAPI*gpfSHGetIDispatchForFolder)(ITEMIDLIST* pidl, IWebBrowserApp** ppIWebBrowserApp); 通常快捷方式給我的ITEMIDLIST是包含文件名的,若直接調(diào)用上面的函數(shù),它將直接會(huì)打開(kāi)出目標(biāo)文件,而不是打開(kāi)文件夾。下面是區(qū)分文件及文件夾的代碼:
?pIdlFile = pidl;??????
?/// 找出目標(biāo)文件中文件名的偏移量
?while (cb = pIdlFile->mkid.cb)
?{
?? pidl2 = pIdlFile;
?? pIdlFile = (ITEMIDLIST*)((BYTE*)pIdlFile + cb);
?}
?cb = pidl2->mkid.cb;
?pidl2->mkid.cb = 0;
下面是打開(kāi)文件夾及選中文件的代碼,相信大家不難理解。???? /// 打開(kāi)目標(biāo)文件所在的文件夾
?if (SUCCEEDED(GetShellFolderViewDual(pidl, &pIShellFolderViewDual)))
?{
?? pidl2->mkid.cb = cb;
?? // 0 Deselect the item.
?? // 1 Select the item.
?? // 3 Put the item in edit mode.
?? // 4 Deselect all but the specified item.
?? // 8 Ensure the item is displayed in the view.
?? // 0x10 Give the item the focus.
?? COleVariant bszFile(pidl2);
??????
?? if(pIShellFolderViewDual != NULL)
?? {
??? /// 選中相應(yīng)的選項(xiàng)
??? pIShellFolderViewDual->SelectItem(bszFile, 0x1d);
??? pIShellFolderViewDual->Release();
?? }
?? return TRUE;
?} 源代碼中包含了一個(gè)DEMO。下面是完整的函數(shù),可以直接調(diào)用FindTarget(CString str)參數(shù)為文件名,若是快捷方式則會(huì)自動(dòng)指向其目標(biāo)。若代碼中已做過(guò)COM的初始化工作,請(qǐng)刪除CoInitialize(NULL);及CoUninitialize();語(yǔ)句。
HRESULT GetShellFolderViewDual(ITEMIDLIST* pidl, IShellFolderViewDual** ppIShellFolderViewDual)
{
?IWebBrowserApp* pIWebBrowserApp;
?IDispatch* pDoc;
?HWND hWnd;
?HRESULT hr;
?HINSTANCE ghSHDOCVW;
?HRESULT (WINAPI*gpfSHGetIDispatchForFolder)(ITEMIDLIST* pidl, IWebBrowserApp** ppIWebBrowserApp);
?*ppIShellFolderViewDual = NULL;
?ghSHDOCVW = LoadLibrary(_T("SHDOCVW.DLL"));
?if (ghSHDOCVW == NULL)
?? return FALSE;
?pIWebBrowserApp=NULL;
?gpfSHGetIDispatchForFolder =
?? (HRESULT (WINAPI*)(ITEMIDLIST*, IWebBrowserApp**)) GetProcAddress(ghSHDOCVW, "SHGetIDispatchForFolder");
?if (gpfSHGetIDispatchForFolder == NULL)
?? return FALSE;
?/// 調(diào)用未公開(kāi)的API函數(shù) SHGetIDispatchForFolder
?if (SUCCEEDED(gpfSHGetIDispatchForFolder(pidl, &pIWebBrowserApp)))
?{
?? if (SUCCEEDED(pIWebBrowserApp->get_HWND((long*)&hWnd)))
?? {
??? SetForegroundWindow(hWnd);
??? ShowWindow(hWnd, SW_SHOWNORMAL);
?? }
?? if (SUCCEEDED(hr = pIWebBrowserApp->get_Document(&pDoc)))
?? {
??? pDoc->QueryInterface(IID_IShellFolderViewDual, (void**) ppIShellFolderViewDual);
??? pDoc->Release();
?? }
?? pIWebBrowserApp->Release();
?}
?FreeLibrary(ghSHDOCVW);
?return TRUE;
}
BOOL XZSHOpenFolderAndSelectItems(ITEMIDLIST *pidlFolder)
{
?ITEMIDLIST *pidl, *pidl2;
?ITEMIDLIST* pIdlFile;
?USHORT cb;
?IShellFolderViewDual* pIShellFolderViewDual;
?HRESULT (WINAPI *gpfSHOpenFolderAndSelectItems)(LPCITEMIDLIST *pidlFolder, UINT cidl, LPCITEMIDLIST *apidl, DWORD dwFlags);
?HINSTANCE ghShell32;
/// 只有WinXp及以上及系統(tǒng)才支持SHOpenFolderAndSelectItems() API
/// 那其它系統(tǒng)該怎么實(shí)現(xiàn)這個(gè)功能呢?只能采用其它的方法來(lái)處理
/// 首先用XP跟蹤到SHOpenFolderAndSelectItems()API中,看它是如何處理的,再用同樣的方法去實(shí)現(xiàn)
/// 其它系統(tǒng)的這個(gè)功能使用工具 VC6 .net 2003 MSDN Ollydbg v1.10中文版
?ghShell32 = LoadLibrary(_T("Shell32.DLL"));
?if (ghShell32 == NULL)
?? return FALSE;
?gpfSHOpenFolderAndSelectItems =
(HRESULT (WINAPI*)(LPCITEMIDLIST*, UINT, LPCITEMIDLIST*, DWORD)) GetProcAddress(ghShell32, "SHOpenFolderAndSelectItems");
?if (gpfSHOpenFolderAndSelectItems != NULL)
?{
?? /// 可以獲得SHOpenFolderAndSelectItems()函數(shù)的API地址
?? if (SUCCEEDED(gpfSHOpenFolderAndSelectItems((LPCITEMIDLIST*)pidlFolder,0,(LPCITEMIDLIST*)NULL,0)))
?? {
??? ///直接調(diào)用系統(tǒng)的功能
??? FreeLibrary(ghShell32);
??? return TRUE;
?? }
?? FreeLibrary(ghShell32);
?? return FALSE;
?}
?FreeLibrary(ghShell32);
?/// 當(dāng)操作系統(tǒng)不支持SHOpenFolderAndSelectItems()函數(shù)的API時(shí)的處理,
?/// 自已動(dòng)手寫一個(gè)與系統(tǒng)功能相同的代碼
?pidl = pidlFolder;
?pIdlFile = pidl;
?/// 找出目標(biāo)文件中文件名的偏移量
?while (cb = pIdlFile->mkid.cb)
?{
?? pidl2 = pIdlFile;
?? pIdlFile = (ITEMIDLIST*)((BYTE*)pIdlFile + cb);
?}
?cb = pidl2->mkid.cb;
?pidl2->mkid.cb = 0;
?/// 打開(kāi)目標(biāo)文件所在的文件夾
?if (SUCCEEDED(GetShellFolderViewDual(pidl, &pIShellFolderViewDual)))
?{
?? pidl2->mkid.cb = cb;
?? // 0 Deselect the item.??
?? // 1 Select the item.??
?? // 3 Put the item in edit mode.??
?? // 4 Deselect all but the specified item.??
?? // 8 Ensure the item is displayed in the view.??
?? // 0x10 Give the item the focus.??
?? COleVariant bszFile(pidl2);
?? if(pIShellFolderViewDual != NULL)
?? {
??? /// 選中相應(yīng)的選項(xiàng)
??? pIShellFolderViewDual->SelectItem(bszFile, 0x1d);
??? pIShellFolderViewDual->Release();
?? }
?? return TRUE;
?}
?return FALSE;
}
void FindTarget(CString str)
{
?HRESULT hres;
?IShellLink *psl;
?ITEMIDLIST *pidl;
?IPersistFile *ppf;
CoInitialize(NULL);
?// Get a pointer to the IShellLink interface.
?hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
??????? IID_IShellLink, (LPVOID*)&psl);
?if (SUCCEEDED(hres))
?{
?? // 設(shè)置目標(biāo)文件
?? psl->SetPath(str);
?? /// 獲得目標(biāo)文件的ITEMIDLIST
?? psl->GetIDList(&pidl);
?? // Get a pointer to the IPersistFile interface.
?? hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
?? if (SUCCEEDED(hres))
?? {
??? WCHAR wsz[MAX_PATH];
#ifdef _UNICODE
??? wcscpy(wsz, str);
#else
??? // Ensure that the string is Unicode.
??? MultiByteToWideChar(CP_ACP, 0, str, -1, wsz, MAX_PATH);
#endif
?
??? // Load the shortcut.
??? hres = ppf->Load(wsz, STGM_READ);
??? if (SUCCEEDED(hres))
??? {
???? /// 獲得快捷方式的ITEMIDLIST
???? psl->GetIDList(&pidl);
??? }m
??? ppf->Release();
?? }
?? /// 打開(kāi)文件夾并選中項(xiàng)目
?? XZSHOpenFolderAndSelectItems(pidl);
?? psl->Release();
?}
?CoUninitialize();
}
在VC6下編譯后的代碼,通過(guò)98,2k,XP的測(cè)試。
?
本篇文章來(lái)源于:開(kāi)發(fā)學(xué)院 http://edu.codepub.com?? 原文鏈接:http://edu.codepub.com/2009/0808/12691.php