Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

785 lines
38 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. MPC_utils.h
  5. Abstract:
  6. This file contains the declaration of various utility functions/classes.
  7. Revision History:
  8. Davide Massarenti (Dmassare) 05/09/99
  9. created
  10. ******************************************************************************/
  11. #if !defined(__INCLUDED___MPC___UTILS_H___)
  12. #define __INCLUDED___MPC___UTILS_H___
  13. #include <MPC_main.h>
  14. #include <MPC_COM.h>
  15. #include <fci.h>
  16. #include <fdi.h>
  17. #include <objidl.h>
  18. #undef CopyFile
  19. #undef MoveFile
  20. #undef DeleteFile
  21. namespace MPC
  22. {
  23. // Forward declarations.
  24. class Serializer;
  25. class CComHGLOBAL;
  26. ////////////////////
  27. inline int StrCmp( const MPC::string& left, const MPC::string& right ) { return strcmp( left.c_str(), right.c_str() ); }
  28. inline int StrCmp( const MPC::string& left, LPCSTR right ) { return strcmp( left.c_str(), right ? right : "" ); }
  29. inline int StrCmp( LPCSTR left, const MPC::string& right ) { return strcmp( left ? left : "" , right.c_str() ); }
  30. inline int StrCmp( LPCSTR left, LPCSTR right ) { return strcmp( left ? left : "" , right ? right : "" ); }
  31. inline int StrCmp( const MPC::wstring& left, const MPC::wstring& right ) { return wcscmp( left.c_str(), right.c_str() ); }
  32. inline int StrCmp( const MPC::wstring& left, LPCWSTR right ) { return wcscmp( left.c_str(), right ? right : L"" ); }
  33. inline int StrCmp( LPCWSTR left, const MPC::wstring& right ) { return wcscmp( left ? left : L"" , right.c_str() ); }
  34. inline int StrCmp( LPCWSTR left, LPCWSTR right ) { return wcscmp( left ? left : L"" , right ? right : L"" ); }
  35. ////////////////////
  36. inline int StrICmp( const MPC::string& left, const MPC::string& right ) { return _stricmp( left.c_str(), right.c_str() ); }
  37. inline int StrICmp( const MPC::string& left, LPCSTR right ) { return _stricmp( left.c_str(), right ? right : "" ); }
  38. inline int StrICmp( LPCSTR left, const MPC::string& right ) { return _stricmp( left ? left : "" , right.c_str() ); }
  39. inline int StrICmp( LPCSTR left, LPCSTR right ) { return _stricmp( left ? left : "" , right ? right : "" ); }
  40. inline int StrICmp( const MPC::wstring& left, const MPC::wstring& right ) { return _wcsicmp( left.c_str(), right.c_str() ); }
  41. inline int StrICmp( const MPC::wstring& left, LPCWSTR right ) { return _wcsicmp( left.c_str(), right ? right : L"" ); }
  42. inline int StrICmp( LPCWSTR left, const MPC::wstring& right ) { return _wcsicmp( left ? left : L"" , right.c_str() ); }
  43. inline int StrICmp( LPCWSTR left, LPCWSTR right ) { return _wcsicmp( left ? left : L"" , right ? right : L"" ); }
  44. //////////////////////////////////////////////////////////////////////
  45. HRESULT LocalizeInit( LPCWSTR szFile = NULL );
  46. HRESULT LocalizeString( UINT uID, LPSTR lpBuf, int nBufMax, bool fMUI = false );
  47. HRESULT LocalizeString( UINT uID, LPWSTR lpBuf, int nBufMax, bool fMUI = false );
  48. HRESULT LocalizeString( UINT uID, MPC::string& szStr , bool fMUI = false );
  49. HRESULT LocalizeString( UINT uID, MPC::wstring& szStr , bool fMUI = false );
  50. HRESULT LocalizeString( UINT uID, CComBSTR& bstrStr , bool fMUI = false );
  51. int LocalizedMessageBox ( UINT uID_Title, UINT uID_Msg, UINT uType );
  52. int LocalizedMessageBoxFmt( UINT uID_Title, UINT uID_Msg, UINT uType, ... );
  53. //////////////////////////////////////////////////////////////////////
  54. void RemoveTrailingBackslash( /*[in/out]*/ LPWSTR szPath );
  55. HRESULT GetProgramDirectory ( /*[out]*/ MPC::wstring& szPath );
  56. HRESULT GetUserWritablePath ( /*[out]*/ MPC::wstring& szPath, /*[in]*/ LPCWSTR szSubDir = NULL );
  57. HRESULT GetCanonialPathName ( /*[out]*/ MPC::wstring& szPathNameOut, /*[in]*/ LPCWSTR szPathNameIn );
  58. HRESULT GetTemporaryFileName( /*[out]*/ MPC::wstring& szFile, /*[in]*/ LPCWSTR szBase = NULL, /*[in]*/ LPCWSTR szPrefix = NULL );
  59. HRESULT RemoveTemporaryFile ( /*[in/out]*/ MPC::wstring& szFile );
  60. HRESULT SubstituteEnvVariables( /*[in/out]*/ MPC::wstring& szStr );
  61. //////////////////////////////////////////////////
  62. int HexToNum( int c );
  63. char NumToHex( int c );
  64. DATE GetSystemTime();
  65. DATE GetLocalTime ();
  66. DATE GetSystemTimeEx( /*[in]*/ bool fHighPrecision );
  67. DATE GetLocalTimeEx ( /*[in]*/ bool fHighPrecision );
  68. DATE GetLastModifiedDate( /*[out]*/ const MPC::wstring& strFile );
  69. HRESULT ConvertSizeUnit( /*[in]*/ const MPC::wstring& szStr, /*[out]*/ DWORD& dwRes );
  70. HRESULT ConvertTimeUnit( /*[in]*/ const MPC::wstring& szStr, /*[out]*/ DWORD& dwRes );
  71. HRESULT ConvertDateToString( /*[in] */ DATE dDate ,
  72. /*[out]*/ MPC::wstring& szDate ,
  73. /*[in] */ bool fGMT ,
  74. /*[in] */ bool fCIM ,
  75. /*[in] */ LCID lcid );
  76. HRESULT ConvertStringToDate( /*[in] */ const MPC::wstring& szDate ,
  77. /*[out]*/ DATE& dDate ,
  78. /*[in] */ bool fGMT ,
  79. /*[in] */ bool fCIM ,
  80. /*[in] */ LCID lcid );
  81. HRESULT ConvertStringToHex( /*[in]*/ const CComBSTR& bstrText, /*[out]*/ CComBSTR& bstrHex );
  82. HRESULT ConvertHexToString( /*[in]*/ const CComBSTR& bstrHex , /*[out]*/ CComBSTR& bstrText );
  83. HRESULT ConvertHGlobalToHex( /*[in]*/ HGLOBAL hg , /*[out]*/ CComBSTR& bstrHex, /*[in]*/ bool fNullAllowed = false , DWORD* pdwCount = NULL );
  84. HRESULT ConvertHexToHGlobal( /*[in]*/ const CComBSTR& bstrText, /*[out]*/ HGLOBAL& hg , /*[in]*/ bool fNullAllowed = false );
  85. HRESULT ConvertBufferToVariant( /*[in]*/ const BYTE* pBuf, /*[in]*/ DWORD dwLen, /*[out]*/ CComVariant& v );
  86. HRESULT ConvertVariantToBuffer( /*[in]*/ const VARIANT* v , /*[out]*/ BYTE*& pBuf, /*[out]*/ DWORD& dwLen );
  87. HRESULT ConvertIStreamToVariant( /*[in]*/ IStream* stream, /*[out]*/ CComVariant& v );
  88. HRESULT ConvertVariantToIStream( /*[in]*/ const VARIANT* v , /*[out]*/ IStream* *pStream );
  89. HRESULT ConvertListToSafeArray( /*[in]*/ const MPC::WStringList& lst , /*[out]*/ VARIANT& array, /*[in]*/ VARTYPE vt );
  90. HRESULT ConvertSafeArrayToList( /*[in]*/ const VARIANT& array, /*[out]*/ MPC::WStringList& lst );
  91. //////////////////////////////////////////////////
  92. typedef struct
  93. {
  94. LPCWSTR szName;
  95. DWORD dwMask; // Mask of the bit field.
  96. DWORD dwSet; // Bits to set.
  97. DWORD dwReset; // Bits to reset before set.
  98. } StringToBitField;
  99. HRESULT CommandLine_Parse( /*[out]*/ int& argc, /*[out]*/ LPCWSTR*& argv, /*[in]*/ LPWSTR lpCmdLine = NULL, /*[in]*/ bool fBackslashForEscape = false );
  100. void CommandLine_Free ( /*[in ]*/ int& argc, /*[in ]*/ LPCWSTR*& argv );
  101. HRESULT ConvertStringToBitField( /*[in]*/ LPCWSTR szText , /*[out]*/ DWORD& dwBitField, /*[in]*/ const StringToBitField* pLookup, /*[in]*/ bool fUseTilde = false );
  102. HRESULT ConvertBitFieldToString( /*[in]*/ DWORD dwBitField, /*[out]*/ MPC::wstring& szText , /*[in]*/ const StringToBitField* pLookup );
  103. HRESULT SplitAtDelimiter( StringVector& vec, LPCSTR ptr, LPCSTR delims, bool fDelimIsAString = true, bool fSkipAdjacentDelims = false );
  104. HRESULT SplitAtDelimiter( WStringVector& vec, LPCWSTR ptr, LPCWSTR delims, bool fDelimIsAString = true, bool fSkipAdjacentDelims = false );
  105. HRESULT JoinWithDelimiter( const StringVector& vec, MPC::string& ptr, LPCSTR delims );
  106. HRESULT JoinWithDelimiter( const WStringVector& vec, MPC::wstring& ptr, LPCWSTR delims );
  107. //////////////////////////////////////////////////
  108. HRESULT MakeDir ( /*[in]*/ const MPC::wstring& szPath, /*[in]*/ bool fCreateParent = true );
  109. HRESULT GetDiskSpace ( /*[in]*/ const MPC::wstring& szFile , /*[out]*/ ULARGE_INTEGER& liFree, /*[out]*/ ULARGE_INTEGER& liTotal );
  110. HRESULT ExecuteCommand( /*[in]*/ const MPC::wstring& szCommandLine );
  111. HRESULT FailOnLowDiskSpace( /*[in]*/ LPCWSTR szFile, /*[in]*/ DWORD dwLowLevel );
  112. HRESULT FailOnLowMemory ( /*[in]*/ DWORD dwLowLevel );
  113. //////////////////////////////////////////////////
  114. HRESULT GetCallingPidFromRPC ( /*[out]*/ ULONG& pid );
  115. HRESULT GetFileNameFromProcess( /*[in ]*/ HANDLE hProc, /*[out]*/ MPC::wstring& strFile );
  116. HRESULT GetFileNameFromPid ( /*[in ]*/ ULONG pid , /*[out]*/ MPC::wstring& strFile );
  117. HRESULT MapDeviceToDiskLetter( /*[out]*/ MPC::wstring& strDevice, /*[out]*/ MPC::wstring& strDisk );
  118. bool IsCallerInList ( /*[in]*/ const LPCWSTR* rgList, /*[in]*/ const MPC::wstring& strCallerFile );
  119. HRESULT VerifyCallerIsTrusted( /*[in]*/ const LPCWSTR* rgList );
  120. //////////////////////////////////////////////////
  121. class MSITS
  122. {
  123. public:
  124. static bool IsCHM( /*[in]*/ LPCWSTR pwzUrl, /*[out]*/ BSTR* pbstrStorageName = NULL, /*[out]*/ BSTR* pbstrFilePath = NULL );
  125. static HRESULT OpenAsStream( /*[in] */ const CComBSTR& bstrStorageName, /*[in] */ const CComBSTR& bstrFilePath, /*[out]*/ IStream **ppStream );
  126. };
  127. //////////////////////////////////////////////////
  128. class Cabinet
  129. {
  130. public:
  131. class File
  132. {
  133. public:
  134. MPC::wstring m_szFullName;
  135. MPC::wstring m_szName;
  136. bool m_fFound;
  137. DWORD m_dwSizeUncompressed;
  138. DWORD m_dwSizeCompressed;
  139. File()
  140. {
  141. m_fFound = false;
  142. m_dwSizeUncompressed = 0;
  143. m_dwSizeCompressed = 0;
  144. }
  145. };
  146. typedef std::list<File> List;
  147. typedef List::iterator Iter;
  148. typedef List::const_iterator IterConst;
  149. typedef HRESULT (*PFNPROGRESS_FILES)( Cabinet* /*cabinet*/, LPCWSTR /*szFile*/, ULONG /*lDone*/, ULONG /*lTotal*/, LPVOID /*user*/ );
  150. typedef HRESULT (*PFNPROGRESS_BYTES)( Cabinet* /*cabinet*/, ULONG /*lDone*/, ULONG /*lTotal*/, LPVOID /*user*/ );
  151. private:
  152. WCHAR m_szCabinetPath[MAX_PATH];
  153. WCHAR m_szCabinetName[MAX_PATH];
  154. List m_lstFiles;
  155. Iter m_itCurrent;
  156. DWORD m_dwSizeDone;
  157. DWORD m_dwSizeTotal;
  158. HFCI m_hfci;
  159. HFDI m_hfdi;
  160. ERF m_erf;
  161. CCAB m_cab_parameters;
  162. BOOL m_fIgnoreMissingFiles;
  163. LPVOID m_lpUser;
  164. PFNPROGRESS_FILES m_pfnCallback_Files;
  165. PFNPROGRESS_BYTES m_pfnCallback_Bytes;
  166. ////////////////////////////////////////
  167. static LPVOID DIAMONDAPI mem_alloc( ULONG cb );
  168. static void DIAMONDAPI mem_free ( LPVOID memory );
  169. ////////////////////////////////////////
  170. static int DIAMONDAPI fci_delete( LPSTR pszFile, int *err, LPVOID pv );
  171. static INT_PTR DIAMONDAPI fci_open ( LPSTR pszFile, int oflag, int pmode, int *err, LPVOID pv );
  172. static UINT DIAMONDAPI fci_read ( INT_PTR hf, LPVOID memory, UINT cb, int *err, LPVOID pv );
  173. static UINT DIAMONDAPI fci_write ( INT_PTR hf, LPVOID memory, UINT cb, int *err, LPVOID pv );
  174. static int DIAMONDAPI fci_close ( INT_PTR hf, int *err, LPVOID pv );
  175. static long DIAMONDAPI fci_seek ( INT_PTR hf, long dist, int seektype, int *err, LPVOID pv );
  176. static BOOL DIAMONDAPI fci_get_next_cabinet( PCCAB pccab, ULONG cbPrevCab, LPVOID pv );
  177. static int DIAMONDAPI fci_file_placed ( PCCAB pccab, LPSTR pszFile, long cbFile, BOOL fContinuation, LPVOID pv );
  178. static long DIAMONDAPI fci_progress ( UINT typeStatus, ULONG cb1, ULONG cb2, LPVOID pv );
  179. static BOOL DIAMONDAPI fci_get_temp_file ( LPSTR pszTempName, int cbTempName, LPVOID pv );
  180. static INT_PTR DIAMONDAPI fci_get_open_info ( LPSTR pszName, USHORT *pdate, USHORT *ptime, USHORT *pattribs, int *err, LPVOID pv );
  181. ////////////////////////////////////////
  182. static INT_PTR DIAMONDAPI fdi_open ( LPSTR pszFile, int oflag, int pmode );
  183. static UINT DIAMONDAPI fdi_read ( INT_PTR hf, LPVOID pv, UINT cb );
  184. static UINT DIAMONDAPI fdi_write ( INT_PTR hf, LPVOID pv, UINT cb );
  185. static int DIAMONDAPI fdi_close ( INT_PTR hf );
  186. static long DIAMONDAPI fdi_seek ( INT_PTR hf, long dist, int seektype );
  187. static INT_PTR DIAMONDAPI fdi_notification_copy ( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin );
  188. static INT_PTR DIAMONDAPI fdi_notification_enumerate( FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin );
  189. ////////////////////////////////////////
  190. public:
  191. Cabinet();
  192. ~Cabinet();
  193. HRESULT put_CabinetFile ( /*[in]*/ LPCWSTR szVal, /*[in]*/ UINT cbSpaceToReserve = 0 );
  194. HRESULT put_IgnoreMissingFiles( /*[in]*/ BOOL fVal );
  195. HRESULT put_UserData ( /*[in]*/ LPVOID lpVal );
  196. HRESULT put_onProgress_Files ( /*[in]*/ PFNPROGRESS_FILES pfnVal );
  197. HRESULT put_onProgress_Bytes ( /*[in]*/ PFNPROGRESS_BYTES pfnVal );
  198. HRESULT ClearFiles();
  199. HRESULT GetFiles ( /*[out]*/ List& lstFiles );
  200. HRESULT AddFile ( /*[in]*/ LPCWSTR szFileName, /*[in]*/ LPCWSTR szFileNameInsideCabinet = NULL );
  201. HRESULT Compress ();
  202. HRESULT Decompress();
  203. HRESULT Enumerate ();
  204. };
  205. HRESULT CompressAsCabinet ( /*[in]*/ LPCWSTR szInputFile , /*[in]*/ LPCWSTR szCabinetFile, /*[in]*/ LPCWSTR szFileName );
  206. HRESULT CompressAsCabinet ( /*[in]*/ const WStringList& lstFiles , /*[in]*/ LPCWSTR szCabinetFile, /*[in]*/ BOOL fIgnoreMissingFiles = FALSE );
  207. HRESULT ListFilesInCabinet ( /*[in]*/ LPCWSTR szCabinetFile, /*[in]*/ WStringList& lstFiles );
  208. HRESULT DecompressFromCabinet( /*[in]*/ LPCWSTR szCabinetFile, /*[in]*/ LPCWSTR szOutputFile , /*[in]*/ LPCWSTR szFileName );
  209. //////////////////////////////////////////////////
  210. class URL
  211. {
  212. MPC::wstring m_szURL;
  213. URL_COMPONENTSW m_ucURL;
  214. void Clean ();
  215. HRESULT Prepare();
  216. public:
  217. URL();
  218. ~URL();
  219. HRESULT CheckFormat( /*[in]*/ bool fDecode = false );
  220. HRESULT Append( /*[in]*/ const MPC::wstring& szExtra, /*[in]*/ bool fEscape = true );
  221. HRESULT Append( /*[in]*/ LPCWSTR szExtra, /*[in]*/ bool fEscape = true );
  222. HRESULT AppendQueryParameter( /*[in]*/ LPCWSTR szName, /*[in]*/ LPCWSTR szValue );
  223. HRESULT get_URL ( /*[out]*/ MPC::wstring& szURL );
  224. HRESULT put_URL ( /*[in] */ const MPC::wstring& szURL );
  225. HRESULT put_URL ( /*[in] */ LPCWSTR szURL );
  226. HRESULT get_Scheme ( /*[out]*/ MPC::wstring& szVal ) const;
  227. HRESULT get_Scheme ( /*[out]*/ INTERNET_SCHEME& nVal ) const;
  228. HRESULT get_HostName ( /*[out]*/ MPC::wstring& szVal ) const;
  229. HRESULT get_Port ( /*[out]*/ DWORD & dwVal ) const;
  230. HRESULT get_Path ( /*[out]*/ MPC::wstring& szVal ) const;
  231. HRESULT get_ExtraInfo( /*[out]*/ MPC::wstring& szVal ) const;
  232. };
  233. //////////////////////////////////////////////////
  234. void InitCRC ( /*[out] */ DWORD& dwCRC );
  235. void ComputeCRC( /*[in/out]*/ DWORD& dwCRC, /*[in]*/ UCHAR* rgBlock, /*[in]*/ int nLength );
  236. HRESULT ComputeCRC( /*[out] */ DWORD& dwCRC, /*[in]*/ IStream* stream );
  237. HRESULT ComputeCRC( /*[out] */ DWORD& dwCRC, /*[in]*/ LPCWSTR szFile );
  238. //////////////////////////////////////////////////
  239. HRESULT GetBSTR( /*[in ]*/ LPCWSTR bstr, /*[out]*/ BSTR * pVal, /*[in]*/ bool fNullOk = true );
  240. HRESULT PutBSTR( /*[out]*/ CComBSTR& bstr, /*[in ]*/ LPCWSTR newVal, /*[in]*/ bool fNullOk = true );
  241. HRESULT PutBSTR( /*[out]*/ CComBSTR& bstr, /*[in ]*/ VARIANT* newVal, /*[in]*/ bool fNullOk = true );
  242. ////////////////////////////////////////////////////////////////////////////////
  243. class CComHGLOBAL
  244. {
  245. HGLOBAL m_hg;
  246. mutable LPVOID m_ptr;
  247. mutable DWORD m_dwLock;
  248. public:
  249. CComHGLOBAL();
  250. ~CComHGLOBAL();
  251. // copy constructors...
  252. CComHGLOBAL ( /*[in]*/ const CComHGLOBAL& chg );
  253. CComHGLOBAL& operator=( /*[in]*/ const CComHGLOBAL& chg );
  254. CComHGLOBAL& operator=( /*[in]*/ HGLOBAL hg );
  255. void Attach( /*[in]*/ HGLOBAL hg );
  256. HGLOBAL Detach( );
  257. HGLOBAL Get () const;
  258. HGLOBAL GetRef ();
  259. HGLOBAL GetPointer();
  260. DWORD Size() const;
  261. ////////////////////
  262. HRESULT New ( /*[in]*/ UINT uFlags, /*[in]*/ DWORD dwSize );
  263. void Release( );
  264. LPVOID Lock () const;
  265. void Unlock() const;
  266. HRESULT Copy( /*[in]*/ HGLOBAL hg, /*[in]*/ DWORD dwMaxSize = 0xFFFFFFFF );
  267. HRESULT CopyFromStream( /*[in ]*/ IStream* val );
  268. HRESULT CopyToStream ( /*[out]*/ IStream* val );
  269. HRESULT CloneAsStream ( /*[out]*/ IStream* *pVal );
  270. HRESULT DetachAsStream( /*[out]*/ IStream* *pVal );
  271. HRESULT GetAsStream ( /*[out]*/ IStream* *pVal, /*[in]*/ bool fClone );
  272. HRESULT NewStream ( /*[out]*/ IStream* *pVal );
  273. };
  274. //////////////////////////////////////////////////
  275. namespace Connectivity
  276. {
  277. class Proxy
  278. {
  279. bool m_fInitialized;
  280. MPC::string m_strProxy;
  281. MPC::string m_strProxyBypass;
  282. CComHGLOBAL m_hgConnection;
  283. public:
  284. Proxy();
  285. ~Proxy();
  286. HRESULT Initialize( /*[in]*/ bool fImpersonate = false );
  287. HRESULT Apply ( /*[in]*/ HINTERNET hSession );
  288. friend HRESULT operator>>( /*[in]*/ MPC::Serializer& stream, /*[out]*/ Proxy& val );
  289. friend HRESULT operator<<( /*[in]*/ MPC::Serializer& stream, /*[in ]*/ const Proxy& val );
  290. };
  291. class WinInetTimeout
  292. {
  293. MPC::CComSafeAutoCriticalSection& m_cs;
  294. HINTERNET& m_hReq;
  295. HANDLE m_hTimer;
  296. DWORD m_dwTimeout;
  297. INTERNET_STATUS_CALLBACK m_PreviousCallback;
  298. DWORD_PTR m_PreviousContext;
  299. static VOID CALLBACK TimerFunction( PVOID lpParameter, BOOLEAN TimerOrWaitFired );
  300. static VOID CALLBACK InternetStatusCallback( HINTERNET hInternet ,
  301. DWORD_PTR dwContext ,
  302. DWORD dwInternetStatus ,
  303. LPVOID lpvStatusInformation ,
  304. DWORD dwStatusInformationLength );
  305. HRESULT InternalSet ();
  306. HRESULT InternalReset();
  307. public:
  308. WinInetTimeout( /*[in]*/ MPC::CComSafeAutoCriticalSection& cs, /*[in]*/ HINTERNET& hReq );
  309. ~WinInetTimeout();
  310. HRESULT Set ( /*[in]*/ DWORD dwTimeout );
  311. HRESULT Reset( );
  312. };
  313. HRESULT NetworkAlive ( /*[in]*/ DWORD dwTimeout, /*[in]*/ MPC::Connectivity::Proxy* pProxy = NULL );
  314. HRESULT DestinationReachable( /*[in]*/ LPCWSTR szDestination, /*[in]*/ DWORD dwTimeout, /*[in]*/ MPC::Connectivity::Proxy* pProxy = NULL );
  315. };
  316. ////////////////////////////////////////////////////////////////////////////////
  317. class RegKey
  318. {
  319. REGSAM m_samDesired;
  320. HKEY m_hRoot;
  321. mutable HKEY m_hKey;
  322. MPC::wstring m_strKey;
  323. MPC::wstring m_strPath;
  324. MPC::wstring m_strName;
  325. HRESULT Clean( /*[in]*/ bool fBoth );
  326. public:
  327. RegKey();
  328. ~RegKey();
  329. operator HKEY() const;
  330. RegKey& operator=( /*[in]*/ const RegKey& rk );
  331. HRESULT SetRoot( /*[in]*/ HKEY hKey, /*[in]*/ REGSAM samDesired = KEY_READ, /*[in]*/ LPCWSTR szMachine = NULL );
  332. HRESULT Attach ( /*[in]*/ LPCWSTR szKeyName );
  333. HRESULT Exists( /*[out]*/ bool& fFound ) const;
  334. HRESULT Create( ) const;
  335. HRESULT Delete( /*[in]*/ bool fDeep ) ;
  336. HRESULT SubKey( /*[in]*/ LPCWSTR szKeyName, /*[out]*/ RegKey& rkSubKey ) const;
  337. HRESULT Parent( /*[out]*/ RegKey& rkParent ) const;
  338. HRESULT EnumerateSubKeys( /*[out]*/ MPC::WStringList& lstSubKeys ) const;
  339. HRESULT EnumerateValues ( /*[out]*/ MPC::WStringList& lstValues ) const;
  340. HRESULT DeleteSubKeys() const;
  341. HRESULT DeleteValues () const;
  342. HRESULT ReadDirect ( /*[in]*/ LPCWSTR szValueName, /*[out]*/ CComHGLOBAL& hgBuffer, /*[out]*/ DWORD& dwSize, /*[out]*/ DWORD& dwType, /*[out]*/ bool& fFound ) const;
  343. HRESULT WriteDirect( /*[in]*/ LPCWSTR szValueName, /*[in ]*/ void* pBuffer , /*[in ]*/ DWORD dwSize, /*[in ]*/ DWORD dwType ) const;
  344. HRESULT get_Key ( /*[out]*/ MPC::wstring& strKey ) const;
  345. HRESULT get_Name( /*[out]*/ MPC::wstring& strName ) const;
  346. HRESULT get_Path( /*[out]*/ MPC::wstring& strPath ) const;
  347. HRESULT get_Value( /*[out]*/ VARIANT& vValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szValueName = NULL ) const;
  348. HRESULT put_Value( /*[in] */ const VARIANT vValue, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ bool fExpand = false ) const;
  349. HRESULT del_Value( /*[in]*/ LPCWSTR szValueName = NULL ) const;
  350. HRESULT Read( /*[out]*/ MPC::string& strValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szValueName = NULL );
  351. HRESULT Read( /*[out]*/ MPC::wstring& strValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szValueName = NULL );
  352. HRESULT Read( /*[out]*/ CComBSTR& bstrValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szValueName = NULL );
  353. HRESULT Read( /*[out]*/ DWORD& dwValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szValueName = NULL );
  354. HRESULT Read( /*[out]*/ MPC::WStringList& lstValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szValueName );
  355. HRESULT Write( /*[in]*/ const MPC::string& strValue, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ bool fExpand = false );
  356. HRESULT Write( /*[in]*/ const MPC::wstring& strValue, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ bool fExpand = false );
  357. HRESULT Write( /*[in]*/ BSTR bstrValue, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ bool fExpand = false );
  358. HRESULT Write( /*[in]*/ DWORD dwValue, /*[in]*/ LPCWSTR szValueName = NULL );
  359. HRESULT Write( /*[in]*/ const MPC::WStringList& lstValue, /*[in]*/ LPCWSTR szValueName );
  360. static HRESULT ParsePath( /*[in]*/ LPCWSTR szKey, /*[out]*/ HKEY& hKey, /*[out]*/ LPCWSTR& szPath, /*[in]*/ HKEY hKeyDefault = HKEY_LOCAL_MACHINE );
  361. };
  362. HRESULT RegKey_Value_Read ( /*[out]*/ VARIANT& vValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szKeyName, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ HKEY hKey = HKEY_LOCAL_MACHINE );
  363. HRESULT RegKey_Value_Read ( /*[out]*/ MPC::wstring& strValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szKeyName, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ HKEY hKey = HKEY_LOCAL_MACHINE );
  364. HRESULT RegKey_Value_Read ( /*[out]*/ DWORD& dwValue, /*[out]*/ bool& fFound, /*[in]*/ LPCWSTR szKeyName, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ HKEY hKey = HKEY_LOCAL_MACHINE );
  365. HRESULT RegKey_Value_Write( /*[in] */ const VARIANT& vValue, /*[in]*/ LPCWSTR szKeyName, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ HKEY hKey = HKEY_LOCAL_MACHINE, /*[in]*/ bool fExpand = false );
  366. HRESULT RegKey_Value_Write( /*[in] */ const MPC::wstring& strValue, /*[in]*/ LPCWSTR szKeyName, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ HKEY hKey = HKEY_LOCAL_MACHINE, /*[in]*/ bool fExpand = false );
  367. HRESULT RegKey_Value_Write( /*[in] */ DWORD dwValue, /*[in]*/ LPCWSTR szKeyName, /*[in]*/ LPCWSTR szValueName = NULL, /*[in]*/ HKEY hKey = HKEY_LOCAL_MACHINE );
  368. //////////////////////////////////////////////////
  369. //
  370. // These functions also deal with Read-Only, protected files.
  371. //
  372. HRESULT CopyFile ( /*[in]*/ LPCWSTR szFileSrc, /*[in]*/ LPCWSTR szFileDst, /*[in]*/ bool fForce = true, /*[in]*/ bool fDelayed = false );
  373. HRESULT CopyFile ( /*[in]*/ const MPC::wstring& strFileSrc, /*[in]*/ const MPC::wstring& strFileDst, /*[in]*/ bool fForce = true, /*[in]*/ bool fDelayed = false );
  374. HRESULT MoveFile ( /*[in]*/ LPCWSTR szFileSrc, /*[in]*/ LPCWSTR szFileDst, /*[in]*/ bool fForce = true, /*[in]*/ bool fDelayed = false );
  375. HRESULT MoveFile ( /*[in]*/ const MPC::wstring& strFileSrc, /*[in]*/ const MPC::wstring& strFileDst, /*[in]*/ bool fForce = true, /*[in]*/ bool fDelayed = false );
  376. HRESULT DeleteFile( /*[in]*/ LPCWSTR szFile, /*[in]*/ bool fForce = true, /*[in]*/ bool fDelayed = false );
  377. HRESULT DeleteFile( /*[in]*/ const MPC::wstring& strFile, /*[in]*/ bool fForce = true, /*[in]*/ bool fDelayed = false );
  378. ////////////////////
  379. class FileSystemObject // Hungarian: fso
  380. {
  381. public:
  382. typedef std::list<FileSystemObject*> List;
  383. typedef List::iterator Iter;
  384. typedef List::const_iterator IterConst;
  385. private:
  386. MPC::wstring m_strPath;
  387. WIN32_FILE_ATTRIBUTE_DATA m_wfadInfo;
  388. List m_lstChilds;
  389. bool m_fLoaded;
  390. bool m_fScanned;
  391. bool m_fScanned_Deep;
  392. void Init ( /*[in]*/ LPCWSTR szPath, /*[in]*/ const FileSystemObject* fsoParent );
  393. void Clean( );
  394. public:
  395. FileSystemObject( /*[in]*/ LPCWSTR szPath = NULL, /*[in]*/ const FileSystemObject* fsoParent = NULL );
  396. FileSystemObject( /*[in]*/ const WIN32_FIND_DATAW& wfdInfo , /*[in]*/ const FileSystemObject* fsoParent = NULL );
  397. FileSystemObject( /*[in]*/ const FileSystemObject& fso );
  398. ~FileSystemObject();
  399. FileSystemObject& operator=( /*[in]*/ LPCWSTR szPath );
  400. FileSystemObject& operator=( /*[in]*/ const FileSystemObject& fso );
  401. HRESULT Scan( /*[in]*/ bool fDeep = false, /*[in]*/ bool fReload = false, /*[in]*/ LPCWSTR szSearchPattern = NULL );
  402. bool Exists ();
  403. bool IsFile ();
  404. bool IsDirectory();
  405. HRESULT EnumerateFolders( /*[out]*/ List& lstFolders );
  406. HRESULT EnumerateFiles ( /*[out]*/ List& lstFiles );
  407. HRESULT get_Name ( /*[out]*/ MPC::wstring& szName ) const;
  408. HRESULT get_Path ( /*[out]*/ MPC::wstring& szPath ) const;
  409. HRESULT get_Parent( /*[out]*/ MPC::wstring& szParent ) const;
  410. HRESULT get_Attributes ( /*[out]*/ DWORD& dwFileAttributes );
  411. HRESULT put_Attributes ( /*[in] */ DWORD dwFileAttributes );
  412. HRESULT get_CreationTime ( /*[out]*/ FILETIME& ftCreationTime );
  413. HRESULT put_CreationTime ( /*[in] */ const FILETIME& ftCreationTime );
  414. HRESULT get_LastAccessTime( /*[out]*/ FILETIME& ftLastAccessTime );
  415. HRESULT put_LastAccessTime( /*[in] */ const FILETIME& ftLastAccessTime );
  416. HRESULT get_LastWriteTime ( /*[out]*/ FILETIME& ftLastWriteTime );
  417. HRESULT put_LastWriteTime ( /*[in] */ const FILETIME& ftLastWriteTime );
  418. HRESULT get_FileSize ( /*[out]*/ DWORD& nFileSizeLow , /*[out]*/ DWORD *pnFileSizeHigh = NULL );
  419. HRESULT CreateDir ( /*[in]*/ bool fForce = false );
  420. HRESULT Delete ( /*[in]*/ bool fForce = false, /*[in]*/ bool fComplain = true );
  421. HRESULT DeleteChildren( /*[in]*/ bool fForce = false, /*[in]*/ bool fComplain = true );
  422. HRESULT Rename ( /*[in]*/ const FileSystemObject& fsoDst, /*[in]*/ bool fForce = false );
  423. HRESULT Copy ( /*[in]*/ const FileSystemObject& fsoDst, /*[in]*/ bool fForce = false );
  424. HRESULT Open( /*[out]*/ HANDLE& hfFile, /*[in]*/ DWORD dwDesiredAccess, /*[in]*/ DWORD dwShareMode, /*[in]*/ DWORD dwCreationDisposition );
  425. //////////////////////////////////////////////////
  426. static bool Exists ( /*[in]*/ LPCWSTR szPath );
  427. static bool IsFile ( /*[in]*/ LPCWSTR szPath );
  428. static bool IsDirectory( /*[in]*/ LPCWSTR szPath );
  429. };
  430. ////////////////////
  431. class StorageObject // Hungarian: so
  432. {
  433. public:
  434. typedef std::list<StorageObject*> List;
  435. typedef List::iterator Iter;
  436. typedef List::const_iterator IterConst;
  437. struct Stat : public STATSTG
  438. {
  439. Stat();
  440. ~Stat();
  441. void Clean();
  442. };
  443. private:
  444. StorageObject* m_parent;
  445. CComBSTR m_bstrPath;
  446. bool m_fITSS;
  447. DWORD m_grfMode;
  448. DWORD m_type;
  449. Stat m_stat;
  450. CComPtr<IStorage> m_stg;
  451. CComPtr<IStream> m_stream;
  452. bool m_fChecked;
  453. bool m_fScanned;
  454. bool m_fMarkedForDeletion;
  455. List m_lstChilds;
  456. ////////////////////
  457. void Init ( /*[in]*/ DWORD grfMode, /*[in]*/ bool fITSS, /*[in]*/ LPCWSTR szPath, /*[in]*/ StorageObject* soParent );
  458. void Clean( /*[in]*/ bool fFinal );
  459. HRESULT Scan();
  460. HRESULT RemoveChild( /*[in]*/ StorageObject* child );
  461. private:
  462. // copy constructors...
  463. StorageObject ( /*[in]*/ const StorageObject& so );
  464. StorageObject& operator=( /*[in]*/ const StorageObject& so );
  465. public:
  466. StorageObject( /*[in]*/ DWORD grfMode = STGM_READ, /*[in]*/ bool fITSS = false, /*[in]*/ LPCWSTR szPath = NULL, /*[in]*/ StorageObject* soParent = NULL );
  467. ~StorageObject();
  468. StorageObject& operator=( /*[in]*/ LPCWSTR szPath );
  469. ////////////////////
  470. const Stat& GetStat() { m_stat; }
  471. HRESULT Compact ( );
  472. HRESULT Exists ( );
  473. HRESULT EnumerateSubStorages( /*[out]*/ List& lstSubStorages );
  474. HRESULT EnumerateStreams ( /*[out]*/ List& lstStreams );
  475. HRESULT GetStorage( /*[out]*/ CComPtr<IStorage>& out );
  476. HRESULT GetStream ( /*[out]*/ CComPtr<IStream>& out );
  477. HRESULT GetChild ( /*[in]*/ LPCWSTR szName, /*[out]*/ StorageObject*& child, /*[in]*/ DWORD grfMode = STGM_READ, /*[in]*/ DWORD type = 0 );
  478. HRESULT Create ();
  479. HRESULT Rewind ();
  480. HRESULT Truncate ();
  481. HRESULT Delete ();
  482. HRESULT DeleteChildren();
  483. void Release ();
  484. const CComBSTR& GetName() { return m_bstrPath; }
  485. };
  486. /////////////////////////////////////////////////////////////////////////////
  487. class NamedMutex
  488. {
  489. protected:
  490. bool m_fCloseOnRelease; // If true, the mutex is closed on the last release.
  491. MPC::wstring m_szName; // Name of the object, optional.
  492. HANDLE m_hMutex; // The mutex handle.
  493. DWORD m_dwCount; // Recursion counter.
  494. void CleanUp ();
  495. HRESULT EnsureInitialized();
  496. public:
  497. NamedMutex( LPCWSTR szName, bool fCloseOnRelease = true );
  498. virtual ~NamedMutex();
  499. HRESULT SetName( LPCWSTR szName );
  500. HRESULT Acquire( DWORD dwMilliseconds = INFINITE );
  501. HRESULT Release( );
  502. bool IsOwned( );
  503. };
  504. class NamedMutexWithState : public NamedMutex
  505. {
  506. DWORD m_dwSize; // Size of the shared state.
  507. HANDLE m_hMap; // Handle to the mapping object.
  508. LPVOID m_rgData; // Pointer to the mapped area.
  509. void CleanUp ();
  510. void Flush ();
  511. HRESULT EnsureInitialized();
  512. public:
  513. NamedMutexWithState( LPCWSTR szName, DWORD dwSize, bool fCloseOnRelease = false );
  514. virtual ~NamedMutexWithState();
  515. HRESULT SetName( LPCWSTR szName );
  516. HRESULT Acquire( DWORD dwMilliseconds = INFINITE );
  517. HRESULT Release( );
  518. LPVOID GetData();
  519. };
  520. ////////////////////////////////////////////////////////////////////////////////
  521. namespace Pooling
  522. {
  523. class Base
  524. {
  525. protected:
  526. MPC::CComSafeAutoCriticalSection m_cs;
  527. DWORD m_dwInCallback;
  528. DWORD m_dwThreadID;
  529. public:
  530. Base();
  531. void Lock ();
  532. void Unlock();
  533. void AddRef ();
  534. void Release();
  535. };
  536. class Timer : public Base
  537. {
  538. DWORD m_dwFlags;
  539. HANDLE m_hTimer;
  540. static VOID CALLBACK TimerFunction( PVOID lpParameter, BOOLEAN TimerOrWaitFired );
  541. public:
  542. Timer( /*[in]*/ DWORD dwFlags = WT_EXECUTEDEFAULT );
  543. ~Timer();
  544. HRESULT Set ( /*[in]*/ DWORD dwTimeout, /*[in]*/ DWORD dwPeriod );
  545. HRESULT Reset( );
  546. virtual HRESULT Execute( /*[in]*/ BOOLEAN TimerOrWaitFired );
  547. };
  548. class Event : public Base
  549. {
  550. DWORD m_dwFlags;
  551. HANDLE m_hWaitHandle;
  552. HANDLE m_hEvent;
  553. static VOID CALLBACK WaitOrTimerFunction( PVOID lpParameter, BOOLEAN EventOrWaitFired );
  554. public:
  555. Event( /*[in]*/ DWORD dwFlags = WT_EXECUTEDEFAULT );
  556. ~Event();
  557. void Attach( /*[in]*/ HANDLE hEvent );
  558. HRESULT Set ( /*[in]*/ DWORD dwTimeout );
  559. HRESULT Reset( );
  560. virtual HRESULT Signaled( /*[in]*/ BOOLEAN TimerOrWaitFired );
  561. };
  562. };
  563. };
  564. #endif // !defined(__INCLUDED___MPC___UTILS_H___)