www.pudn.com > FTP¿Í»§¶Ë³ÌÐòµÄÔ´³ÌÐò.zip > FolderPointer.h


// FolderPointer.h : Declaration of the CFolderPointer 
// Copyright © 1999 Ziff-Davis, Inc. 
// Written by Ignacio Alvarez 
 
#ifndef __FOLDERPOINTER_H_ 
#define __FOLDERPOINTER_H_ 
 
#include "resource.h"       // main symbols 
#include "FPView.h" 
#include "SubFolder.h" 
 
///////////////////////////////////////////////////////////////////////////// 
// CFolderPointer 
class ATL_NO_VTABLE CFolderPointer :  
	public CComObjectRootEx, 
	public CComCoClass, 
	public IFolderPointer, 
	public IShellFolder, 
	public IPersistFolder 
{ 
public: 
	CFolderPointer() 
	{ 
		Folder = NULL; 
		FolderPIDL = NULL; 
		SHGetMalloc(&ShellMalloc); 
	} 
 
	~CFolderPointer() 
	{ 
		ShellMalloc->Free(FolderPIDL); 
		ShellMalloc->Release(); 
		if(Folder) 
			Folder->Release(); 
	} 
 
 
DECLARE_REGISTRY_RESOURCEID(IDR_FOLDERPOINTER) 
DECLARE_NOT_AGGREGATABLE(CFolderPointer) 
 
DECLARE_PROTECT_FINAL_CONSTRUCT() 
 
BEGIN_COM_MAP(CFolderPointer) 
	COM_INTERFACE_ENTRY(IFolderPointer) 
	COM_INTERFACE_ENTRY(IShellFolder) 
	COM_INTERFACE_ENTRY(IPersistFolder) 
END_COM_MAP() 
 
// IFolderPointer 
private: 
	LPSHELLFOLDER Folder; 
	LPITEMIDLIST FolderPIDL; 
	LPMALLOC ShellMalloc; 
 
public: 
 
	STDMETHOD(ParseDisplayName) ( HWND hwndOwner, 
		LPBC pbcReserved, LPOLESTR lpszDisplayName, 
		ULONG * pchEaten, LPITEMIDLIST * ppidl, ULONG *pdwAttributes) 
	{ 
		if(Folder) // Calls the ParseDisplayName method of the target folder 
			return Folder->ParseDisplayName(hwndOwner, pbcReserved, lpszDisplayName, pchEaten, ppidl, pdwAttributes); 
		ATLTRACENOTIMPL(_T("ParseDisplayName")); 
	}; 
 
	STDMETHOD(EnumObjects) (HWND hwndOwner, DWORD grfFlags, LPENUMIDLIST * ppenumIDList) 
	{ 
		*ppenumIDList = NULL; 
		if(Folder) // Calls the EnumObjects method of the target folder 
			return Folder->EnumObjects(hwndOwner, grfFlags, ppenumIDList); 
		ATLTRACENOTIMPL(_T("EnumObjects")); 
	}; 
 
	STDMETHOD(BindToObject) ( LPCITEMIDLIST pidl, LPBC pbcReserved, 
						REFIID riid, LPVOID * ppvOut) 
	{ 
		*ppvOut = NULL; 
		__try 
		{ 
			if(Folder)	// Checks if the IShellFolder interface is valid 
			{ 
				if(FolderPIDL)	// Checks if the PIDL is valid 
					if(IsEqualIID(riid, IID_IShellFolder))	// Checks if the requested interface of the object is IShellFolder 
					{ 
						CComObject  * new_object= new CComObject ; // Creates an instance of CSubFolder 
						LPITEMIDLIST FolderID; 
						int	i = sizeofPIDL(FolderPIDL);							// Gets the size of the parent PIDL 
						int j = sizeofPIDL(pidl);								// Gets the size of the object PIDL 
						FolderID = (LPITEMIDLIST) ShellMalloc->Alloc(i + j - 2);// Allocates enough space for both PIDLs 
						CopyMemory(FolderID, FolderPIDL, i);					// Copies parent PIDL 
						CopyMemory((((LPBYTE)FolderID) + i - 2), pidl, j);		// Appends object PIDL 
						new_object->Initialize(FolderID);						// Initializes the extension passing it the absolute location of the folder 
						ShellMalloc->Free(FolderID);							// Frees the PIDL 
						return new_object->QueryInterface (riid, ppvOut);		// Returns the IShellFolder interface of CSubFolder 
					} 
				return Folder->BindToObject(pidl, pbcReserved, riid, ppvOut); 
			} 
		} 
		__except(1) 
		{ 
		} 
		ATLTRACENOTIMPL(_T("BindToObject")); 
	}; 
 
	STDMETHOD(BindToStorage) ( LPCITEMIDLIST pidl, LPBC pbcReserved, 
						REFIID riid, LPVOID * ppvObj) 
	{ 
		*ppvObj = NULL; 
		if(Folder) // Calls the BindToStorage method of the target folder 
			return Folder->BindToStorage(pidl, pbcReserved, riid, ppvObj); 
		ATLTRACENOTIMPL(_T("BindToStorage")); 
	}; 
 
	STDMETHOD(CompareIDs) ( LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2) 
	{ 
		if(Folder) // Calls the CompareIDs method of the target folder 
			return Folder->CompareIDs(lParam, pidl1, pidl2); 
		ATLTRACENOTIMPL(_T("CompareIDs")); 
	}; 
 
	STDMETHOD(CreateViewObject) ( HWND hwndOwner, REFIID riid, LPVOID * ppvOut) 
	{ 
		if(Folder) // Calls the CreateViewObject method of the target folder 
			return Folder->CreateViewObject(hwndOwner, riid, ppvOut); 
		CComObject  * new_view = new CComObject ;	// Creates an instance of the CFPView class 
		return new_view->QueryInterface (riid, ppvOut);				// Returns its IShellView interface 
	}; 
 
	STDMETHOD(GetAttributesOf) ( UINT cidl, LPCITEMIDLIST * apidl, 
								ULONG * rgfInOut) 
	{ 
		if(Folder) // Calls the GetAttributesOf method of the target folder 
			return Folder->GetAttributesOf(cidl, apidl, rgfInOut); 
		ATLTRACENOTIMPL(_T("GetAttributesOf")); 
	}; 
 
	STDMETHOD(GetUIObjectOf) ( HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl, 
					REFIID riid, UINT * prgfInOut, LPVOID * ppvOut) 
	{ 
		*ppvOut = NULL; 
		if(Folder) // Calls the GetUIObjectOf method of the target folder 
			return Folder->GetUIObjectOf(hwndOwner, cidl, apidl, riid, prgfInOut, ppvOut); 
		ATLTRACENOTIMPL(_T("GetUIObjectOf")); 
	}; 
 
	STDMETHOD(GetDisplayNameOf) ( LPCITEMIDLIST pidl, DWORD uFlags, LPSTRRET lpName) 
	{ 
		if(Folder) // Calls the GetDisplayNameOf method of the target folder 
			return Folder->GetDisplayNameOf(pidl, uFlags, lpName); 
		ATLTRACENOTIMPL(_T("GetDisplayNameOf")); 
	}; 
 
	STDMETHOD(SetNameOf) ( HWND hwndOwner, LPCITEMIDLIST pidl, 
					LPCOLESTR lpszName, DWORD uFlags, 
					LPITEMIDLIST * ppidlOut) 
	{ 
		if(Folder) // Calls the SetNameOf method of the target folder 
			return Folder->SetNameOf(hwndOwner, pidl, lpszName, uFlags, ppidlOut); 
		ATLTRACENOTIMPL(_T("SetNameOf")); 
	}; 
 
	STDMETHOD(GetClassID)(LPCLSID lpClassID) 
	{ 
		ATLTRACENOTIMPL(_T("GetClassID")); 
	}; 
 
	// Implementation of IPersistFolder::Initialize() 
	// This function is always called by the shell before accessing a Folder Pointer 
	// Here, the IShellFolder interface of the target folder is retrieved 
	STDMETHOD(Initialize)(LPCITEMIDLIST pidl) 
	{ 
		LPSHELLFOLDER Desktop; 
		LPITEMIDLIST FolderID; 
		char Path[MAX_PATH]; 
		WCHAR Shortcut[MAX_PATH]; 
		IShellLink* psl; 
		LPPERSISTFILE ppf; 
		__try 
		{ 
			BOOL Visible = FALSE; 
			EnumWindows(EnumWProc, (long)&Visible); 
			if(Visible)	// If there is a visible BaseBar class window 
			{			// Procceed 
				SHGetDesktopFolder(&Desktop); 
				SHGetPathFromIDList(pidl, Path);	// Gets the folder location 
				strcat(Path, "\\Folder.lnk"); 
				MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, Path, strlen(Path) + 1, Shortcut, MAX_PATH); 
				CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl); 
				psl->QueryInterface(IID_IPersistFile, (void**)&ppf); 
				ppf->Load(Shortcut, STGM_READ);		// Loads the shortcut hidden inside the folder 
				psl->GetIDList(&FolderID);			// Gets the PIDL of the target folder from the shortcut 
 
				int i = sizeofPIDL(FolderID);						// Gets the size of the PIDL 
				FolderPIDL = (LPITEMIDLIST) ShellMalloc->Alloc(i);	// Allocates enough memory to copy the PIDL 
				CopyMemory(FolderPIDL, FolderID, i);				// Copies the target PIDL to the variable FolderPIDL 
 
				if(Desktop->BindToObject(FolderID, NULL, IID_IShellFolder, (void**)&Folder)) // Retrieves the IShellFolder interface of the target folder and stores it in the Folder variable 
					Folder = Desktop;				// If it failed, it assigns the IShellFolder of the Desktop to the Folder variable 
				else 
					Desktop->Release(); 
				ppf->Release(); 
				psl->Release(); 
				ShellMalloc->Free(FolderID); 
			} 
		} 
		__except(1) 
		{ 
			Folder = NULL; 
		} 
		return NOERROR; 
	}; 
 
 
 
}; 
 
 
 
#endif //__FOLDERPOINTER_H_