Misc.cpp
上传用户:hy_wanghao
上传日期:2007-01-08
资源大小:279k
文件大小:9k
- #include "stdafx.h"
- #include "misc.h"
- ////////////////////////////////////////////
- // PIDL helper functions
- // Extracts the complete path from a complex PIDL list.
- // The root "/" is prepended to the final path.
- void PidlGetFullPath(LPTSTR pstrPath, LPCITEMIDLIST pidl, LPCTSTR cSep/*="/"*/)
- {
- ATLASSERT(!::IsBadWritePtr(pstrPath,MAX_PATH));
- ATLASSERT(cSep);
- // Create the full path from the PIDL list
- _tcscpy( pstrPath, cSep );
- while( (pidl!=NULL) && (pidl->mkid.cb!=0) ) {
- ATLASSERT(((LPPIDLDATA)pidl)->tag==PIDL_TAG);
- _tcscat( pstrPath, ((LPPIDLDATA)pidl)->szName );
- pidl = CPidl::_GetNextItem(pidl);
- if( pidl->mkid.cb!=0 ) _tcscat( pstrPath, cSep );
- };
- }
- // Extracts the path from a complex PIDL list, leaving out
- // the last entry (which is assumed to be the filename).
- // The root "/" is prepended to the final path.
- void PidlGetFilePath(LPTSTR pstrPath, LPCITEMIDLIST pidl, LPCTSTR cSep/*="/"*/)
- {
- ATLASSERT(!::IsBadWritePtr(pstrPath,MAX_PATH));
- ATLASSERT(cSep);
- UINT nCount = CPidl::_GetCount(pidl);
- _tcscpy( pstrPath, cSep );
- if( nCount==0 ) return;
- for( UINT i=0; i<nCount-1; i++ ) {
- ATLASSERT(((LPPIDLDATA)pidl)->tag==PIDL_TAG);
- _tcscat( pstrPath, ((LPPIDLDATA)pidl)->szName );
- pidl = CPidl::_GetNextItem(pidl);
- if( pidl->mkid.cb!=0 ) _tcscat( pstrPath, cSep );
- }
- }
- // Extracts the Name from a complex PIDL list.
- void PidlGetName(LPTSTR pstrName, LPCITEMIDLIST pidl)
- {
- ATLASSERT(!::IsBadWritePtr(pstrName,MAXNAMELEN));
- pidl = CPidl::_GetLastItem(pidl);
- if( (pidl!=NULL) && (pidl->mkid.cb!=0) ) {
- ATLASSERT(((LPPIDLDATA)pidl)->tag==PIDL_TAG);
- _tcscpy( pstrName, ((LPPIDLDATA)pidl)->szName );
- }
- else {
- pstrName[0] = _T(' ');
- }
- }
- // Returns the PIDLTYPE type of the last PIDL entry
- PIDLTYPE PidlGetType(LPCITEMIDLIST pidl)
- {
- if( pidl==NULL ) return PT_UNKNOWN;
- LPPIDLDATA pData = (LPPIDLDATA)CPidl::_GetLastItem(pidl);
- ATLASSERT(pData->tag==PIDL_TAG);
- return pData->type;
- }
- ////////////////////////////////////////////
- // Misc helper functions
- // Open and prepare an Amiga device and volume.
- // If a path is supplied (in the 'pidl' argument) the folder will be changed
- // right away.
- // NOTE:
- // Currently this implementation will always get the first partition if more
- // than one is found on the device.
- HRESULT OpenAmigaDevice(LPCTSTR pstrDevPath,
- BOOL bReadOnly,
- LPCITEMIDLIST pidlStartFolder,
- CAdfDevice &dev,
- CAdfVolume &vol)
- {
- ATLASSERT(!::IsBadStringPtr(pstrDevPath, MAX_PATH));
- ATLASSERT(!dev.IsOpen());
- ATLASSERT(!vol.IsOpen());
- // Open the Device
- // HACK: If we're started from the MyComputer CLSID, then this is a native Amiga device.
- // The shell reports a path like "::{CLSID_DRIVES}{CLSID_Drive}".
- // On WinNT 4.0, ::SHGetPathFromIDList() fails and the string is empty.
-
- // Floppies and hard-files read from this partition.
- // BUG: Hmm! Need to handle multiple partitions
- DWORD nPartition = 0;
-
- // Do we need to initialize the native drive path?
- if( (*pstrDevPath==_T(' ')) ||
- (_tcsncmp(pstrDevPath, _T("::"), 2)==0) ) {
- static TCHAR s_szDeviceName[MAX_PATH] = { 0 };
- static DWORD s_nPartition = 0;
- if( s_szDeviceName[0]==_T(' ') ) {
- // Set the default device and partition
- _tcscpy( s_szDeviceName, _T("|H1") );
- s_nPartition = 0;
- // Open registry and find device name and partition
- OLECHAR szCLSID[64];
- ::StringFromGUID2(CLSID_Drive, szCLSID, lengthof(szCLSID));
- TCHAR szSubKey[80];
- LONG lResult;
- ::wsprintf(szSubKey, _T("CLSID\%ls"), szCLSID);
- CRegKey reg;
- lResult = reg.Open(HKEY_CLASSES_ROOT, szSubKey);
- if( lResult==ERROR_SUCCESS ) {
- DWORD dwCount = MAX_PATH;
- reg.QueryValue(s_szDeviceName, _T("DeviceName"), &dwCount);
- reg.QueryValue(s_nPartition, _T("Partition"));
- }
- }
- pstrDevPath = s_szDeviceName;
- nPartition = s_nPartition;
- }
- // Open Amiga device
- if( dev.Open(pstrDevPath)==FALSE ) return E_INVALIDARG;
- if( dev.GetPartitionCount() >= nPartition ) nPartition = 0;
- // Then mount the volume...
- if( dev.Mount(nPartition, bReadOnly, vol)==FALSE ) return E_ACCESSDENIED;
- // If a path is supplied, change folder right away...
- if( pidlStartFolder!=NULL ) {
- TCHAR szPath[MAX_PATH];
- PidlGetFullPath(szPath, pidlStartFolder);
- if( vol.ChangeDirectory(szPath)==FALSE ) return E_FAIL;
- }
- return S_OK;
- }
- // Initializes our ImageList with default images.
- // Because almost all Windows versions have different system icons, we
- // need to query for icons like "folder", "binary file"...
- BOOL CreateImageLists()
- {
- // Set the small image list
- int nSmallCx = ::GetSystemMetrics(SM_CXSMICON);
- int nSmallCy = ::GetSystemMetrics(SM_CYSMICON);
- HIMAGELIST imgSmall = ImageList_Create(nSmallCx, nSmallCy, ILC_COLORDDB | ILC_MASK, 4, 0);
- if( imgSmall==NULL ) return FALSE;
- // Set the large image list
- int nLargeCx = ::GetSystemMetrics(SM_CXICON);
- int nLargeCy = ::GetSystemMetrics(SM_CYICON);
- HIMAGELIST imgLarge = ImageList_Create(nLargeCx, nLargeCy, ILC_COLORDDB | ILC_MASK, 4, 0);
- if( imgLarge==NULL ) return FALSE;
- HICON hIcon;
- SHFILEINFO sfi;
- TCHAR szPath[MAX_PATH];
- // Root icon
- hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(),
- MAKEINTRESOURCE(IDI_APP),
- IMAGE_ICON,
- nSmallCx, nSmallCy,
- LR_DEFAULTCOLOR);
- ImageList_AddIcon(imgSmall, hIcon);
- hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(),
- MAKEINTRESOURCE(IDI_APP),
- IMAGE_ICON,
- nLargeCx, nLargeCy,
- LR_DEFAULTCOLOR);
- ImageList_AddIcon(imgLarge, hIcon);
- // Folder icon (get the icon for path "C:Windows")...
- ::GetWindowsDirectory(szPath, lengthof(szPath));
- hIcon = (HICON)::SHGetFileInfo( szPath, 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SMALLICON );
- ImageList_AddIcon(imgSmall, sfi.hIcon);
- hIcon = (HICON)::SHGetFileInfo( szPath, 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE );
- ImageList_AddIcon(imgLarge, sfi.hIcon);
- // also the open folder icon
- hIcon = (HICON)::SHGetFileInfo( szPath, 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_OPENICON | SHGFI_SMALLICON );
- ImageList_AddIcon(imgSmall, sfi.hIcon);
- hIcon = (HICON)::SHGetFileInfo( szPath, 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_OPENICON );
- ImageList_AddIcon(imgLarge, sfi.hIcon);
- // File icon (use the icon for this DLL)
- ::GetModuleFileName(_Module.GetModuleInstance(), szPath, lengthof(szPath));
- hIcon = (HICON)::SHGetFileInfo( szPath, 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE | SHGFI_SMALLICON );
- ImageList_AddIcon(imgSmall, sfi.hIcon);
- hIcon = (HICON)::SHGetFileInfo( szPath, 0, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_SHELLICONSIZE );
- ImageList_AddIcon(imgLarge, sfi.hIcon);
- _Module.m_ImageLists.m_hImageListSmall = imgSmall;
- _Module.m_ImageLists.m_hImageListLarge = imgLarge;
- return TRUE;
- }
- ////////////////////////////////////////////
- // Debug functions
- #ifdef _DEBUG
- // A DEBUG function which returns the name of a CLIPFORMAT.
- // Windows sends a lot of really weird formats to inform the
- // IDataObject about various state changes.
- LPTSTR DbgGetCF(CLIPFORMAT cf)
- {
- static TCHAR szName[128];
- *szName = _T(' ');
- ::GetClipboardFormatName(cf, szName, 128);
- return szName;
- }
- // A DEBUG function which returns the item name of the
- // PIDL structure.
- // The PIDL must be of PIDLDATA type.
- LPTSTR DbgGetPidlPath(LPCITEMIDLIST pidl)
- {
- static TCHAR szName[MAX_PATH];
- if( pidl==NULL ) return _T("<null>");
- PidlGetFullPath(szName, pidl);
- return szName;
- }
- // A DEBUG function which returns the CLSID or interface name
- // for the IID argument.
- LPTSTR DbgGetIID(REFIID iid)
- {
- USES_CONVERSION;
- static TCHAR szName[64];
- OLECHAR szGUID[64];
- ::StringFromGUID2(iid, szGUID, lengthof(szGUID));
- // Attempt to find it in the interfaces section
- CRegKey key;
- DWORD dwType, dw = sizeof(szName);
- key.Open(HKEY_CLASSES_ROOT, _T("Interface"), KEY_READ);
- if( key.Open(key, OLE2T(szGUID), KEY_READ) == S_OK ) {
- *szName = _T(' ');
- ::RegQueryValueEx(key.m_hKey, NULL, NULL, &dwType, (LPBYTE)szName, &dw);
- return szName;
- }
- // Attempt to find it in the clsid section
- key.Open(HKEY_CLASSES_ROOT, _T("CLSID"), KEY_READ);
- if( key.Open(key, OLE2T(szGUID), KEY_READ) == S_OK ) {
- *szName = _T(' ');
- ::RegQueryValueEx(key.m_hKey, NULL, NULL, &dwType, (LPBYTE)szName, &dw);
- return szName;
- }
- _tcscpy(szName, OLE2CT(szGUID));
- return szName;
- }
- #endif