/*--------------------------------------------------------------------------- File: EADCTAgent.cpp Comments: Implementation of DCT Agent COM server, mostly generated by ATL. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved Proprietary and confidential to Mission Critical Software, Inc. REVISION LOG ENTRY Revision By: Christy Boles Revised on 02/18/99 11:34:16 --------------------------------------------------------------------------- */ // Note: Proxy/Stub Information // To build a separate proxy/stub DLL, // run nmake -f EADCTAgentps.mk in the project directory. #include "stdafx.h" #include "resource.h" #include #include //#include "McsEaDctAgent.h" #include "Engine.h" //#include "McsEaDctAgent_i.c" #include "Engine_i.c" #include #include "DCTAgent.h" #include #include CExeModule _Module; BEGIN_OBJECT_MAP(ObjectMap) OBJECT_ENTRY(CLSID_DCTAgent, CDCTAgent) END_OBJECT_MAP() const DWORD dwTimeOut = 5000; // time for EXE to be idle before shutting down const DWORD dwPause = 1000; // time to wait for threads to finish up // function prototypes for DCOM initialization functions we'll try to load dynamically /*#ifndef OFA // here are some definitions we need, but that are only defined when _WIN32_WINNT is 0x400 and up typedef struct tagSOLE_AUTHENTICATION_SERVICE { DWORD dwAuthnSvc; DWORD dwAuthzSvc; OLECHAR __RPC_FAR *pPrincipalName; HRESULT hr; } SOLE_AUTHENTICATION_SERVICE; typedef SOLE_AUTHENTICATION_SERVICE __RPC_FAR *PSOLE_AUTHENTICATION_SERVICE; typedef enum tagEOLE_AUTHENTICATION_CAPABILITIES { EOAC_NONE = 0, EOAC_MUTUAL_AUTH = 0x1, EOAC_CLOAKING = 0x10, EOAC_SECURE_REFS = 0x2, EOAC_ACCESS_CONTROL = 0x4, EOAC_APPID = 0x8 } EOLE_AUTHENTICATION_CAPABILITIES; #endif*/ typedef HRESULT STDAPICALLTYPE COINITIALIZEEX (LPVOID,DWORD); typedef HRESULT STDAPICALLTYPE COINITIALIZESECURITY (PSECURITY_DESCRIPTOR,LONG,SOLE_AUTHENTICATION_SERVICE *, void*,DWORD,DWORD,void*,DWORD,void*); // Passed to CreateThread to monitor the shutdown event static DWORD WINAPI MonitorProc(void* pv) { CExeModule* p = (CExeModule*)pv; p->MonitorShutdown(); return 0; } LONG CExeModule::Unlock() { LONG l = CComModule::Unlock(); if (l == 0) { bActivity = true; SetEvent(hEventShutdown); // tell monitor that we transitioned to zero } return l; } //Monitors the shutdown event void CExeModule::MonitorShutdown() { while (1) { WaitForSingleObject(hEventShutdown, INFINITE); DWORD dwWait=0; do { bActivity = false; dwWait = WaitForSingleObject(hEventShutdown, dwTimeOut); } while (dwWait == WAIT_OBJECT_0); // timed out if (!bActivity && m_nLockCnt == 0) // if no activity let's really bail { #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED) CoSuspendClassObjects(); if (!bActivity && m_nLockCnt == 0) #endif break; } } CloseHandle(hEventShutdown); PostThreadMessage(dwThreadID, WM_QUIT, 0, 0); } bool CExeModule::StartMonitor() { bool bCreated = false; hEventShutdown = CreateEvent(NULL, false, false, NULL); if (hEventShutdown == NULL) return false; DWORD dwThreadID; HANDLE h = CreateThread(NULL, 0, MonitorProc, this, 0, &dwThreadID); if (h != NULL) bCreated = true; CloseHandle(h); return bCreated; } LPCTSTR FindOneOf(LPCTSTR p1, LPCTSTR p2) { while (p1 != NULL && *p1 != NULL) { LPCTSTR p = p2; while (p != NULL && *p != NULL) { if (*p1 == *p) return CharNext(p1); p = CharNext(p); } p1 = CharNext(p1); } return NULL; } ///////////////////////////////////////////////////////////////////////////// // extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/) { ATLTRACE(_T("E {ADMTAgnt.exe}_tWinMain(hInstance=0x%08lX,...)\n"), hInstance); // obtain agent mutex // the migration driver uses this mutex to determine // if the agent process is currently running CMigrationMutex mutex(AGENT_MUTEX, true); // set debug flags to check memory allocations and leaks _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF); lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT #ifdef OFA #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED) HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED); #else HRESULT hRes = CoInitialize(NULL); #endif #if _WIN32_WINNT >= 0x0400 hRes = CoInitializeSecurity( NULL, //Points to security descriptor -1, //Count of entries in asAuthSvc NULL, //Array of names to register NULL, //Reserved for future use RPC_C_AUTHN_LEVEL_NONE, //The default authentication //level for proxies RPC_C_IMP_LEVEL_IMPERSONATE, //The default impersonation //level for proxies NULL, //Reserved; must be set to NULL EOAC_NONE, //Additional client or //server-side capabilities NULL //Reserved for future use ); // errTrace.DbgMsgWrite(0, L"CoInitSecurity returned %lx", hRes); #endif #else // we need to be able to run on NT 3.51, so see if we can dynamically load CoInitializeEx and CoInitializeSecurity HRESULT hRes = S_OK; HMODULE hLib = LoadLibrary(L"ole32.dll"); BOOL bInitialized = FALSE; if ( hLib ) { COINITIALIZEEX * pCoInitializeEx = (COINITIALIZEEX *)GetProcAddress(hLib,"CoInitializeEx"); COINITIALIZESECURITY * pCoInitializeSecurity = (COINITIALIZESECURITY *)GetProcAddress(hLib,"CoInitializeSecurity"); if ( pCoInitializeEx && pCoInitializeSecurity ) { hRes = (*pCoInitializeEx)(NULL,0 /*COINIT_MULTITHREADED*/); if ( SUCCEEDED(hRes) ) { hRes = (*pCoInitializeSecurity)( NULL, //Points to security descriptor -1, //Count of entries in asAuthSvc NULL, //Array of names to register NULL, //Reserved for future use RPC_C_AUTHN_LEVEL_NONE, //The default authentication //level for proxies RPC_C_IMP_LEVEL_IMPERSONATE, //The default impersonation //level for proxies NULL, //Reserved; must be set to NULL EOAC_NONE, //Additional client or //server-side capabilities NULL //Reserved for future use ); bInitialized = TRUE; } } FreeLibrary(hLib); } if (! bInitialized ) { hRes = CoInitialize(NULL); } #endif _ASSERTE(SUCCEEDED(hRes)); _Module.Init(ObjectMap, hInstance, &LIBID_MCSEADCTAGENTLib); _Module.dwThreadID = GetCurrentThreadId(); TCHAR szTokens[] = _T("-/"); int nRet = 0; BOOL bRun = TRUE; LPCTSTR lpszToken = FindOneOf(lpCmdLine, szTokens); while (lpszToken != NULL) { if (lstrcmpi(lpszToken, _T("UnregServer"))==0) { _Module.UpdateRegistryFromResource(IDR_EADCTAgent, FALSE); nRet = _Module.UnregisterServer(TRUE); bRun = FALSE; break; } if (lstrcmpi(lpszToken, _T("RegServer"))==0) { _Module.UpdateRegistryFromResource(IDR_EADCTAgent, TRUE); nRet = _Module.RegisterServer(TRUE); bRun = FALSE; break; } lpszToken = FindOneOf(lpszToken, szTokens); } if (bRun) { _Module.StartMonitor(); #if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED) hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED); _ASSERTE(SUCCEEDED(hRes)); hRes = CoResumeClassObjects(); #else hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE); #endif _ASSERTE(SUCCEEDED(hRes)); MSG msg; while (GetMessage(&msg, 0, 0, 0)) DispatchMessage(&msg); _Module.RevokeClassObjects(); Sleep(dwPause); //wait for any threads to finish } ATLTRACE(_T("L {ADMTAgnt.exe}_tWinMain(hInstance=0x%08lX,...)\n"), hInstance); // _CrtDbgBreak(); _Module.Term(); CoUninitialize(); return nRet; }