一个windows shell extension的例子
时间:2010-10-05 来源:wochi
下面的例子演示了windows shell扩展编程的一般方法———获取目录c:\program files\下的对象,然后再列出改目录下的第一个子目录中的对象。
#include "stdafx.h" #include <Shlobj.h> #include <Shlwapi.h>
int _tmain(int argc, _TCHAR* argv[]) { LPITEMIDLIST pidlProgFiles = NULL; LPITEMIDLIST pidlItems = NULL; IShellFolder *psfFirstFolder = NULL; IShellFolder *psfDeskTop = NULL; IShellFolder *psfProgFiles = NULL; LPENUMIDLIST ppenum = NULL; ULONG celtFetched; HRESULT hr; STRRET strDispName; TCHAR pszDisplayName[MAX_PATH]; ULONG uAttr; CoInitialize( NULL ); hr = SHGetFolderLocation(NULL, CSIDL_PROGRAM_FILES, NULL, NULL, &pidlProgFiles);
// hr = SHGetFolderLocation(NULL, CSIDL_DESKTOP, NULL, NULL, &pidlProgFiles);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->BindToObject(pidlProgFiles, NULL, IID_IShellFolder, (LPVOID *) &psfProgFiles); psfDeskTop->Release();
hr = psfProgFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1) { HRESULT ret = psfProgFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName); if(FAILED(ret))continue; ret = StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH); if(FAILED(ret))continue; MessageBox(NULL,pszDisplayName,_T("1111"),0); wprintf(_T("%s\n"),pszDisplayName); if(!psfFirstFolder) { uAttr = SFGAO_FOLDER; psfProgFiles->GetAttributesOf(1, (LPCITEMIDLIST *) &pidlItems, &uAttr); if(uAttr & SFGAO_FOLDER) { hr = psfProgFiles->BindToObject(pidlItems, NULL, IID_IShellFolder, (LPVOID *) &psfFirstFolder); } } CoTaskMemFree(pidlItems); }
ppenum->Release();
if(psfFirstFolder) { hr = psfFirstFolder->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1) { psfFirstFolder->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName); StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH); wprintf(_T("%s\n"),pszDisplayName); CoTaskMemFree(pidlItems); } }
ppenum->Release(); CoTaskMemFree(pidlProgFiles); psfProgFiles->Release(); psfFirstFolder->Release();
CoUninitialize(); return 0; }
在vs2008中配置SDK 7.0:
#include "stdafx.h" #include <Shlobj.h> #include <Shlwapi.h>
int _tmain(int argc, _TCHAR* argv[]) { LPITEMIDLIST pidlProgFiles = NULL; LPITEMIDLIST pidlItems = NULL; IShellFolder *psfFirstFolder = NULL; IShellFolder *psfDeskTop = NULL; IShellFolder *psfProgFiles = NULL; LPENUMIDLIST ppenum = NULL; ULONG celtFetched; HRESULT hr; STRRET strDispName; TCHAR pszDisplayName[MAX_PATH]; ULONG uAttr; CoInitialize( NULL ); hr = SHGetFolderLocation(NULL, CSIDL_PROGRAM_FILES, NULL, NULL, &pidlProgFiles);
// hr = SHGetFolderLocation(NULL, CSIDL_DESKTOP, NULL, NULL, &pidlProgFiles);
hr = SHGetDesktopFolder(&psfDeskTop);
hr = psfDeskTop->BindToObject(pidlProgFiles, NULL, IID_IShellFolder, (LPVOID *) &psfProgFiles); psfDeskTop->Release();
hr = psfProgFiles->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1) { HRESULT ret = psfProgFiles->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName); if(FAILED(ret))continue; ret = StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH); if(FAILED(ret))continue; MessageBox(NULL,pszDisplayName,_T("1111"),0); wprintf(_T("%s\n"),pszDisplayName); if(!psfFirstFolder) { uAttr = SFGAO_FOLDER; psfProgFiles->GetAttributesOf(1, (LPCITEMIDLIST *) &pidlItems, &uAttr); if(uAttr & SFGAO_FOLDER) { hr = psfProgFiles->BindToObject(pidlItems, NULL, IID_IShellFolder, (LPVOID *) &psfFirstFolder); } } CoTaskMemFree(pidlItems); }
ppenum->Release();
if(psfFirstFolder) { hr = psfFirstFolder->EnumObjects(NULL,SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &ppenum);
while( hr = ppenum->Next(1,&pidlItems, &celtFetched) == S_OK && (celtFetched) == 1) { psfFirstFolder->GetDisplayNameOf(pidlItems, SHGDN_INFOLDER, &strDispName); StrRetToBuf(&strDispName, pidlItems, pszDisplayName, MAX_PATH); wprintf(_T("%s\n"),pszDisplayName); CoTaskMemFree(pidlItems); } }
ppenum->Release(); CoTaskMemFree(pidlProgFiles); psfProgFiles->Release(); psfFirstFolder->Release();
CoUninitialize(); return 0; }
在vs2008中配置SDK 7.0:
开始> 所有程序 > Microsoft Windows SDK v7.0 > Visual Studio Registration > Windows SDK Configuration Tool.
然后在下拉框选择 SDK v7
相关阅读 更多 +