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.

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