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.

212 lines
5.7 KiB

  1. //=================================================================
  2. //
  3. // DllUnreg.cpp
  4. //
  5. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #include "precomp.h"
  9. extern HMODULE ghModule ;
  10. //***************************************************************************
  11. //
  12. // UnregisterServer
  13. //
  14. // Given a clsid, remove the com registration
  15. //
  16. //***************************************************************************
  17. HRESULT UnregisterServer( REFGUID a_rguid )
  18. {
  19. WCHAR wcID[128];
  20. WCHAR szCLSID[128];
  21. WCHAR szProviderCLSIDAppID[128];
  22. HKEY hKey;
  23. // Create the path using the CLSID
  24. StringFromGUID2( a_rguid, wcID, 128);
  25. StringCchCopyW(szCLSID, LENGTH_OF(szCLSID),TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  26. StringCchCopyW(szProviderCLSIDAppID,LENGTH_OF(szProviderCLSIDAppID), TEXT("SOFTWARE\\CLASSES\\APPID\\"));
  27. StringCchCatW(szCLSID, LENGTH_OF(szCLSID),wcID);
  28. StringCchCatW(szProviderCLSIDAppID,LENGTH_OF(szProviderCLSIDAppID), wcID);
  29. DWORD dwRet ;
  30. //Delete entries under APPID
  31. dwRet = RegDeleteKeyW(HKEY_LOCAL_MACHINE, szProviderCLSIDAppID);
  32. dwRet = RegOpenKeyW(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  33. if(dwRet == NO_ERROR)
  34. {
  35. dwRet = RegDeleteKey(hKey, L"InProcServer32" );
  36. dwRet = RegDeleteKey(hKey, L"LocalServer32");
  37. CloseHandle(hKey);
  38. }
  39. dwRet = RegDeleteKeyW(HKEY_LOCAL_MACHINE, szCLSID);
  40. return NOERROR;
  41. }
  42. //***************************************************************************
  43. //
  44. // Is4OrMore
  45. //
  46. // Returns true if win95 or any version of NT > 3.51
  47. //
  48. //***************************************************************************
  49. BOOL Is4OrMore ()
  50. {
  51. OSVERSIONINFO os;
  52. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  53. if ( ! GetVersionEx ( & os ) )
  54. {
  55. return FALSE; // should never happen
  56. }
  57. return os.dwMajorVersion >= 4;
  58. }
  59. /***************************************************************************
  60. * SetKeyAndValue
  61. *
  62. * Purpose:
  63. * Private helper function for DllRegisterServer that creates
  64. * a key, sets a value, and closes that key.
  65. *
  66. * Parameters:
  67. * pszKey LPTSTR to the ame of the key
  68. * pszSubkey LPTSTR ro the name of a subkey
  69. * pszValue LPTSTR to the value to store
  70. *
  71. * Return Value:
  72. * BOOL TRUE if successful, FALSE otherwise.
  73. ***************************************************************************/
  74. BOOL SetKeyAndValue (
  75. wchar_t *pszKey,
  76. wchar_t *pszSubkey,
  77. wchar_t *pszValueName,
  78. wchar_t *pszValue
  79. )
  80. {
  81. HKEY hKey;
  82. WCHAR szKey[256];
  83. StringCchCopyW(szKey,LENGTH_OF(szKey),pszKey);
  84. if (NULL != pszSubkey)
  85. {
  86. StringCchCatW(szKey,LENGTH_OF(szKey), _T("\\"));
  87. StringCchCatW(szKey,LENGTH_OF(szKey), pszSubkey);
  88. }
  89. if (ERROR_SUCCESS!=RegCreateKeyExW(HKEY_LOCAL_MACHINE
  90. , szKey, 0, NULL, REG_OPTION_NON_VOLATILE
  91. , KEY_ALL_ACCESS, NULL, &hKey, NULL))
  92. return FALSE;
  93. if (NULL!=pszValue)
  94. {
  95. if (ERROR_SUCCESS != RegSetValueExW(hKey, (LPCTSTR)pszValueName, 0, REG_SZ, (BYTE *)(LPCTSTR)pszValue
  96. , (wcslen(pszValue)+1)*sizeof(WCHAR)))
  97. return FALSE;
  98. }
  99. RegCloseKey(hKey);
  100. return TRUE;
  101. }
  102. //***************************************************************************
  103. //
  104. // RegisterServer
  105. //
  106. // Given a clsid and a description, perform the com registration
  107. //
  108. //***************************************************************************
  109. HRESULT RegisterServer (
  110. WCHAR *a_pName,
  111. REFGUID a_rguid
  112. )
  113. {
  114. WCHAR wcID[128];
  115. WCHAR szCLSID[128];
  116. WCHAR szModule[MAX_PATH + 1];
  117. WCHAR * pName = _T("WBEM Framework Instance Provider");
  118. WCHAR * pModel;
  119. HKEY hKey1;
  120. szModule[MAX_PATH] = 0;
  121. GetModuleFileName(ghModule, szModule, MAX_PATH);
  122. // Normally we want to use "Both" as the threading model since
  123. // the DLL is free threaded, but NT 3.51 Ole doesnt work unless
  124. // the model is "Aparment"
  125. if(Is4OrMore())
  126. pModel = L"Both" ;
  127. else
  128. pModel = L"Apartment" ;
  129. // Create the path.
  130. StringFromGUID2(a_rguid, wcID, 128);
  131. StringCchCopyW(szCLSID,LENGTH_OF(szCLSID), TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  132. StringCchCatW(szCLSID,LENGTH_OF(szCLSID), wcID);
  133. #ifdef LOCALSERVER
  134. WCHAR szProviderCLSIDAppID[128];
  135. StringCchCopyW(szProviderCLSIDAppID,LENGTH_OF(szProviderCLSIDAppID),TEXT("SOFTWARE\\CLASSES\\APPID\\"));
  136. StringCchCatW(szProviderCLSIDAppID,LENGTH_OF(szProviderCLSIDAppID),wcID);
  137. if (FALSE ==SetKeyAndValue(szProviderCLSIDAppID, NULL, NULL, a_pName ))
  138. return SELFREG_E_CLASS;
  139. #endif
  140. // Create entries under CLSID
  141. RegCreateKeyW(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  142. RegSetValueExW(hKey1, NULL, 0, REG_SZ, (BYTE *)a_pName, (lstrlen(a_pName)+1) *
  143. sizeof(WCHAR));
  144. #ifdef LOCALSERVER
  145. if (FALSE ==SetKeyAndValue(szCLSID, _T("LocalServer32"), NULL,szModule))
  146. return SELFREG_E_CLASS;
  147. if (FALSE ==SetKeyAndValue(szCLSID, _T("LocalServer32"),_T("ThreadingModel"), pModel))
  148. return SELFREG_E_CLASS;
  149. #else
  150. HKEY hKey2 ;
  151. RegCreateKey(hKey1, _T("InprocServer32"), &hKey2);
  152. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  153. (lstrlen(szModule)+1) * sizeof(TCHAR));
  154. RegSetValueEx(hKey2, _T("ThreadingModel"), 0, REG_SZ,
  155. (BYTE *)pModel, (lstrlen(pModel)+1) * sizeof(TCHAR));
  156. CloseHandle(hKey2);
  157. #endif
  158. CloseHandle(hKey1);
  159. return NOERROR;
  160. }