Source code of Windows XP (NT5)
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.

221 lines
4.7 KiB

  1. // ConnMgr.cpp : Implementation of WinMain
  2. // Note: Proxy/Stub Information
  3. // To build a separate proxy/stub DLL,
  4. // run nmake -f ConnMgrps.mk in the project directory.
  5. #include "stdafx.h"
  6. #include "resource.h"
  7. #include "initguid.h"
  8. #include "Connection.h"
  9. #include "ConnMgr.h"
  10. #include "ConnMgr_i.c"
  11. #include "ConnectionManager.h"
  12. class CConnApp : public CWinApp
  13. {
  14. public:
  15. virtual BOOL InitInstance();
  16. virtual int ExitInstance();
  17. BOOL InitCOMSecurity();
  18. };
  19. CConnApp theApp;
  20. LONG CExeModule::Unlock()
  21. {
  22. LONG l = CComModule::Unlock();
  23. if (l == 0)
  24. {
  25. #if _WIN32_WINNT >= 0x0400
  26. if (CoSuspendClassObjects() == S_OK)
  27. PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  28. #else
  29. PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
  30. #endif
  31. }
  32. return l;
  33. }
  34. HRESULT CExeModule::UpdateRegistry(LPCOLESTR lpszRes, BOOL bRegister)
  35. {
  36. USES_CONVERSION;
  37. CComPtr<IRegistrar> p;
  38. HRESULT hRes = CoCreateInstance(CLSID_Registrar, NULL,
  39. CLSCTX_INPROC_SERVER,
  40. IID_IRegistrar, (void**)&p);
  41. if (FAILED(hRes))
  42. {
  43. AfxMessageBox(_T("Failed to create ATL Registrar! Try updated version of ATL.DLL."));
  44. return hRes;
  45. }
  46. _ATL_MODULE* pM = this;
  47. TCHAR szModule[_MAX_PATH];
  48. GetModuleFileName(pM->m_hInst, szModule, _MAX_PATH);
  49. p->AddReplacement(OLESTR("Module"), T2OLE(szModule));
  50. LPCOLESTR szType = OLESTR("REGISTRY");
  51. GetModuleFileName(pM->m_hInstResource, szModule, _MAX_PATH);
  52. LPOLESTR pszModule = T2OLE(szModule);
  53. #ifdef SAVE
  54. if (HIWORD(lpszRes)==0)
  55. {
  56. if (bRegister)
  57. hRes = p->ResourceRegister(pszModule, ((UINT)LOWORD((DWORD)lpszRes)), szType);
  58. else
  59. hRes = p->ResourceUnregister(pszModule, ((UINT)LOWORD((DWORD)lpszRes)), szType);
  60. }
  61. else
  62. {
  63. #endif
  64. if (bRegister)
  65. hRes = p->ResourceRegisterSz(pszModule, lpszRes, szType);
  66. else
  67. hRes = p->ResourceUnregisterSz(pszModule, lpszRes, szType);
  68. #ifdef SAVE
  69. }
  70. #endif
  71. return hRes;
  72. }
  73. CExeModule _Module;
  74. BEGIN_OBJECT_MAP(ObjectMap)
  75. OBJECT_ENTRY(CLSID_ConnectionManager, CConnectionManager)
  76. END_OBJECT_MAP()
  77. LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2)
  78. {
  79. while (*p1 != NULL)
  80. {
  81. LPCTSTR p = p2;
  82. while (*p != NULL)
  83. {
  84. if (*p1 == *p++)
  85. return p1+1;
  86. }
  87. p1++;
  88. }
  89. return NULL;
  90. }
  91. BOOL CConnApp::InitCOMSecurity()
  92. {
  93. HRESULT hRes = CoInitializeSecurity(NULL, -1, NULL, NULL,
  94. RPC_C_AUTHN_LEVEL_CONNECT,
  95. RPC_C_IMP_LEVEL_IMPERSONATE,
  96. NULL,
  97. EOAC_NONE,
  98. NULL);
  99. if (FAILED(hRes))
  100. return false;
  101. return true;
  102. }
  103. BOOL CConnApp::InitInstance()
  104. {
  105. // CG: The following block was added by the Windows Sockets component.
  106. {
  107. if (!AfxSocketInit())
  108. {
  109. AfxMessageBox(CG_IDS_SOCKETS_INIT_FAILED);
  110. return FALSE;
  111. }
  112. }
  113. // Initialize OLE libraries
  114. HRESULT hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);
  115. if (FAILED(hr))
  116. {
  117. AfxMessageBox(_T("OLE Initialization Failed!"));
  118. return FALSE;
  119. }
  120. // Initialize COM Security
  121. if (!InitCOMSecurity())
  122. {
  123. AfxMessageBox(_T("Security Initialization Failed!"));
  124. return FALSE;
  125. }
  126. // Initialize the ATL Module
  127. _Module.Init(ObjectMap,m_hInstance);
  128. _Module.dwThreadID = GetCurrentThreadId();
  129. #ifdef _AFXDLL
  130. Enable3dControls(); // Call this when using MFC in a shared DLL
  131. #else
  132. Enable3dControlsStatic();
  133. #endif
  134. // Check command line arguments
  135. TCHAR szTokens[] = _T("-/");
  136. BOOL bRun = TRUE;
  137. LPCTSTR lpszToken = FindOneOf(m_lpCmdLine, szTokens);
  138. while (lpszToken != NULL)
  139. {
  140. // Register ATL/MFC class factories
  141. if (lstrcmpi(lpszToken, _T("Embedding")) == 0 ||
  142. lstrcmpi(lpszToken, _T("Automation")) == 0)
  143. {
  144. AfxOleSetUserCtrl(FALSE);
  145. break;
  146. }
  147. else if (lstrcmpi(lpszToken, _T("UnregServer")) == 0)
  148. {
  149. _Module.UpdateRegistryFromResource(IDR_ConnMgr, FALSE);
  150. // _Module.UpdateRegistry((LPCOLESTR)MAKEINTRESOURCE(IDR_CONNECTIONMANAGER), false);
  151. _Module.UpdateRegistry(_T("connectionmanager.rgs"), false);
  152. bRun = FALSE;
  153. break;
  154. }
  155. else if (lstrcmpi(lpszToken, _T("RegServer")) == 0)
  156. {
  157. _Module.UpdateRegistryFromResource(IDR_ConnMgr, TRUE);
  158. // _Module.UpdateRegistry((LPCOLESTR)MAKEINTRESOURCE(IDR_CONNECTIONMANAGER), true);
  159. _Module.UpdateRegistry(_T("connectionmanager.rgs"), true);
  160. COleObjectFactory::UpdateRegistryAll();
  161. bRun = FALSE;
  162. break;
  163. }
  164. lpszToken = FindOneOf(lpszToken, szTokens);
  165. }
  166. if (bRun)
  167. {
  168. _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,REGCLS_MULTIPLEUSE);
  169. COleObjectFactory::RegisterAll();
  170. }
  171. return TRUE;
  172. }
  173. int CConnApp::ExitInstance()
  174. {
  175. CoUninitialize();
  176. // MFC's class factories registration is
  177. // automatically revoked by MFC itself
  178. _Module.RevokeClassObjects();
  179. // Revoke class factories for ATL
  180. _Module.Term();
  181. // cleanup ATL Global Module
  182. CoUninitialize();
  183. return CWinApp::ExitInstance();
  184. }