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.

465 lines
12 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. winssnap.cpp
  7. WINS snapin entry points/registration functions
  8. Note: Proxy/Stub Information
  9. To build a separate proxy/stub DLL,
  10. run nmake -f Snapinps.mak in the project directory.
  11. FILE HISTORY:
  12. */
  13. #include "stdafx.h"
  14. #include "initguid.h"
  15. #include "winscomp.h"
  16. #include "winssnap.h"
  17. #include "ncglobal.h" // network console global defines
  18. #include "cmptrmgr.h" // computer management snapin node types
  19. #include "locale.h" // for setlocale
  20. #include <lmerr.h> // for NERR stuff
  21. #ifdef _DEBUG
  22. void DbgVerifyInstanceCounts();
  23. #define DEBUG_VERIFY_INSTANCE_COUNTS DbgVerifyInstanceCounts()
  24. #else
  25. #define DEBUG_VERIFY_INSTANCE_COUNTS
  26. #endif
  27. CComModule _Module;
  28. BEGIN_OBJECT_MAP(ObjectMap)
  29. OBJECT_ENTRY(CLSID_WinsSnapin, CWinsComponentDataPrimary)
  30. OBJECT_ENTRY(CLSID_WinsSnapinExtension, CWinsComponentDataExtension)
  31. OBJECT_ENTRY(CLSID_WinsSnapinAbout, CWinsAbout)
  32. END_OBJECT_MAP()
  33. #ifdef _DEBUG
  34. #define new DEBUG_NEW
  35. #undef THIS_FILE
  36. static char THIS_FILE[] = __FILE__;
  37. #endif
  38. CWinsSnapinApp theApp;
  39. BOOL CWinsSnapinApp::InitInstance()
  40. {
  41. _Module.Init(ObjectMap, m_hInstance);
  42. SHFusionInitializeFromModuleID (m_hInstance, 2);
  43. //
  44. // Initialize the CWndIpAddress control window class IPADDRESS
  45. //
  46. CWndIpAddress::CreateWindowClass( m_hInstance ) ;
  47. // set the default locale to the system locale
  48. setlocale(LC_ALL, "");
  49. //
  50. // Initialize use of the WinSock routines
  51. //
  52. WSADATA wsaData ;
  53. if ( ::WSAStartup( MAKEWORD( 1, 1 ), & wsaData ) != 0 )
  54. {
  55. m_bWinsockInited = TRUE;
  56. Trace0("InitInstance: Winsock initialized!\n");
  57. }
  58. else
  59. {
  60. m_bWinsockInited = FALSE;
  61. }
  62. ::IPAddrInit(m_hInstance);
  63. return CWinApp::InitInstance();
  64. }
  65. int CWinsSnapinApp::ExitInstance()
  66. {
  67. SHFusionUninitialize();
  68. _Module.Term();
  69. DEBUG_VERIFY_INSTANCE_COUNTS;
  70. //
  71. // Terminate use of the WinSock routines.
  72. //
  73. if ( m_bWinsockInited )
  74. {
  75. WSACleanup() ;
  76. }
  77. return CWinApp::ExitInstance();
  78. }
  79. /***
  80. *
  81. * CWinsadmnApp::GetSystemMessage
  82. *
  83. * Purpose:
  84. *
  85. * Given a message ID, determine where the message resides,
  86. * and load it into the buffer.
  87. *
  88. * Arguments:
  89. *
  90. * UINT nId Message ID number
  91. * char * chBuffer Character buffer to load into.
  92. * int cbBuffSize Size of buffer in characters
  93. *
  94. * Returns:
  95. *
  96. * API error return code, or ERROR_SUCCESS
  97. *
  98. */
  99. DWORD
  100. CWinsSnapinApp::GetSystemMessage(
  101. UINT nId,
  102. TCHAR * chBuffer,
  103. int cbBuffSize
  104. )
  105. {
  106. TCHAR * pszText = NULL ;
  107. HINSTANCE hdll = NULL ;
  108. DWORD flags = FORMAT_MESSAGE_IGNORE_INSERTS
  109. | FORMAT_MESSAGE_MAX_WIDTH_MASK;
  110. //
  111. // Interpret the error. Need to special case
  112. // the lmerr & ntstatus ranges.
  113. //
  114. if( nId >= NERR_BASE && nId <= MAX_NERR )
  115. {
  116. hdll = ::LoadLibrary( _T("netmsg.dll") );
  117. }
  118. else if( nId >= 0x40000000L )
  119. {
  120. hdll = ::LoadLibrary( _T("ntdll.dll") );
  121. }
  122. if( hdll == NULL )
  123. {
  124. flags |= FORMAT_MESSAGE_FROM_SYSTEM;
  125. }
  126. else
  127. {
  128. flags |= FORMAT_MESSAGE_FROM_HMODULE;
  129. }
  130. DWORD dwResult = ::FormatMessage( flags,
  131. (LPVOID) hdll,
  132. nId,
  133. 0,
  134. chBuffer,
  135. cbBuffSize,
  136. NULL );
  137. if( hdll != NULL )
  138. {
  139. LONG err = ::GetLastError();
  140. ::FreeLibrary( hdll );
  141. if ( dwResult == 0 )
  142. {
  143. ::SetLastError( err );
  144. }
  145. }
  146. return dwResult ? ERROR_SUCCESS : ::GetLastError();
  147. }
  148. /***
  149. *
  150. * CWinsadmnApp::MessageBox
  151. *
  152. * Purpose:
  153. *
  154. * Replacement for AfxMessageBox(). This function will call up the
  155. * appropriate message from wherever before displaying it
  156. *
  157. * Arguments:
  158. *
  159. * UINT nIdPrompt Message ID
  160. * UINT nType AfxMessageBox type (YESNO, OKCANCEL, etc)
  161. * UINT nHelpContext Help context ID for AfxMessageBox();
  162. *
  163. * Notes:
  164. *
  165. * If an error occurs, a standard message (hard-coded in english) will
  166. * be shown that gives the error number.
  167. *
  168. */
  169. int
  170. CWinsSnapinApp::MessageBox (
  171. UINT nIdPrompt,
  172. UINT nType,
  173. UINT nHelpContext
  174. )
  175. {
  176. CThemeContextActivator themeActivator;
  177. //
  178. // Substitute a friendly message for "RPC server not
  179. // available" and "No more endpoints available from
  180. // the endpoint mapper".
  181. //
  182. if (nIdPrompt == EPT_S_NOT_REGISTERED ||
  183. nIdPrompt == RPC_S_SERVER_UNAVAILABLE)
  184. {
  185. nIdPrompt = IDS_ERR_WINS_DOWN;
  186. }
  187. //
  188. // If it's our error, the text is in our resource segment.
  189. // Otherwise, use FormatMessage() and the appropriate DLL>
  190. //
  191. if ((nIdPrompt >= IDS_ERR_INVALID_IP) && (nIdPrompt <= IDS_MSG_LAST))
  192. {
  193. return ::AfxMessageBox(nIdPrompt, nType, nHelpContext);
  194. }
  195. TCHAR szMesg [1024] ;
  196. int nResult;
  197. if ((nResult = GetSystemMessage(nIdPrompt, szMesg, sizeof(szMesg)/sizeof(TCHAR)))
  198. == ERROR_SUCCESS)
  199. {
  200. return ::AfxMessageBox(szMesg, nType, nHelpContext);
  201. }
  202. Trace1("Message number %d not found", nIdPrompt);
  203. ASSERT(0 && "Error Message ID not handled");
  204. //
  205. // Do something for the retail version
  206. //
  207. ::wsprintf ( szMesg, _T("Error: %lu"), nIdPrompt);
  208. ::AfxMessageBox(szMesg, nType, nHelpContext);
  209. return nResult;
  210. }
  211. int
  212. CWinsSnapinApp::MessageBox (
  213. LPCTSTR pPrefixText,
  214. UINT nIdPrompt,
  215. UINT nType,
  216. UINT nHelpContext
  217. )
  218. {
  219. CThemeContextActivator themeActivator;
  220. CString strText = pPrefixText;
  221. CString strAppend;
  222. //
  223. // Substitute a friendly message for "RPC server not
  224. // available" and "No more endpoints available from
  225. // the endpoint mapper".
  226. //
  227. if (nIdPrompt == EPT_S_NOT_REGISTERED ||
  228. nIdPrompt == RPC_S_SERVER_UNAVAILABLE)
  229. {
  230. nIdPrompt = IDS_ERR_WINS_DOWN;
  231. }
  232. //
  233. // If it's our error, the text is in our resource segment.
  234. // Otherwise, use FormatMessage() and the appropriate DLL>
  235. //
  236. if ((nIdPrompt >= IDS_ERR_BASE) && (nIdPrompt <= IDS_MSG_LAST))
  237. {
  238. strAppend.LoadString(nIdPrompt);
  239. strText += strAppend;
  240. return ::AfxMessageBox(strText, nType, nHelpContext);
  241. }
  242. TCHAR szMesg [1024] ;
  243. int nResult;
  244. if ((nResult = GetSystemMessage(nIdPrompt, szMesg, sizeof(szMesg)/sizeof(TCHAR)))
  245. == ERROR_SUCCESS)
  246. {
  247. strText += szMesg;
  248. return ::AfxMessageBox(strText, nType, nHelpContext);
  249. }
  250. Trace1("Message number %d not found", nIdPrompt);
  251. ASSERT(0 && "Error Message ID not handled");
  252. //
  253. // Do something for the retail version
  254. //
  255. ::wsprintf ( szMesg, _T("Error: %lu"), nIdPrompt);
  256. strText += szMesg;
  257. ::AfxMessageBox(strText, nType, nHelpContext);
  258. return nResult;
  259. }
  260. /////////////////////////////////////////////////////////////////////////////
  261. // Used to determine whether the DLL can be unloaded by OLE
  262. STDAPI DllCanUnloadNow(void)
  263. {
  264. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  265. return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  266. }
  267. /////////////////////////////////////////////////////////////////////////////
  268. // Returns a class factory to create an object of the requested type
  269. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  270. {
  271. return _Module.GetClassObject(rclsid, riid, ppv);
  272. }
  273. /////////////////////////////////////////////////////////////////////////////
  274. // DllRegisterServer - Adds entries to the system registry
  275. STDAPI DllRegisterServer(void)
  276. {
  277. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  278. //
  279. // registers object, typelib and all interfaces in typelib
  280. //
  281. HRESULT hr = _Module.RegisterServer(/* bRegTypeLib */ FALSE);
  282. ASSERT(SUCCEEDED(hr));
  283. if (FAILED(hr))
  284. return hr;
  285. CString strDesc, strExtDesc, strRootDesc, strVersion;
  286. strDesc.LoadString(IDS_SNAPIN_DESC);
  287. strExtDesc.LoadString(IDS_SNAPIN_EXTENSION_DESC);
  288. strRootDesc.LoadString(IDS_ROOT_DESC);
  289. strVersion.LoadString(IDS_ABOUT_VERSION);
  290. //
  291. // register the snapin into the console snapin list
  292. //
  293. hr = RegisterSnapinGUID(&CLSID_WinsSnapin,
  294. &GUID_WinsRootNodeType,
  295. &CLSID_WinsSnapinAbout,
  296. strDesc,
  297. strVersion,
  298. TRUE);
  299. ASSERT(SUCCEEDED(hr));
  300. if (FAILED(hr))
  301. return hr;
  302. hr = RegisterSnapinGUID(&CLSID_WinsSnapinExtension,
  303. NULL,
  304. &CLSID_WinsSnapinAbout,
  305. strExtDesc,
  306. strVersion,
  307. FALSE);
  308. ASSERT(SUCCEEDED(hr));
  309. if (FAILED(hr))
  310. return hr;
  311. //
  312. // register the snapin nodes into the console node list
  313. //
  314. hr = RegisterNodeTypeGUID(&CLSID_WinsSnapin,
  315. &GUID_WinsRootNodeType,
  316. strRootDesc);
  317. ASSERT(SUCCEEDED(hr));
  318. #ifdef __NETWORK_CONSOLE__
  319. hr = RegisterAsRequiredExtensionGUID(&GUID_NetConsRootNodeType,
  320. &CLSID_WinsSnapinExtension,
  321. strExtDesc,
  322. EXTENSION_TYPE_TASK | EXTENSION_TYPE_NAMESPACE,
  323. &CLSID_WinsSnapinExtension); // the value doesn't matter,
  324. // just needs to be non-null
  325. ASSERT(SUCCEEDED(hr));
  326. #endif
  327. hr = RegisterAsRequiredExtensionGUID(&NODETYPE_COMPUTERMANAGEMENT_SERVERAPPS,
  328. &CLSID_WinsSnapinExtension,
  329. strExtDesc,
  330. EXTENSION_TYPE_TASK | EXTENSION_TYPE_NAMESPACE,
  331. &NODETYPE_COMPUTERMANAGEMENT_SERVERAPPS); // NULL : not dynamic
  332. ASSERT(SUCCEEDED(hr));
  333. return hr;
  334. }
  335. /////////////////////////////////////////////////////////////////////////////
  336. // DllUnregisterServer - Removes entries from the system registry
  337. STDAPI DllUnregisterServer(void)
  338. {
  339. HRESULT hr = _Module.UnregisterServer();
  340. ASSERT(SUCCEEDED(hr));
  341. if (FAILED(hr))
  342. return hr;
  343. // un register the snapin
  344. //
  345. hr = UnregisterSnapinGUID(&CLSID_WinsSnapin);
  346. ASSERT(SUCCEEDED(hr));
  347. if (FAILED(hr))
  348. return hr;
  349. hr = UnregisterSnapinGUID(&CLSID_WinsSnapinExtension);
  350. if (FAILED(hr))
  351. return hr;
  352. // unregister the snapin nodes
  353. //
  354. hr = UnregisterNodeTypeGUID(&GUID_WinsRootNodeType);
  355. ASSERT(SUCCEEDED(hr));
  356. if (FAILED(hr))
  357. return hr;
  358. #ifdef __NETWORK_CONSOLE__
  359. hr = UnregisterAsRequiredExtensionGUID(&GUID_NetConsRootNodeType,
  360. &CLSID_WinsSnapinExtension,
  361. EXTENSION_TYPE_TASK | EXTENSION_TYPE_NAMESPACE,
  362. &CLSID_WinsSnapinExtension);
  363. ASSERT(SUCCEEDED(hr));
  364. #endif
  365. // computer management snapin extension
  366. hr = UnregisterAsRequiredExtensionGUID(&NODETYPE_COMPUTERMANAGEMENT_SERVERAPPS,
  367. &CLSID_WinsSnapinExtension,
  368. EXTENSION_TYPE_TASK | EXTENSION_TYPE_NAMESPACE,
  369. &CLSID_WinsSnapinExtension);
  370. ASSERT(SUCCEEDED(hr));
  371. if (FAILED(hr))
  372. return hr;
  373. return hr;
  374. }
  375. #ifdef _DEBUG
  376. void DbgVerifyInstanceCounts()
  377. {
  378. DEBUG_VERIFY_INSTANCE_COUNT(CHandler);
  379. DEBUG_VERIFY_INSTANCE_COUNT(CMTHandler);
  380. }
  381. #endif // _DEBUG