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.

224 lines
6.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: D L L M A I N . C P P
  7. //
  8. // Contents: DLL entry points for netman.dll
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 3 Apr 1998
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "nmbase.h"
  18. #include "nmres.h"
  19. #include "ncreg.h"
  20. #include "nceh.h"
  21. #define INITGUID
  22. #include "nmclsid.h"
  23. #include "..\conman\conman.h"
  24. //+---------------------------------------------------------------------------
  25. // DLL Entry Point
  26. //
  27. EXTERN_C
  28. BOOL
  29. WINAPI
  30. DllMain (
  31. IN HINSTANCE hinst,
  32. IN DWORD dwReason,
  33. IN WACKYAPI LPVOID pvReserved)
  34. {
  35. if (dwReason == DLL_PROCESS_ATTACH)
  36. {
  37. #ifndef DBG
  38. DisableThreadLibraryCalls (hinst);
  39. #endif
  40. EnableCPPExceptionHandling();
  41. InitializeDebugging();
  42. _Module.DllProcessAttach (hinst);
  43. }
  44. else if (dwReason == DLL_PROCESS_DETACH)
  45. {
  46. DbgCheckPrematureDllUnload ("netman.dll", _Module.GetLockCount());
  47. _Module.DllProcessDetach ();
  48. UnInitializeDebugging();
  49. DisableCPPExceptionHandling(); // Disable translation of SEH exceptions into CPP exceptions.
  50. }
  51. #ifdef DBG
  52. else if (dwReason == DLL_THREAD_DETACH)
  53. {
  54. CTracingIndent::FreeThreadInfo();
  55. }
  56. #endif
  57. return TRUE;
  58. }
  59. //+---------------------------------------------------------------------------
  60. // ServiceMain - Called by the generic service process when starting
  61. // this service.
  62. //
  63. // type of LPSERVICE_MAIN_FUNCTIONW
  64. //
  65. EXTERN_C
  66. VOID
  67. WINAPI
  68. ServiceMain (
  69. DWORD argc,
  70. LPCWSTR argv[])
  71. {
  72. _Module.ServiceMain (argc, argv);
  73. }
  74. //SD for netman
  75. const BYTE sdNetmanLaunchPermission[] = {
  76. 01,0x00,0x04,0x80,0x70,0x00,0x00,0x00,0x8c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x00,
  77. 00,0x00,0x02,0x00,0x5c,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x01,0x00,0x00,0x00,0x01,0x02,0x00,0x00,0x00,0x00,0x00,
  78. 05,0x20,0x00,0x00,0x00,0x20,0x02,0x00,0x00,0x00,0x00,0x14,0x00,0x01,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x05,
  79. 04,0x00,0x00,0x00,0x00,0x00,0x14,0x00,0x01,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x05,0x14,0x00,0x00,0x00,0x00,
  80. 00,0x14,0x00,0x01,0x00,0x00,0x00,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x05,0x12,0x00,0x00,0x00,0x01,0x05,0x00,0x00,0x00,0x00,
  81. 00,0x05,0x15,0x00,0x00,0x00,0xa0,0x5f,0x84,0x1f,0x5e,0x2e,0x6b,0x49,0xce,0x12,0x03,0x03,0xf4,0x01,0x00,0x00,0x01,0x05,0x00,
  82. 00,0x00,0x00,0x00,0x05,0x15,0x00,0x00,0x00,0xa0,0x5f,0x84,0x1f,0x5e,0x2e,0x6b,0x49,0xce,0x12,0x03,0x03,0xf4,0x01,0x00,0x00
  83. };
  84. const static WCHAR c_szNetmanAppId[] = L"AppId\\{27AF75ED-20D9-11D1-B1CE-00805FC1270E}";
  85. const static WCHAR c_szLaunchPermission[] = L"LaunchPermission";
  86. //+---------------------------------------------------------------------------
  87. // DllRegisterServer - Adds entries to the system registry
  88. //
  89. STDAPI
  90. DllRegisterServer ()
  91. {
  92. BOOL fCoUninitialize = TRUE;
  93. HRESULT hr = CoInitializeEx (NULL,
  94. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
  95. if (FAILED(hr))
  96. {
  97. fCoUninitialize = FALSE;
  98. if (RPC_E_CHANGED_MODE == hr)
  99. {
  100. hr = S_OK;
  101. }
  102. }
  103. if (SUCCEEDED(hr))
  104. {
  105. hr = _Module.UpdateRegistryFromResource (IDR_NETMAN, TRUE);
  106. if (SUCCEEDED(hr))
  107. {
  108. HKEY hkNetmanAppId = NULL;
  109. hr = HrRegOpenKeyEx(HKEY_CLASSES_ROOT, c_szNetmanAppId, KEY_WRITE, &hkNetmanAppId);
  110. if (SUCCEEDED(hr))
  111. {
  112. HrRegSetBinary(hkNetmanAppId, c_szLaunchPermission, sdNetmanLaunchPermission, sizeof(sdNetmanLaunchPermission));
  113. if (SUCCEEDED(hr))
  114. {
  115. hr = NcAtlModuleRegisterServer (&_Module);
  116. if (fCoUninitialize)
  117. {
  118. CoUninitialize ();
  119. }
  120. }
  121. RegCloseKey(hkNetmanAppId);
  122. }
  123. }
  124. }
  125. TraceHr (ttidError, FAL, hr, FALSE, "netman!DllRegisterServer");
  126. return hr;
  127. }
  128. //+---------------------------------------------------------------------------
  129. // DllUnregisterServer - Removes entries from the system registry
  130. //
  131. STDAPI
  132. DllUnregisterServer ()
  133. {
  134. _Module.UpdateRegistryFromResource (IDR_NETMAN, FALSE);
  135. _Module.UnregisterServer ();
  136. return S_OK;
  137. }
  138. HRESULT
  139. GetClientAdvises(OUT LPWSTR** pppszAdviseUsers, OUT LPDWORD pdwCount)
  140. {
  141. HRESULT hr = S_OK;
  142. if ( (!pppszAdviseUsers) || (!pdwCount) )
  143. {
  144. return E_POINTER;
  145. }
  146. CConnectionManager *pConMan = const_cast<CConnectionManager*>(CConnectionManager::g_pConMan);
  147. if (!pConMan)
  148. {
  149. return E_UNEXPECTED;
  150. }
  151. DWORD dwTotalLength = 0;
  152. DWORD dwNumItems = 0;
  153. list<tstring> NameList;
  154. pConMan->g_fInUse = TRUE;
  155. pConMan->Lock();
  156. for (ITERUSERNOTIFYMAP iter = pConMan->m_mapNotify.begin(); iter != pConMan->m_mapNotify.end(); iter++)
  157. {
  158. tstring szName = iter->second->szUserName;
  159. NameList.push_back(szName);
  160. dwTotalLength += sizeof(WCHAR) * (szName.length() + 1);
  161. dwNumItems++;
  162. }
  163. pConMan->Unlock();
  164. pConMan->g_fInUse = FALSE;
  165. if (!dwNumItems)
  166. {
  167. *pppszAdviseUsers = NULL;
  168. *pdwCount = 0;
  169. return S_FALSE;
  170. }
  171. DWORD dwAllocSize = dwNumItems * sizeof(LPCWSTR) + dwTotalLength;
  172. *pppszAdviseUsers = reinterpret_cast<LPWSTR *>(CoTaskMemAlloc(dwAllocSize));
  173. if (!*pppszAdviseUsers)
  174. {
  175. return E_OUTOFMEMORY;
  176. }
  177. LPWSTR pszEndString = reinterpret_cast<LPWSTR>(reinterpret_cast<LPBYTE>(*pppszAdviseUsers) + dwAllocSize);
  178. // First string in the structure
  179. LPWSTR pszCurrentString = reinterpret_cast<LPWSTR>(reinterpret_cast<LPBYTE>(*pppszAdviseUsers) + (sizeof(LPWSTR) * dwNumItems));
  180. // First pointer in the structure
  181. LPWSTR* lppArray = *pppszAdviseUsers;
  182. for (list<tstring>::const_iterator iterName = NameList.begin(); iterName != NameList.end(); iterName++)
  183. {
  184. *lppArray = pszCurrentString;
  185. wcscpy(pszCurrentString, iterName->c_str());
  186. lppArray++;
  187. pszCurrentString += (iterName->size() + 1);
  188. Assert(pszCurrentString <= pszEndString);
  189. }
  190. *pdwCount = dwNumItems;
  191. return hr;
  192. }