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.

269 lines
7.7 KiB

  1. /****************************************************************************/
  2. // server.cpp
  3. //
  4. // General COM in-proc server framework code. TSSD-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 "factory.h"
  23. #include "trace.h"
  24. #define SDMAX_PATH 1024
  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. // {005a9c68-e216-4b27-8f59-b336829b3868}
  35. DEFINE_GUID(CLSID_TSSDJET,
  36. 0x005a9c68, 0xe216, 0x4b27, 0x8f, 0x59, 0xb3, 0x36, 0x82, 0x9b, 0x38, 0x68);
  37. // {ec98d957-48ad-436d-90be-bc291f42709c}
  38. DEFINE_GUID(CLSID_TSSDJETEX,
  39. 0xec98d957, 0x48ad, 0x436d, 0x90, 0xbe, 0xbc, 0x29, 0x1f, 0x42, 0x70, 0x9c);
  40. #define IMPLEMENTED_CLSID CLSID_TSSDJET
  41. #define IMPLEMENTED_CLSIDEX CLSID_TSSDJETEX
  42. #define SERVER_REGISTRY_COMMENT L"Terminal Server Session Directory Interface"
  43. #define CPP_CLASS_NAME CTSSessionDirectory
  44. #define INTERFACE_CAST (ITSSessionDirectory *)
  45. /****************************************************************************/
  46. // End CLSID SPECIFIC section
  47. /****************************************************************************/
  48. HINSTANCE g_hInstance;
  49. long g_lLocks = 0;
  50. long g_lObjects = 0;
  51. /****************************************************************************/
  52. // DllMain
  53. //
  54. // Standard DLL entry point. Returns FALSE on failure.
  55. /****************************************************************************/
  56. BOOL WINAPI DllMain(
  57. HINSTANCE hInstDLL,
  58. DWORD dwReason,
  59. LPVOID lpReserved)
  60. {
  61. if (dwReason == DLL_PROCESS_ATTACH) {
  62. setlocale(LC_ALL, ""); // Set to the 'current' locale
  63. g_hInstance = hInstDLL;
  64. DisableThreadLibraryCalls(hInstDLL);
  65. }
  66. return TRUE;
  67. }
  68. /****************************************************************************/
  69. // DllGetClassObject
  70. //
  71. // Standard OLE In-Process Server entry point to return an class factory
  72. // instance.
  73. //***************************************************************************
  74. STDAPI DllGetClassObject(
  75. REFCLSID rclsid,
  76. REFIID riid,
  77. LPVOID *ppv)
  78. {
  79. CClassFactory *pClassFactory;
  80. HRESULT hr;
  81. TRC2((TB,"DllGetClassObject"));
  82. // Verify the caller is asking for our type of object
  83. if (rclsid == IMPLEMENTED_CLSID || rclsid == IMPLEMENTED_CLSIDEX) {
  84. // Create the class factory.
  85. pClassFactory = new CClassFactory;
  86. if (pClassFactory != NULL) {
  87. hr = pClassFactory->QueryInterface(riid, ppv);
  88. if (FAILED(hr)) {
  89. ERR((TB,"DllGetClassObject: GUID not found"));
  90. delete pClassFactory;
  91. }
  92. }
  93. else {
  94. ERR((TB,"DllGetClassObject: Failed alloc class factory"));
  95. hr = E_OUTOFMEMORY;
  96. }
  97. }
  98. else {
  99. ERR((TB,"DllGetClassObject: Failed alloc class factory"));
  100. hr = CLASS_E_CLASSNOTAVAILABLE;
  101. }
  102. return hr;
  103. }
  104. /****************************************************************************/
  105. // DllCanUnloadNow
  106. //
  107. // Standard COM entry point for COM server shutdown request. Allows shutdown
  108. // only if no outstanding objects or locks are present.
  109. /****************************************************************************/
  110. STDAPI DllCanUnloadNow(void)
  111. {
  112. HRESULT hr;
  113. if (g_lLocks == 0 && g_lObjects == 0)
  114. hr = S_OK;
  115. else
  116. hr = S_FALSE;
  117. return hr;
  118. }
  119. /****************************************************************************/
  120. // DllRegisterServer
  121. //
  122. // Standard COM entry point for registering the server.
  123. /****************************************************************************/
  124. HRESULT RegisterCLSID(CLSID clsid)
  125. {
  126. HRESULT hr = E_FAIL;
  127. wchar_t *pGuidStr = 0;
  128. wchar_t KeyPath[SDMAX_PATH];
  129. wchar_t Path[SDMAX_PATH];
  130. // Get the DLL's filename
  131. GetModuleFileNameW(g_hInstance, Path, 1024);
  132. Path[SDMAX_PATH - 1] = L'\0';
  133. TRC2((TB,"RegisterCLSID: %S", KeyPath));
  134. // Convert CLSID to string.
  135. if( SUCCEEDED( StringFromCLSID(clsid, &pGuidStr) ) )
  136. {
  137. swprintf(KeyPath, L"Software\\Classes\\CLSID\\\\%s", pGuidStr);
  138. // Place it in registry.
  139. // CLSID\\CLSID_Nt5PerProvider_v1 : <no_name> : "name"
  140. // \\CLSID_Nt5PerProvider_v1\\InProcServer32 :
  141. // <no_name> : "path to DLL"
  142. // ThreadingModel : "both"
  143. HKEY hKey;
  144. LONG lRes = RegCreateKeyW(HKEY_LOCAL_MACHINE, KeyPath, &hKey);
  145. if (lRes == 0) {
  146. wchar_t *pName = SERVER_REGISTRY_COMMENT;
  147. RegSetValueExW(hKey, 0, 0, REG_SZ, (const BYTE *)pName,
  148. wcslen(pName) * 2 + 2);
  149. HKEY hSubkey;
  150. lRes = RegCreateKeyW(hKey, L"InprocServer32", &hSubkey);
  151. RegSetValueExW(hSubkey, 0, 0, REG_SZ, (const BYTE *) Path,
  152. wcslen(Path) * 2 + 2);
  153. RegSetValueExW(hSubkey, L"ThreadingModel", 0, REG_SZ,
  154. (const BYTE *) L"Both", wcslen(L"Both") * 2 + 2);
  155. RegCloseKey(hSubkey);
  156. RegCloseKey(hKey);
  157. }
  158. else {
  159. TRC2((TB,"RegisterCLSID: Failed to Create key: %x", lRes));
  160. }
  161. CoTaskMemFree(pGuidStr);
  162. hr = HRESULT_FROM_WIN32( lRes );
  163. }
  164. else {
  165. TRC2((TB,"RegisterCLSID failed"));
  166. }
  167. return hr;
  168. }
  169. STDAPI DllRegisterServer(void)
  170. {
  171. HRESULT hr = E_FAIL;
  172. hr = RegisterCLSID(IMPLEMENTED_CLSID);
  173. hr = RegisterCLSID(IMPLEMENTED_CLSIDEX);
  174. return hr;
  175. }
  176. /****************************************************************************/
  177. // DllUnregisterServer
  178. //
  179. // Standard COM entry point for unregistering the server.
  180. /****************************************************************************/
  181. HRESULT UnregisterCLSID(REFCLSID rclsid)
  182. {
  183. wchar_t *pGuidStr = 0;
  184. HKEY hKey;
  185. wchar_t KeyPath[256];
  186. HRESULT hr = E_FAIL;
  187. if( SUCCEEDED( StringFromCLSID(rclsid, &pGuidStr) ) )
  188. {
  189. swprintf(KeyPath, L"Software\\Classes\\CLSID\\\\%s", pGuidStr);
  190. // Delete InProcServer32 subkey.
  191. LONG lRes = RegOpenKeyW(HKEY_LOCAL_MACHINE, KeyPath, &hKey);
  192. if (!lRes) {
  193. RegDeleteKeyW(hKey, L"InprocServer32");
  194. RegCloseKey(hKey);
  195. // Delete CLSID GUID key.
  196. lRes = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Classes\\CLSID", &hKey);
  197. if (!lRes) {
  198. RegDeleteKeyW(hKey, pGuidStr);
  199. RegCloseKey(hKey);
  200. }
  201. }
  202. CoTaskMemFree(pGuidStr);
  203. hr = HRESULT_FROM_WIN32( lRes );
  204. }
  205. return hr;
  206. }
  207. STDAPI DllUnregisterServer(void)
  208. {
  209. wchar_t *pGuidStr = 0;
  210. HKEY hKey;
  211. wchar_t KeyPath[256];
  212. HRESULT hr = E_FAIL;
  213. hr = UnregisterCLSID(IMPLEMENTED_CLSID);
  214. hr = UnregisterCLSID(IMPLEMENTED_CLSIDEX);
  215. return hr;
  216. }