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.

200 lines
5.3 KiB

  1. //***************************************************************************
  2. //
  3. // MAINDLL.CPP
  4. //
  5. // Module: Sample WMI provider - SCE attachment
  6. //
  7. // Purpose: Contains DLL entry points. Also has code that controls
  8. // when the DLL can be unloaded by tracking the number of
  9. // objects and locks as well as routines that support
  10. // self registration.
  11. //
  12. // Copyright (c) 1997-1999 Microsoft Corporation
  13. //
  14. //***************************************************************************
  15. #include <objbase.h>
  16. #include <initguid.h>
  17. #include "podprov.h"
  18. HMODULE ghModule;
  19. DEFINE_GUID(CLSID_PodTestProv,0xc5f6cc21, 0x6195, 0x4555, 0xb9, 0xd8, 0x3e, 0xf3, 0x27, 0x76, 0x3c, 0xae);
  20. //{c5f6cc21_6195_4555_b9d8_3ef327763cae}
  21. //Count number of objects and number of locks.
  22. long g_cObj=0;
  23. long g_cLock=0;
  24. //***************************************************************************
  25. //
  26. // DllMain
  27. //
  28. // Purpose: Entry point for DLL.
  29. //
  30. // Return: TRUE if OK.
  31. //
  32. //***************************************************************************
  33. BOOL WINAPI DllMain(HINSTANCE hInstance, ULONG ulReason
  34. , LPVOID pvReserved)
  35. {
  36. if (DLL_PROCESS_ATTACH==ulReason)
  37. ghModule = hInstance;
  38. return TRUE;
  39. }
  40. //***************************************************************************
  41. //
  42. // DllGetClassObject
  43. //
  44. // Purpose: Called by Ole when some client wants a class factory. Return
  45. // one only if it is the sort of class this DLL supports.
  46. //
  47. //***************************************************************************
  48. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  49. {
  50. HRESULT hr;
  51. CProvFactory *pObj;
  52. if (CLSID_PodTestProv != rclsid)
  53. return E_FAIL;
  54. pObj=new CProvFactory();
  55. if (NULL==pObj)
  56. return E_OUTOFMEMORY;
  57. hr=pObj->QueryInterface(riid, ppv);
  58. if (FAILED(hr))
  59. delete pObj;
  60. return hr;
  61. }
  62. //***************************************************************************
  63. //
  64. // DllCanUnloadNow
  65. //
  66. // Purpose: Called periodically by Ole in order to determine if the
  67. // DLL can be freed.
  68. //
  69. // Return: S_OK if there are no objects in use and the class factory
  70. // isn't locked.
  71. //
  72. //***************************************************************************
  73. STDAPI DllCanUnloadNow(void)
  74. {
  75. SCODE sc;
  76. //It is OK to unload if there are no objects or locks on the
  77. // class factory.
  78. sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  79. return sc;
  80. }
  81. //***************************************************************************
  82. //
  83. // DllRegisterServer
  84. //
  85. // Purpose: Called during setup or by regsvr32.
  86. //
  87. // Return: NOERROR if registration successful, error otherwise.
  88. //***************************************************************************
  89. STDAPI DllRegisterServer(void)
  90. {
  91. char szID[128];
  92. WCHAR wcID[128];
  93. char szCLSID[128];
  94. char szModule[MAX_PATH];
  95. char * pName = "Sample SCE Pod Provider";
  96. char * pModel = "Both";
  97. HKEY hKey1, hKey2;
  98. LONG rc=NO_ERROR;
  99. // Create the path.
  100. StringFromGUID2(CLSID_PodTestProv, wcID, 128);
  101. wcstombs(szID, wcID, 128);
  102. strcpy(szCLSID, "Software\\classes\\CLSID\\");
  103. strcat(szCLSID, szID);
  104. // Create entries under CLSID
  105. rc = RegCreateKeyA(HKEY_LOCAL_MACHINE, szCLSID, &hKey1);
  106. if ( NO_ERROR == rc ) {
  107. rc = RegSetValueExA(hKey1, NULL, 0, REG_SZ, (BYTE *)pName, strlen(pName)+1);
  108. if ( NO_ERROR == rc ) {
  109. rc = RegCreateKeyA(hKey1,"InprocServer32",&hKey2);
  110. if ( NO_ERROR == rc ) {
  111. GetModuleFileNameA(ghModule, szModule, MAX_PATH);
  112. rc = RegSetValueExA(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  113. strlen(szModule)+1);
  114. if ( NO_ERROR == rc ) {
  115. rc = RegSetValueExA(hKey2, "ThreadingModel", 0, REG_SZ,
  116. (BYTE *)pModel, strlen(pModel)+1);
  117. }
  118. CloseHandle(hKey2);
  119. }
  120. }
  121. CloseHandle(hKey1);
  122. }
  123. return rc;
  124. }
  125. //***************************************************************************
  126. //
  127. // DllUnregisterServer
  128. //
  129. // Purpose: Called when it is time to remove the registry entries.
  130. //
  131. // Return: NOERROR if registration successful, error otherwise.
  132. //***************************************************************************
  133. STDAPI DllUnregisterServer(void)
  134. {
  135. char szID[128];
  136. WCHAR wcID[128];
  137. char szCLSID[128];
  138. HKEY hKey;
  139. // Create the path using the CLSID
  140. StringFromGUID2(CLSID_PodTestProv, wcID, 128);
  141. wcstombs(szID, wcID, 128);
  142. strcpy(szCLSID, "Software\\classes\\CLSID\\");
  143. strcat(szCLSID, szID);
  144. // First delete the InProcServer subkey.
  145. DWORD dwRet = RegOpenKeyA(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  146. if(dwRet == NO_ERROR)
  147. {
  148. RegDeleteKeyA(hKey, "InProcServer32");
  149. CloseHandle(hKey);
  150. dwRet = RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\classes\\CLSID", &hKey);
  151. if(dwRet == NO_ERROR)
  152. {
  153. RegDeleteKeyA(hKey,szID);
  154. CloseHandle(hKey);
  155. }
  156. }
  157. return dwRet;
  158. }