home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / cluster / smbsmp / smbsmpex / smbsmpex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-29  |  8.2 KB  |  313 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    Copyright (c) 1997 <company name>
  4. //
  5. //    Module Name:
  6. //        SmbSmpEx.cpp
  7. //
  8. //    Abstract:
  9. //        Implementation of the CSmbSmpApp class and DLL initialization
  10. //        routines.
  11. //
  12. //    Author:
  13. //        <name> (<e-mail name>) Mmmm DD, 1997
  14. //
  15. //    Revision History:
  16. //
  17. //    Notes:
  18. //        NOTE: You must use the MIDL compiler from NT 4.0,
  19. //        version 3.00.44 or greater
  20. //
  21. /////////////////////////////////////////////////////////////////////////////
  22.  
  23. #include "stdafx.h"
  24. #include <initguid.h>
  25. #include <CluAdmEx.h>
  26. #include "SmbSmpEx.h"
  27. #include "ExtObj.h"
  28. #include "BasePage.h"
  29. #include "RegExt.h"
  30.  
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36.  
  37. #define IID_DEFINED
  38. #include "ExtObjID_i.c"
  39.  
  40. CComModule _Module;
  41.  
  42. #pragma warning(disable : 4701) // local variable may be used without having been initialized
  43. #include <atlimpl.cpp>
  44. #pragma warning(default : 4701)
  45.  
  46. BEGIN_OBJECT_MAP(ObjectMap)
  47.     OBJECT_ENTRY(CLSID_CoSmbSmpEx, CExtObject)
  48. END_OBJECT_MAP()
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // Global Function Prototypes
  52. /////////////////////////////////////////////////////////////////////////////
  53.  
  54. STDAPI DllCanUnloadNow(void);
  55. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
  56. STDAPI DllRegisterServer(void);
  57. STDAPI DllUnregisterServer(void);
  58. STDAPI DllRegisterCluAdminExtension(IN HCLUSTER hcluster);
  59. STDAPI DllUnregisterCluAdminExtension(IN HCLUSTER hcluster);
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // class CSmbSmpApp
  63. /////////////////////////////////////////////////////////////////////////////
  64.  
  65. class CSmbSmpApp : public CWinApp
  66. {
  67. public:
  68.     virtual BOOL InitInstance();
  69.     virtual int ExitInstance();
  70. };
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // The one and only CSmbSmpApp object
  74.  
  75. CSmbSmpApp theApp;
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. //++
  79. //
  80. //    CSmbSmpApp::InitInstance
  81. //
  82. //    Routine Description:
  83. //        Initialize this instance of the application.
  84. //
  85. //    Arguments:
  86. //        None.
  87. //
  88. //    Return Value:
  89. //        Any return codes from CWinApp::InitInstance().
  90. //
  91. //--
  92. /////////////////////////////////////////////////////////////////////////////
  93. BOOL CSmbSmpApp::InitInstance(void)
  94. {
  95.     _Module.Init(ObjectMap, m_hInstance);
  96.     return CWinApp::InitInstance();
  97.  
  98. }  //*** CSmbSmpApp::InitInstance()
  99.  
  100. /////////////////////////////////////////////////////////////////////////////
  101. //++
  102. //
  103. //    CSmbSmpApp::ExitInstance
  104. //
  105. //    Routine Description:
  106. //        Deinitialize this instance of the application.
  107. //
  108. //    Arguments:
  109. //        None.
  110. //
  111. //    Return Value:
  112. //        Any return codes from CWinApp::ExitInstance().
  113. //
  114. //--
  115. /////////////////////////////////////////////////////////////////////////////
  116. int CSmbSmpApp::ExitInstance(void)
  117. {
  118.     _Module.Term();
  119.     return CWinApp::ExitInstance();
  120.  
  121. }  //*** CSmbSmpApp::ExitInstance()
  122.  
  123. /////////////////////////////////////////////////////////////////////////////
  124. //++
  125. //
  126. //    FormatError
  127. //
  128. //    Routine Description:
  129. //        Format an error.
  130. //
  131. //    Arguments:
  132. //        rstrError    [OUT] String in which to return the error message.
  133. //        dwError        [IN] Error code to format.
  134. //
  135. //    Return Value:
  136. //        None.
  137. //
  138. //--
  139. /////////////////////////////////////////////////////////////////////////////
  140. void FormatError(CString & rstrError, DWORD dwError)
  141. {
  142.     DWORD        dwResult;
  143.     TCHAR        szError[256];
  144.  
  145.     dwResult = ::FormatMessage(
  146.                     FORMAT_MESSAGE_FROM_SYSTEM,
  147.                     NULL,
  148.                     dwError,
  149.                     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  150.                     szError,
  151.                     sizeof(szError) / sizeof(TCHAR),
  152.                     0
  153.                     );
  154.     if (dwResult == 0)
  155.     {
  156.         // Format the NT status code from CLUSAPI since some of these haven't
  157.         // been integrated into the system yet.
  158.         dwResult = ::FormatMessage(
  159.                         FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
  160.                         ::GetModuleHandle(_T("CLUSAPI.DLL")),
  161.                         dwError,
  162.                         MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  163.                         szError,
  164.                         sizeof(szError) / sizeof(TCHAR),
  165.                         0
  166.                         );
  167.         if (dwResult == 0)
  168.         {
  169.             // Format the NT status code from NTDLL since this hasn't been
  170.             // integrated into the system yet.
  171.             dwResult = ::FormatMessage(
  172.                             FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
  173.                             ::GetModuleHandle(_T("NTDLL.DLL")),
  174.                             dwError,
  175.                             MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  176.                             szError,
  177.                             sizeof(szError) / sizeof(TCHAR),
  178.                             0
  179.                             );
  180.         }  // if:  error formatting status code from CLUSAPI
  181.     }  // if:  error formatting status code from system
  182.  
  183.     if (dwResult != 0)
  184.         rstrError = szError;
  185.     else
  186.     {
  187.         dwResult = ::GetLastError();
  188.         TRACE(_T("FormatError() - Error 0x%08.8x formatting string for error code 0x%08.8x\n"), dwResult, dwError);
  189.         rstrError.Format(_T("Error 0x%08.8x"));
  190.     }  // else:  error formatting the message
  191.  
  192. }  //*** FormatError()
  193.  
  194. /////////////////////////////////////////////////////////////////////////////
  195. // Used to determine whether the DLL can be unloaded by OLE
  196.  
  197. STDAPI DllCanUnloadNow(void)
  198. {
  199.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  200.     return (AfxDllCanUnloadNow() && _Module.GetLockCount()==0) ? S_OK : S_FALSE;
  201.  
  202. }  //*** DllCanUnloadNow()
  203.  
  204. /////////////////////////////////////////////////////////////////////////////
  205. // Returns a class factory to create an object of the requested type
  206.  
  207. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  208. {
  209.     return _Module.GetClassObject(rclsid, riid, ppv);
  210.  
  211. }  //*** DllGetClassObject()
  212.  
  213. /////////////////////////////////////////////////////////////////////////////
  214. // DllRegisterServer - Adds entries to the system registry
  215.  
  216. STDAPI DllRegisterServer(void)
  217. {
  218.     HRESULT hRes = S_OK;
  219.     // registers object, typelib and all interfaces in typelib
  220.     hRes = _Module.RegisterServer(FALSE /*bRegTypeLib*/);
  221.     return hRes;
  222.  
  223. }  //*** DllRegisterServer()
  224.  
  225. /////////////////////////////////////////////////////////////////////////////
  226. // DllUnregisterServer - Removes entries from the system registry
  227.  
  228. STDAPI DllUnregisterServer(void)
  229. {
  230.     HRESULT hRes = S_OK;
  231.     _Module.UnregisterServer();
  232.     return hRes;
  233.  
  234. }  //*** DllUnregisterServer()
  235.  
  236. /////////////////////////////////////////////////////////////////////////////
  237. //++
  238. //
  239. //    DllRegisterCluAdminExtension
  240. //
  241. //    Routine Description:
  242. //        Register the extension with the cluster database.
  243. //
  244. //    Arguments:
  245. //        hCluster        [IN] Handle to the cluster to modify.
  246. //
  247. //    Return Value:
  248. //        S_OK            Extension registered successfully.
  249. //        Win32 error code if another failure occurred.
  250. //
  251. //--
  252. /////////////////////////////////////////////////////////////////////////////
  253. STDAPI DllRegisterCluAdminExtension(IN HCLUSTER hCluster)
  254. {
  255.     HRESULT        hr = S_OK;
  256.     LPCWSTR        pwszResTypes = g_wszResourceTypeNames;
  257.  
  258.     while (*pwszResTypes != L'\0')
  259.     {
  260.         wprintf(L"  %s\n", pwszResTypes);
  261.         hr = RegisterCluAdminResourceTypeExtension(
  262.                     hCluster,
  263.                     pwszResTypes,
  264.                     &CLSID_CoSmbSmpEx
  265.                     );
  266.         if (hr != S_OK)
  267.             break;
  268.         pwszResTypes += lstrlenW(pwszResTypes) + 1;
  269.     }  // while:  more resource types
  270.  
  271.     return hr;
  272.  
  273. }  //*** DllRegisterCluAdminExtension()
  274.  
  275. /////////////////////////////////////////////////////////////////////////////
  276. //++
  277. //
  278. //    DllUnregisterCluAdminExtension
  279. //
  280. //    Routine Description:
  281. //        Unregister the extension with the cluster database.
  282. //
  283. //    Arguments:
  284. //        hCluster        [IN] Handle to the cluster to modify.
  285. //
  286. //    Return Value:
  287. //        S_OK            Extension unregistered successfully.
  288. //        Win32 error code if another failure occurred.
  289. //
  290. //--
  291. /////////////////////////////////////////////////////////////////////////////
  292. STDAPI DllUnregisterCluAdminExtension(IN HCLUSTER hCluster)
  293. {
  294.     HRESULT        hr = S_OK;
  295.     LPCWSTR        pwszResTypes = g_wszResourceTypeNames;
  296.  
  297.     while (*pwszResTypes != L'\0')
  298.     {
  299.         wprintf(L"  %s\n", pwszResTypes);
  300.         hr = UnregisterCluAdminResourceTypeExtension(
  301.                     hCluster,
  302.                     pwszResTypes,
  303.                     &CLSID_CoSmbSmpEx
  304.                     );
  305.         if (hr != S_OK)
  306.             break;
  307.         pwszResTypes += lstrlenW(pwszResTypes) + 1;
  308.     }  // while:  more resource types
  309.  
  310.     return hr;
  311.  
  312. }  //*** DllUnregisterCluAdminExtension()
  313.