用Shell获取windows文件的详细信息

static bool GetInfoFromWindows(wchar_t* szFilePath)
{
	IShellFolder * psfDesktop = NULL;//用来定位某个文件夹,并对此文件夹下的文件和文件夹进行操作
	IShellFolder2* psf2Desktop = NULL;//从IShellFolder借口继承,提供了一些新的功能(即扩展属性,如作者)。
	//Shell的基础是桌面,桌面可看做任何文件夹的父类
	if(NOERROR != SHGetDesktopFolder(&psfDesktop))
		return false;
	psfDesktop->QueryInterface(IID_IShellFolder2,(void**)&psf2Desktop);
	psfDesktop->Release();


	LPITEMIDLIST pTargetPathID;//ITEMIDLIST,每个文件或文件夹都维护自己的ItemIDList,里面记录了它们的所有属性。
	                           //一个文件夹下的文件会形成有序的链表,即EnumIDList,对这个链表进行遍历即可找到所有的文件信息。
	IShellFolder2* psf2Folder = NULL;
	//定位到文件所在文件夹
	int iFolderLength = _tcsrchr(szFilePath,_T('\\')) != NULL ? _tcsrchr(szFilePath,_T('\\')) - szFilePath : -1;
	if(iFolderLength <= 0)
		return false;
	wchar_t szFolder[128] = _T("");
	_tcsncpy(szFolder,szFilePath,iFolderLength);
 	if(S_OK != psfDesktop->ParseDisplayName(NULL,NULL,szFolder,NULL,&pTargetPathID,NULL))
 		return false;
	
 	HRESULT hr2;
 	if(S_OK != (hr2 = psf2Desktop->BindToObject(pTargetPathID,NULL,IID_IShellFolder2,(void**)&psf2Folder)))
 	    return false;
	/*LPCITEMIDLIST pidlRelative = NULL;
	if(S_OK == SHGetFolderLocation (NULL, CSIDL_MYMUSIC,NULL,NULL, &pTargetPathID))
	{
		if(S_OK !=  SHBindToParent(pTargetPathID,IID_IShellFolder,(void**)&psf2Folder,&pidlRelative))
			return false;
	}*/
	
// 	if ((SUCCEEDED (SHGetDesktopFolder(&pDesktop))) &&
// 		(SUCCEEDED (SHGetSpecialFolderLocation (m_hWnd, CSIDL_BITBUCKET, &pidlRecycleBin))))
// 	{
// 		if (SUCCEEDED (pDesktop->BindToObject(pidlRecycleBin, NULL, IID_IShellFolder2, (LPVOID *)&m_pFolder2)))
// 		{
// 			
// 		}
// 	}


	LPENUMIDLIST pEnum;
	if(S_OK != psf2Folder->EnumObjects(NULL,SHCONTF_NONFOLDERS,&pEnum))
		return false;
	
	STRRET retFile;
	wchar_t szFilename[128] = _T("");
	LPITEMIDLIST pFileItemID;
	ULONG uEleFetched = 0;
	while(pEnum->Next(1,&pFileItemID,&uEleFetched) == S_OK)
	{
		ZeroMemory(szFilename,128);
		//按照完成文件名格式获得文件名
		psf2Folder->GetDisplayNameOf(pFileItemID,SHGDN_FORPARSING,&retFile);
		StrRetToBuf(&retFile,pFileItemID,szFilename,128);
		if(0 == _tcsicmp(szFilename,szFilePath))
			break;
	}


	//get title,column 21
	//::CoInitialize(NULL);
	SHELLDETAILS shDetail;
	wprintf(L"中文\n");
	for(int i=0;i<10000;++i)
	{
		HRESULT hr = psf2Folder->GetDetailsOf(pFileItemID,i,&shDetail);
		if(hr == S_OK)
		{
			wchar_t szContent[128];
			ZeroMemory(szContent,128);
			StrRetToBuf(&(shDetail.str),pFileItemID,szContent,128);
			_tprintf(_T("xxxx %d : %ls\n"), i,szContent);
		}
	}
	//CoUninitialize();
	return true;
}


你可能感兴趣的:(VC++神奇理论,windows,shell,null,扩展)