Source code of Windows XP (NT5)
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.

233 lines
6.6 KiB

  1. /****************************************************************************/
  2. // server.cpp
  3. //
  4. // General COM in-proc server framework code. TSSDI-specific code is
  5. // designated by CLSID SPECIFIC comments.
  6. //
  7. // Copyright (C) 2000 Microsoft Corporation
  8. /****************************************************************************/
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include <time.h>
  12. #include <locale.h>
  13. #include <tchar.h>
  14. #define INITGUID
  15. #include <ole2.h>
  16. #include <objbase.h>
  17. #include <comutil.h>
  18. #include <comdef.h>
  19. #include <adoid.h>
  20. #include <adoint.h>
  21. #include <initguid.h>
  22. #include <regapi.h>
  23. #include "factory.h"
  24. #include "trace.h"
  25. /****************************************************************************/
  26. // CLSID SPECIFIC section
  27. //
  28. // Provider-specific includes, unique CLSID, other info.
  29. /****************************************************************************/
  30. // For new components, this is the only area that needs to be modified in this
  31. // file. Include any appropriate header files, a unique CLSID and update
  32. // the macros.
  33. #include "tssd.h"
  34. // {943e9311-c6a6-42cf-a591-e7ce8bb1de8d}
  35. DEFINE_GUID(CLSID_TSSDSQL,
  36. 0x943e9311, 0xc6a6, 0x42cf, 0xA5, 0x91, 0xe7, 0xce, 0x8b, 0xb1, 0xde, 0x8d);
  37. #define IMPLEMENTED_CLSID CLSID_TSSDSQL
  38. #define SERVER_REGISTRY_COMMENT L"Terminal Server Session Directory Interface"
  39. #define CPP_CLASS_NAME CTSSessionDirectory
  40. #define INTERFACE_CAST (ITSSessionDirectory *)
  41. /****************************************************************************/
  42. // End CLSID SPECIFIC section
  43. /****************************************************************************/
  44. HINSTANCE g_hInstance;
  45. long g_lLocks = 0;
  46. long g_lObjects = 0;
  47. /****************************************************************************/
  48. // DllMain
  49. //
  50. // Standard DLL entry point. Returns FALSE on failure.
  51. /****************************************************************************/
  52. BOOL WINAPI DllMain(
  53. HINSTANCE hInstDLL,
  54. DWORD dwReason,
  55. LPVOID lpReserved)
  56. {
  57. if (dwReason == DLL_PROCESS_ATTACH) {
  58. setlocale(LC_ALL, ""); // Set to the 'current' locale
  59. g_hInstance = hInstDLL;
  60. DisableThreadLibraryCalls(hInstDLL);
  61. }
  62. return TRUE;
  63. }
  64. /****************************************************************************/
  65. // DllGetClassObject
  66. //
  67. // Standard OLE In-Process Server entry point to return an class factory
  68. // instance.
  69. //***************************************************************************
  70. STDAPI DllGetClassObject(
  71. REFCLSID rclsid,
  72. REFIID riid,
  73. LPVOID *ppv)
  74. {
  75. CClassFactory *pClassFactory;
  76. HRESULT hr;
  77. TRC2((TB,"DllGetClassObject"));
  78. // Verify the caller is asking for our type of object
  79. if (rclsid == IMPLEMENTED_CLSID) {
  80. // Create the class factory.
  81. pClassFactory = new CClassFactory;
  82. if (pClassFactory != NULL) {
  83. hr = pClassFactory->QueryInterface(riid, ppv);
  84. if (FAILED(hr)) {
  85. ERR((TB,"DllGetClassObject: GUID not found"));
  86. delete pClassFactory;
  87. }
  88. }
  89. else {
  90. ERR((TB,"DllGetClassObject: Failed alloc class factory"));
  91. hr = E_OUTOFMEMORY;
  92. }
  93. }
  94. else {
  95. ERR((TB,"DllGetClassObject: Failed alloc class factory"));
  96. hr = CLASS_E_CLASSNOTAVAILABLE;
  97. }
  98. return hr;
  99. }
  100. /****************************************************************************/
  101. // DllCanUnloadNow
  102. //
  103. // Standard COM entry point for COM server shutdown request. Allows shutdown
  104. // only if no outstanding objects or locks are present.
  105. /****************************************************************************/
  106. STDAPI DllCanUnloadNow(void)
  107. {
  108. HRESULT hr;
  109. if (g_lLocks == 0 && g_lObjects == 0)
  110. hr = S_OK;
  111. else
  112. hr = S_FALSE;
  113. return hr;
  114. }
  115. /****************************************************************************/
  116. // DllRegisterServer
  117. //
  118. // Standard COM entry point for registering the server.
  119. /****************************************************************************/
  120. STDAPI DllRegisterServer(void)
  121. {
  122. wchar_t Path[1024];
  123. wchar_t *pGuidStr = 0;
  124. wchar_t KeyPath[1024];
  125. HRESULT hr = E_FAIL;
  126. // Get the DLL's filename
  127. GetModuleFileNameW(g_hInstance, Path, 1024);
  128. // Convert CLSID to string.
  129. if( SUCCEEDED( StringFromCLSID(IMPLEMENTED_CLSID, &pGuidStr ) ) )
  130. {
  131. swprintf(KeyPath, L"Software\\Classes\\CLSID\\\\%s", pGuidStr);
  132. // Place it in registry.
  133. // CLSID\\CLSID_Nt5PerProvider_v1 : <no_name> : "name"
  134. // \\CLSID_Nt5PerProvider_v1\\InProcServer32 :
  135. // <no_name> : "path to DLL"
  136. // ThreadingModel : "both"
  137. HKEY hKey;
  138. LONG lRes = RegCreateKeyW(HKEY_LOCAL_MACHINE, KeyPath, &hKey);
  139. if (lRes == 0)
  140. {
  141. wchar_t *pName = SERVER_REGISTRY_COMMENT;
  142. RegSetValueExW(hKey, 0, 0, REG_SZ, (const BYTE *)pName,
  143. wcslen(pName) * 2 + 2);
  144. HKEY hSubkey;
  145. lRes = RegCreateKeyW(hKey, L"InprocServer32", &hSubkey);
  146. RegSetValueExW(hSubkey, 0, 0, REG_SZ, (const BYTE *) Path,
  147. wcslen(Path) * 2 + 2);
  148. RegSetValueExW(hSubkey, L"ThreadingModel", 0, REG_SZ,
  149. (const BYTE *) L"Both", wcslen(L"Both") * 2 + 2);
  150. RegCloseKey(hSubkey);
  151. RegCloseKey(hKey);
  152. hr = S_OK;
  153. }
  154. CoTaskMemFree(pGuidStr);
  155. hr = HRESULT_FROM_WIN32( lRes );
  156. }
  157. return hr;
  158. }
  159. /****************************************************************************/
  160. // DllUnregisterServer
  161. //
  162. // Standard COM entry point for unregistering the server.
  163. /****************************************************************************/
  164. STDAPI DllUnregisterServer(void)
  165. {
  166. wchar_t *pGuidStr = 0;
  167. HKEY hKey;
  168. wchar_t KeyPath[256];
  169. HRESULT hr = E_FAIL;
  170. if( SUCCEEDED( StringFromCLSID(IMPLEMENTED_CLSID, &pGuidStr) ) )
  171. {
  172. swprintf(KeyPath, L"Software\\Classes\\CLSID\\\\%s", pGuidStr);
  173. // Delete InProcServer32 subkey.
  174. LONG lRes = RegOpenKeyW(HKEY_LOCAL_MACHINE, KeyPath, &hKey);
  175. if (!lRes) {
  176. RegDeleteKeyW(hKey, L"InprocServer32");
  177. RegCloseKey(hKey);
  178. // Delete CLSID GUID key.
  179. lRes = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Classes\\CLSID", &hKey);
  180. if (!lRes) {
  181. RegDeleteKeyW(hKey, pGuidStr);
  182. RegCloseKey(hKey);
  183. }
  184. }
  185. CoTaskMemFree(pGuidStr);
  186. hr = HRESULT_FROM_WIN32( lRes );
  187. }
  188. return hr;
  189. }