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;
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) {
HRESULT(WINAPI * gpfSHOpenFolderAndSelectItems)(LPCITEMIDLIST * pidlFolder, UINT cidl, LPCITEMIDLIST * apidl, DWORD dwFlags);
HINSTANCE ghShell32;
ghShell32 = LoadLibrary(_T("Shell32.DLL"));
if(ghShell32 == NULL) {
return FALSE;
}
gpfSHOpenFolderAndSelectItems = (HRESULT(WINAPI *)(LPCITEMIDLIST *, UINT, LPCITEMIDLIST *, DWORD))
GetProcAddress(ghShell32, "SHOpenFolderAndSelectItems");
if(gpfSHOpenFolderAndSelectItems != NULL) {
if(SUCCEEDED(gpfSHOpenFolderAndSelectItems((LPCITEMIDLIST *) pidlFolder, 0, (LPCITEMIDLIST *) NULL, 0))) {
FreeLibrary(ghShell32);
return TRUE;
}
FreeLibrary(ghShell32);
return FALSE;
}
FreeLibrary(ghShell32);
return FALSE;
}
void FindTarget(LPCTSTR str) {
HRESULT hres;
IShellLink* psl;
ITEMIDLIST* pidl;
IPersistFile* ppf;
CoInitialize(NULL);
printf("11111111, err=%d/n", GetLastError());
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl);
if(SUCCEEDED(hres)) {
printf("222222222, err=%d/n", GetLastError());
psl-> SetPath(str);
printf("666666666 , err=%d/n", GetLastError());
psl-> GetIDList(&pidl);
hres = psl-> QueryInterface(IID_IPersistFile, (void * *) &ppf);
if(SUCCEEDED(hres)) {
printf("3333333333, err=%d/n", GetLastError());
WCHAR wsz[MAX_PATH];
#ifdef _UNICODE
wcscpy(wsz, str);
#else
MultiByteToWideChar(CP_ACP, 0, str, -1, wsz, MAX_PATH);
#endif
// Load the shortcut.
hres = ppf-> Load(wsz, STGM_READ);
if(SUCCEEDED(hres)) {
printf("444444444, err=%d/n", GetLastError());
psl-> GetIDList(&pidl);
}
ppf-> Release();
}
printf("55555555555, err=%d/n", GetLastError());
XZSHOpenFolderAndSelectItems(pidl);
psl-> Release();
}
CoUninitialize();
}
调用FindTarget(LPCTSTR str)函数即可打开输入路径文件夹,选中目标文件。