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.

207 lines
5.1 KiB

  1. // MdKey.cpp
  2. #include "stdafx.h"
  3. // Do stuff to define the iadm guid
  4. #include <objbase.h>
  5. #include <initguid.h>
  6. #define INITGUID
  7. #include "iadm.h"
  8. #include "mdkey.h"
  9. #define TIMEOUT_VALUE 5000
  10. CMDKey::CMDKey():
  11. m_cCoInits(0)
  12. {
  13. m_pcCom = NULL;
  14. m_hKey = NULL;
  15. }
  16. CMDKey::~CMDKey()
  17. {
  18. this->Close();
  19. // while there are outstanding coinits, close them
  20. while ( m_cCoInits > 0 && !(m_cCoInits < 0) )
  21. DoCoUnInit();
  22. }
  23. HRESULT CMDKey::DoCoInitEx()
  24. {
  25. //HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  26. HRESULT hRes = CoInitialize(NULL);
  27. // track our calls to coinit
  28. if ( SUCCEEDED(hRes) )
  29. {
  30. m_cCoInits++;
  31. }
  32. return hRes;
  33. }
  34. void CMDKey::DoCoUnInit()
  35. {
  36. HRESULT hRes = NOERROR;
  37. // if there are outstanding coinits, uninit one
  38. if ( m_cCoInits > 0 )
  39. {
  40. CoUninitialize();
  41. m_cCoInits--;
  42. }
  43. // we shouldn't ever have a negative count. But just in case...
  44. assert( m_cCoInits >= 0 );
  45. if ( m_cCoInits < 0 )
  46. {
  47. // something is seriously wrong here. Prevent looping
  48. // by going straight to zero, and write an error to the log.
  49. m_cCoInits = 0;
  50. iisDebugOut(_T("WARNING: CoInits in mdkey have gone negative"));
  51. }
  52. }
  53. HRESULT CMDKey::OpenNode(LPCTSTR pchSubKeyPath)
  54. {
  55. HRESULT hRes = ERROR_SUCCESS;
  56. IClassFactory * pcsfFactory = NULL;
  57. BOOL b = FALSE;
  58. m_pcCom = NULL;
  59. m_hKey = NULL;
  60. WCHAR szSubKeyPath[_MAX_PATH];
  61. pszFailedAPI = NULL;
  62. iisDebugOut(_T("CMDKey::OpenNode(%s).\n"), pchSubKeyPath);
  63. if ( !pchSubKeyPath || !(*pchSubKeyPath) )
  64. {
  65. *szSubKeyPath = L'\0';
  66. }
  67. else
  68. {
  69. #if defined(UNICODE) || defined(_UNICODE)
  70. _tcscpy(szSubKeyPath, pchSubKeyPath);
  71. #else
  72. MultiByteToWideChar( CP_ACP, 0, pchSubKeyPath, -1, szSubKeyPath, _MAX_PATH);
  73. #endif
  74. }
  75. hRes = DoCoInitEx();
  76. if (FAILED(hRes))
  77. {
  78. iisDebugOut(_T("CoInitializeEx() failed, hRes=%x\n"), hRes);
  79. }
  80. hRes = CoGetClassObject(GETAdminBaseCLSID(TRUE), CLSCTX_SERVER, NULL, IID_IClassFactory, (void**) &pcsfFactory);
  81. if (FAILED(hRes))
  82. {
  83. //SetErrorFlag(__FILE__, __LINE__);
  84. //MyMessageBox(NULL, _T("CoGetClassObject"), hRes, MB_OK | MB_SETFOREGROUND);
  85. }
  86. else
  87. {
  88. hRes = pcsfFactory->CreateInstance(NULL, IID_IMSAdminBase, (void **) &m_pcCom);
  89. pcsfFactory->Release();
  90. if (FAILED(hRes))
  91. {
  92. //SetErrorFlag(__FILE__, __LINE__);
  93. //MyMessageBox(NULL, _T("CoCreateInstance"), hRes, MB_OK | MB_SETFOREGROUND);
  94. }
  95. else
  96. {
  97. hRes = m_pcCom->OpenKey(METADATA_MASTER_ROOT_HANDLE,szSubKeyPath,METADATA_PERMISSION_WRITE | METADATA_PERMISSION_READ,TIMEOUT_VALUE,&m_hKey);
  98. if (FAILED(hRes))
  99. {
  100. if (hRes != RETURNCODETOHRESULT(ERROR_PATH_NOT_FOUND))
  101. {
  102. //SetErrorFlag(__FILE__, __LINE__);
  103. //MyMessageBox(NULL, _T("OpenKey"), hRes, MB_OK | MB_SETFOREGROUND);
  104. }
  105. }
  106. else
  107. {
  108. b = TRUE;
  109. }
  110. } // end of CoCreateInstance
  111. } // end of CoGetClassObject
  112. if (!b) {this->Close();}
  113. return hRes;
  114. }
  115. HRESULT CMDKey::Close()
  116. {
  117. HRESULT hRes = ERROR_SUCCESS;
  118. if (m_pcCom)
  119. {
  120. if (m_hKey){hRes = m_pcCom->CloseKey(m_hKey);}
  121. hRes = m_pcCom->Release();
  122. }
  123. DoCoUnInit();
  124. m_pcCom = NULL;
  125. m_hKey = NULL;
  126. return hRes;
  127. }
  128. HRESULT CMDKey::DeleteNode(LPCTSTR pchSubKeyPath)
  129. {
  130. HRESULT hRes = ERROR_SUCCESS;
  131. WCHAR szSubKeyPath[_MAX_PATH];
  132. if ( pchSubKeyPath && (*pchSubKeyPath) )
  133. {
  134. #if defined(UNICODE) || defined(_UNICODE)
  135. _tcscpy(szSubKeyPath, pchSubKeyPath);
  136. #else
  137. MultiByteToWideChar( CP_ACP, 0, pchSubKeyPath, -1, szSubKeyPath, _MAX_PATH );
  138. #endif
  139. hRes = m_pcCom->DeleteKey(m_hKey, szSubKeyPath);
  140. }
  141. return hRes;
  142. }
  143. #if !defined(UNICODE) && !defined(_UNICODE)
  144. void MyMultiByteToWideChar( char *sData, WCHAR *wData, int cbBufSize, BOOL fMultiSZ)
  145. {
  146. MultiByteToWideChar( CP_ACP, 0, sData, -1, wData, cbBufSize );
  147. while (fMultiSZ)
  148. {
  149. sData = _tcsninc( sData, _tcslen(sData)) + 1;
  150. while (*wData++);
  151. if (*sData)
  152. {
  153. MultiByteToWideChar( CP_ACP, 0, sData, -1, wData, cbBufSize );
  154. }
  155. else
  156. {
  157. *wData = L'\0';
  158. break;
  159. }
  160. }
  161. return;
  162. }
  163. void MyWideCharToMultiByte( WCHAR *wData, char *sData, int cbBufSize, BOOL fMultiSZ)
  164. {
  165. WideCharToMultiByte( CP_ACP, 0, wData, -1, sData, cbBufSize, NULL, NULL );
  166. while (fMultiSZ)
  167. {
  168. while (*wData++);
  169. sData = _tcsninc( sData, _tcslen(sData)) + 1;
  170. if (*wData)
  171. {
  172. WideCharToMultiByte( CP_ACP, 0, wData, -1, sData, cbBufSize, NULL, NULL );
  173. }
  174. else
  175. {
  176. *sData = '\0';
  177. break;
  178. }
  179. }
  180. return;
  181. }
  182. #endif // not unicode