//*************************************************************************** // // Copyright © Microsoft Corporation. All rights reserved. // // SmartPtr.h // // Purpose: Declare smartpointer typedefs // //*************************************************************************** #pragma once #include _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject)); _COM_SMARTPTR_TYPEDEF(IWbemObjectSink, __uuidof(IWbemObjectSink)); _COM_SMARTPTR_TYPEDEF(IWbemQualifierSet, __uuidof(IWbemQualifierSet)); _COM_SMARTPTR_TYPEDEF(IWbemObjectAccess, __uuidof(IWbemObjectAccess)); _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices)); _COM_SMARTPTR_TYPEDEF(IWbemQuery, __uuidof(IWbemQuery)); _COM_SMARTPTR_TYPEDEF(IWbemContext, __uuidof(IWbemContext)); _COM_SMARTPTR_TYPEDEF(IWbemCallResult, __uuidof(IWbemCallResult)); _COM_SMARTPTR_TYPEDEF(IWbemObjectSink, __uuidof(IWbemObjectSink)); _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject)); _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator)); _COM_SMARTPTR_TYPEDEF(IUnsecuredApartment, __uuidof(IUnsecuredApartment)); _COM_SMARTPTR_TYPEDEF(IWbemStatusCodeText, __uuidof(IWbemStatusCodeText)); //_COM_SMARTPTR_TYPEDEF(IWbemBackupRestore, __uuidof(IWbemBackupRestore)); _COM_SMARTPTR_TYPEDEF(IWbemRefresher, __uuidof(IWbemRefresher)); _COM_SMARTPTR_TYPEDEF(IWbemHiPerfEnum, __uuidof(IWbemHiPerfEnum)); _COM_SMARTPTR_TYPEDEF(IWbemConfigureRefresher, __uuidof(IWbemConfigureRefresher)); _COM_SMARTPTR_TYPEDEF(IMofCompiler, __uuidof(IMofCompiler)); _COM_SMARTPTR_TYPEDEF(ExternalMethodContext, __uuidof(ExternalMethodContext)); _COM_SMARTPTR_TYPEDEF(InternalMethodContext, __uuidof(InternalMethodContext)); _COM_SMARTPTR_TYPEDEF(InternalMethodContextAsynch, __uuidof(InternalMethodContextAsynch)); _COM_SMARTPTR_TYPEDEF(CInstance, __uuidof(CInstance)); class SmartCloseHandle { private: HANDLE m_h; public: SmartCloseHandle():m_h(INVALID_HANDLE_VALUE){} SmartCloseHandle(HANDLE h):m_h(h){} ~SmartCloseHandle(){if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h);} HANDLE operator =(HANDLE h) {if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h); m_h=h; return h;} operator HANDLE() const {return m_h;} HANDLE* operator &() {if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h); m_h = INVALID_HANDLE_VALUE; return &m_h;} }; class Smart_findclose { private: intptr_t m_h; public: Smart_findclose():m_h(0){} Smart_findclose(intptr_t h):m_h(h){} ~Smart_findclose(){if (m_h!=0) _findclose(m_h);} intptr_t operator =(intptr_t h) {if (m_h) _findclose(m_h); m_h=h; return h;} operator intptr_t() const {return m_h;} intptr_t* operator &() {if (m_h) _findclose(m_h); m_h = 0; return &m_h;} }; class SmartFindClose { private: HANDLE m_h; public: SmartFindClose():m_h(INVALID_HANDLE_VALUE){} SmartFindClose(HANDLE h):m_h(h){} ~SmartFindClose(){if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h);} HANDLE operator =(HANDLE h) {if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h); m_h=h; return h;} operator HANDLE() const {return m_h;} HANDLE* operator &() {if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h); m_h = INVALID_HANDLE_VALUE; return &m_h;} }; class SmartCloseServiceHandle { private: SC_HANDLE m_h; public: SmartCloseServiceHandle():m_h(NULL){} SmartCloseServiceHandle(SC_HANDLE h):m_h(h){} ~SmartCloseServiceHandle(){if (m_h!=NULL) CloseServiceHandle(m_h);} SC_HANDLE operator =(SC_HANDLE h) {if (m_h!=NULL) CloseServiceHandle(m_h); m_h=h; return h;} operator SC_HANDLE() const {return m_h;} SC_HANDLE* operator &() {if (m_h!=NULL) CloseServiceHandle(m_h); m_h = NULL; return &m_h;} }; class CSmartCreatedDC { public: CSmartCreatedDC(HDC hdc) { m_hdc = hdc;} operator HDC() const {return m_hdc;} ~CSmartCreatedDC() { if (m_hdc) DeleteDC(m_hdc); } protected: HDC m_hdc; }; class CSmartBuffer { private: LPBYTE m_pBuffer; public: CSmartBuffer() : m_pBuffer(NULL) {} CSmartBuffer(LPBYTE pBuffer) : m_pBuffer(pBuffer) {} CSmartBuffer(DWORD dwSize) { m_pBuffer = new BYTE[dwSize]; if (m_pBuffer == NULL) { throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ; } } ~CSmartBuffer() { Free(); } LPBYTE operator =(LPBYTE pBuffer) { Free(); m_pBuffer = pBuffer; return m_pBuffer; } operator LPBYTE() const { return m_pBuffer; } LPBYTE* operator &() { Free(); m_pBuffer = NULL; return &m_pBuffer; } protected: void Free() { if (m_pBuffer != NULL) { delete [] m_pBuffer; } } }; template class FrameDynOnDeleteObjVoid { private: O* Obj_; BOOL bExec; public: FrameDynOnDeleteObjVoid ( O* Val ): Obj_ ( Val ), bExec ( FALSE ) { }; void Exec ( BOOL bSetExecFlag = TRUE ) { (Obj_->*F)(); if ( bSetExecFlag ) { bExec = TRUE; } } void SetExecFlag ( BOOL bSetExecFlag = TRUE ) { bExec = bSetExecFlag ; } void ReSetExecFlag ( ) { SetExecFlag ( FALSE ) ; } ~FrameDynOnDeleteObjVoid ( ) { if ( !bExec ) { Exec (); } }; }; template class FrameDynOnDelete { private: T Val_; BOOL bExec; public: FrameDynOnDelete ( T Val ): Val_ ( Val ), bExec ( FALSE ) { }; void Exec ( ) { F(Val_); bExec = TRUE; } ~FrameDynOnDelete ( ) { if ( !bExec ) { Exec (); } }; }; #ifndef __WAITEX__ #define __WAITEX__ template < typename T, typename FT, FT F, int iTime > class WaitException { public: WaitException ( T Val_ ) { BOOL bResult = FALSE; while ( ! bResult ) { try { F ( Val_ ); bResult = TRUE; } catch ( ... ) { } if ( ! bResult ) { ::Sleep ( iTime ); } } } }; #endif __WAITEX__