GME  13
Console.cpp
Go to the documentation of this file.
00001 // Console.cpp : Implementation of CConsoleApp and DLL registration.
00002 
00003 #include "stdafx.h"
00004 #include "Console.h"
00005 #include <initguid.h>
00006 #include "Console_i.c"
00007 #include "ScriptHost.h"
00008 
00009 #ifdef _DEBUG
00010 #define new DEBUG_NEW
00011 #undef THIS_FILE
00012 static char THIS_FILE[] = __FILE__;
00013 #endif
00014 
00015 
00016 CConsoleApp NEAR theApp;
00017 
00018 const GUID CDECL BASED_CODE _tlid =
00019                 { 0x339fd307, 0x717b, 0x4698, { 0xac, 0x13, 0x4a, 0xcc, 0xcf, 0x5d, 0x4c, 0x91 } };
00020 const WORD _wVerMajor = 1;
00021 const WORD _wVerMinor = 0;
00022 
00023 
00025 // CConsoleApp::InitInstance - DLL initialization
00026 
00027 BOOL CConsoleApp::InitInstance()
00028 {
00029 #if _MSC_VER < 1700
00030         // See MSDN example code for CWinApp::InitInstance: http://msdn.microsoft.com/en-us/library/ae6yx0z0.aspx
00031         // MFC module state handling code is changed with VC80.
00032         // We follow the Microsoft's suggested way, but in case of any trouble the set the
00033         // HKCU\Software\GME\AfxSetAmbientActCtxMod key to 0
00034         UINT uAfxSetAmbientActCtxMod = 1;
00035         HKEY hKey;
00036         if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\GME\\"),
00037                                          0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
00038         {
00039                 TCHAR szData[128];
00040                 DWORD dwKeyDataType;
00041                 DWORD dwDataBufSize = sizeof(szData)/sizeof(TCHAR);
00042 
00043                 if (RegQueryValueEx(hKey, _T("AfxSetAmbientActCtxMod"), NULL, &dwKeyDataType,
00044                                                         (LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
00045                 {
00046                         uAfxSetAmbientActCtxMod = _tcstoul(szData, NULL, 10);
00047                 }
00048 
00049                 RegCloseKey(hKey);
00050         }
00051         if (uAfxSetAmbientActCtxMod != 0)
00052         {
00053                 AfxSetAmbientActCtx(FALSE);
00054         }
00055 #endif
00056 
00057         BOOL bInit = COleControlModule::InitInstance();
00058         if (!InitATL())
00059                 return FALSE;
00060 
00061 
00062         if (bInit)
00063         {
00064                 // TODO: Add your own module initialization code here.
00065         }
00066 
00067         return bInit;
00068 }
00069 
00070 
00072 // CConsoleApp::ExitInstance - DLL termination
00073 
00074 int CConsoleApp::ExitInstance()
00075 {
00076         // TODO: Add your own module termination code here.
00077 
00078         _Module.Term();
00079 
00080 
00081         return COleControlModule::ExitInstance();
00082 }
00083 
00084 
00086 // DllRegisterServer - Adds entries to the system registry
00087 
00088 STDAPI DllRegisterServer(void)
00089 {
00090         AFX_MANAGE_STATE(_afxModuleAddrThis);
00091 
00092         if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), _tlid))
00093                 return ResultFromScode(SELFREG_E_TYPELIB);
00094 
00095         if (!COleObjectFactoryEx::UpdateRegistryAll(TRUE))
00096                 return ResultFromScode(SELFREG_E_CLASS);
00097 
00098         return _Module.RegisterServer(TRUE);
00099 }
00100 
00101 
00103 // DllUnregisterServer - Removes entries from the system registry
00104 
00105 STDAPI DllUnregisterServer(void)
00106 {
00107         AFX_MANAGE_STATE(_afxModuleAddrThis);
00108 
00109         if (!AfxOleUnregisterTypeLib(_tlid, _wVerMajor, _wVerMinor))
00110                 return ResultFromScode(SELFREG_E_TYPELIB);
00111 
00112         if (!COleObjectFactoryEx::UpdateRegistryAll(FALSE))
00113                 return ResultFromScode(SELFREG_E_CLASS);
00114 
00115         return _Module.UnregisterServer(TRUE); //TRUE indicates that typelib is unreg'd
00116 }
00117 
00118 
00119 CComModule _Module;
00120 
00121 BEGIN_OBJECT_MAP(ObjectMap)
00122 OBJECT_ENTRY(CLSID_ScriptHost, CScriptHost)
00123 END_OBJECT_MAP()
00124 
00125 STDAPI DllCanUnloadNow(void)
00126 {
00127         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00128         return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ? S_OK : S_FALSE; 
00129 } 
00131 // Returns a class factory to create an object of the requested type
00132 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
00133 {
00134         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00135         if(AfxDllGetClassObject(rclsid, riid, ppv) == S_OK)
00136                 return S_OK;
00137         return _Module.GetClassObject(rclsid, riid, ppv);
00138 }
00139 
00140 BOOL CConsoleApp::InitATL()
00141 {
00142         _Module.Init(ObjectMap, AfxGetInstanceHandle());
00143         return TRUE;
00144 
00145 }