Пример #1
0
DWORD __stdcall FactoryThreadProc(LPVOID pv)
{
	HRESULT hr;

	HANDLE hShutdownEvent = (HANDLE)pv;

	hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
	ATLASSERT(SUCCEEDED(hr));

	hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE);
	ATLASSERT(SUCCEEDED(hr));

	DWORD dwRet = WaitForSingleObject(hShutdownEvent, INFINITE);
	ATLASSERT(dwRet == WAIT_OBJECT_0);
	
	hr = _Module.RevokeClassObjects();
	ATLASSERT(SUCCEEDED(hr));

	CoUninitialize();

	return 0;
}
Пример #2
0
int RunWinMain(HINSTANCE hInstance, LPTSTR lpstrCmdLine, int nCmdShow)
{
	// DLL攻撃対策
	SetDllDirectory(_T(""));
#if 0
	#if defined (_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
	//メモリリーク検出用
	_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
	_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF
				| _CRTDBG_CHECK_ALWAYS_DF
	);
	//_CrtSetBreakAlloc(874);
	#endif
#endif

	#if defined (_DEBUG) && defined(_CRTDBG_MAP_ALLOC)
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	#endif

#ifdef _DEBUG
	// ATLTRACEで日本語を使うために必要
	_tsetlocale( LC_ALL, _T("japanese") );
#endif

	Misc::setHeapAllocLowFlagmentationMode();	//+++

	// 設定ファイルのフルパスを取得する
	MtlIniFileNameInit(g_szIniFileName, MAX_PATH);

	// 複数起動の確認
	if (CheckOneInstance(lpstrCmdLine)) 
		return 0;

	g_pMainWnd	 = NULL;
	//	HRESULT hRes = ::CoInitialize(NULL);
	HRESULT hRes = ::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
//	ATLASSERT( SUCCEEDED(hRes) );
	// If you are running on NT 4.0 or higher you can use the following call instead to
	// make the EXE free threaded. This means that calls come in on a random RPC thread
	//	HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);

	hRes		 = ::OleInitialize(NULL);
	ATLASSERT( SUCCEEDED(hRes) );

	ATLTRACE(_T("tWinMain\n") _T("CommandLine : %s\n"), lpstrCmdLine);

	/* コモンコントロールを初期化 */
	INITCOMMONCONTROLSEX iccx;
	iccx.dwSize = sizeof (iccx);
	iccx.dwICC	= ICC_COOL_CLASSES | ICC_BAR_CLASSES | ICC_USEREX_CLASSES;
	int 	ret = ::InitCommonControlsEx(&iccx);
	ATLASSERT(ret);

	hRes	= _Module.Init(ObjectMap, hInstance, &LIBID_ATLLib);
	//hRes	= _Module.Init(NULL, hInstance);
	ATLASSERT( SUCCEEDED(hRes) );


	int 	nRet		 = 0;
	bool	bRun		 = true;
	bool	bAutomation  = false;
	bool	bTray		 = false;


	bool bRet = _PrivateInit();
	if (!bRet) {
		ErrorLogPrintf(_T("_PrivateInitでエラー\n"));
		nRet = -1;
	}

	// Init CEF
	CefSettings settings;
	settings.multi_threaded_message_loop	= true;
	std::wstring strCachePath = CMainOption::s_strCacheFolderPath;
	if (strCachePath.empty()) {
		strCachePath = static_cast<LPCWSTR>(Misc::GetExeDirectory() + _T("cache"));
	}
	CefString(&settings.cache_path).FromWString(strCachePath);
	CefString(&settings.locale).FromWString(std::wstring(L"ja"));
	//settings.auto_detect_proxy_settings_enabled = true;
	ATLVERIFY(CefInitialize(settings, nullptr));

	if (nRet < 0)
		goto END_APP;


	// ActiveXコントロールをホストするための準備
	//AtlAxWinInit();

	// コマンドライン入力によってはCOMサーバー登録及び解除を行う
	nRet = RegisterCOMServer(nRet, bRun, bAutomation, bTray);
	if (FAILED(nRet)) {
		ErrorLogPrintf(_T("RegisterCOMServerでエラー\n"));
		nRet = -1;
		goto END_APP;
	}

	CDonutSimpleEventManager::RaiseEvent(EVENT_PROCESS_START);

	if (bRun) {
		_Module.StartMonitor();
		hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
		ATLASSERT( SUCCEEDED(hRes) );
		hRes = ::CoResumeClassObjects();
		ATLASSERT( SUCCEEDED(hRes) );

		if (bAutomation) {
			CMessageLoop theLoop;
			nRet = theLoop.Run();
		} else {
			//+++ 起動時の環境ファイルチェック. unDonut.iniがなく
			//	環境ファイルが足りてなかったら起動ページをabout:warningにする.
			if (lpstrCmdLine == 0 || lpstrCmdLine[0] == 0) {
				if (HaveEnvFiles() == false)
					lpstrCmdLine = _T("about:warning");
			}
			//\\nRet = Run(lpstrCmdLine, nCmdShow, bTray);
			//nRet = MultiThreadManager::Run(lpstrCmdLine, nCmdShow, bTray);
				//int nRet = 0;
			{
				CMessageLoop theLoop;
				_Module.AddMessageLoop(&theLoop);

				CMainFrame	 wndMain;
				if (wndMain.CreateEx() == NULL) {
					ATLTRACE( _T("Main window creation failed!\n") );
					return 0;
				}

				// load windowplacement
				wndMain.startupMainFrameStayle(nCmdShow, /*bTray*/false);

				_Module.Lock();

				if (CStartUpOption::s_dwParam) 
					CStartUpOption::StartUp(wndMain);
				if (lpstrCmdLine && lpstrCmdLine[0] != _T('\0'))
					CommandLineArg(wndMain, lpstrCmdLine);

				wndMain.SetAutoBackUp();		//自動更新するなら、開始.

				// 実際のメインループ.
				nRet = theLoop.Run();

				_Module.RemoveMessageLoop();
			}
		}
	  #if 1 //+++ WTLのメイン窓クローズが正常終了時に、終了コードとして1を返す...
			//+++ OSに返す値なので0のほうがよいはずで、
			//+++ donutの他の部分では0にしているようなので
			//+++ しかたないので、強制的に変換.
		if (nRet == 1)
			nRet = 0;
	  #endif

		_Module.RevokeClassObjects();
		::Sleep(_Module.m_dwPause);
	}

	//_PrivateTerm();
	ATLTRACE(_T("正常終了しました。\n"));
END_APP:
	_Module.Term();
	::OleUninitialize();
	::CoUninitialize();

	// Shut down CEF.
	DWORD	dwTime = ::timeGetTime();
	boost::thread	terminateWatch([dwTime]() {
		while (dwTime + (5 * 1000) > ::timeGetTime()) {
			if (g_bSefShutDown)
				return ;

			::Sleep(0);
		}
		if (g_bSefShutDown == false)
			ExitProcess(-5);
	});
	CefShutdown();
	g_bSefShutDown = true;

	CDonutSimpleEventManager::RaiseEvent(EVENT_PROCESS_END);
 	return nRet;
}