示例#1
0
int Run(LPTSTR lpstrCmdLine = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
  try
  {
    CMessageLoop theLoop;
    _Module.AddMessageLoop(&theLoop);

    wstring strConfigFile(L"");
    bool    bReuse = false;
    wstring strSyncName;

    ParseCommandLine(
      lpstrCmdLine,
      strConfigFile,
      bReuse,
      strSyncName);

    if (strConfigFile.length() == 0)
    {
      strConfigFile = wstring(L"console.xml");
    }

    if (!g_settingsHandler->LoadSettings(Helpers::ExpandEnvironmentStrings(strConfigFile)))
    {
      throw std::exception("enable to load settings!");
    }

		if (!bReuse)
		{
			bReuse = !g_settingsHandler->GetBehaviorSettings2().instanceSettings.bAllowMultipleInstances;
		}

		if (!strSyncName.empty())
		{
			wstring strWindowTitle;
			vector<wstring> startupTabs;
			vector<wstring> startupTabTitles;
			vector<wstring> startupDirs;
			vector<wstring> startupCmds;
			vector<DWORD>   basePriorities;
			int nMultiStartSleep = 0;
			wstring strWorkingDir;

			MainFrame::ParseCommandLine
			(
				lpstrCmdLine,
				strWindowTitle,
				startupTabs,
				startupTabTitles,
				startupDirs,
				startupCmds,
				basePriorities,
				nMultiStartSleep,
				strWorkingDir
			);

			TabSettings& tabSettings = g_settingsHandler->GetTabSettings();

			// find tab with corresponding name...
			for (auto tabData = tabSettings.tabDataVector.begin(); tabData != tabSettings.tabDataVector.end(); ++tabData)
			{
				if (tabData->get()->strTitle == startupTabs[0])
				{
					wstring strInitialDir(g_settingsHandler->GetConsoleSettings().strInitialDir);

					if (startupDirs.size() > 0 && startupDirs[0].length() > 0)
					{
						strInitialDir = startupDirs[0];
					}
					else if (tabData->get()->strInitialDir.length() > 0)
					{
						strInitialDir = tabData->get()->strInitialDir;
					}

					wstring	strShell(g_settingsHandler->GetConsoleSettings().strShell);

					if (tabData->get()->strShell.length() > 0)
					{
						strShell	= tabData->get()->strShell;
					}

					wstring strInitialCmd = strWorkingDir;

					if (startupCmds.size() > 0 && startupCmds[0].length() > 0)
					{
						strInitialCmd = startupCmds[0];
					}

					DWORD dwBasePriority = ULONG_MAX;

					if (basePriorities.size() > 0)
					{
						dwBasePriority = basePriorities[0];
					}

					if (dwBasePriority == ULONG_MAX)
						dwBasePriority = tabData->get()->dwBasePriority;

					try
					{
						ConsoleHandler ConsoleHandler;
						ConsoleHandler.StartShellProcessAsAdministrator
						(
							strSyncName,
							strShell,
							strInitialDir,
							strInitialCmd,
							dwBasePriority,
							tabData->get()->environmentVariables
						);
					}
					catch (const ConsoleException& ex)
					{
						MessageBox(NULL, ex.GetMessage().c_str(), Helpers::LoadString(IDS_CAPTION_ERROR).c_str(), MB_ICONERROR|MB_OK);
						return 1;
					}

					break;
				}
			}

      return 0;
    }

    if (bReuse && HandleReuse(lpstrCmdLine))
      return 0;

    // create main window
    NoTaskbarParent noTaskbarParent;
    MainFrame wndMain(lpstrCmdLine);

    if (!g_settingsHandler->GetAppearanceSettings().stylesSettings.bTaskbarButton)
    {
      noTaskbarParent.Create(NULL);
    }

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

#ifdef _USE_AERO
    // restore the drop files message in elevated console
    ::ChangeWindowMessageFilter(WM_DROPFILES, MSGFLT_ADD);
    ::ChangeWindowMessageFilter(WM_COPYDATA, MSGFLT_ADD);
    ::ChangeWindowMessageFilter(0x0049, MSGFLT_ADD);
#endif

    wndMain.ShowWindow(nCmdShow);

    SharedMemory<HWND> sharedInstance;
    if (bReuse)
    {
      sharedInstance.Create(L"ConsoleZ", 1, syncObjNone, _T(""));
      sharedInstance = wndMain.m_hWnd;
    }

    WallPaperThread wallPaperThread(wndMain);

    if (Helpers::CheckOSVersion(6, 1))
    {
      // Win7 or more, we use the wallpaper slideshow monitoring
      wallPaperThread.Start();
    }

		TranslateMessageEx = (_t_TranslateMessageEx)::GetProcAddress(::GetModuleHandle(L"user32.dll"), "TranslateMessageEx");
		if( !TranslateMessageEx )
			Win32Exception::ThrowFromLastError("TranslateMessageEx");

    int nRet = theLoop.Run();

    if (noTaskbarParent.m_hWnd != NULL) noTaskbarParent.DestroyWindow();

    _Module.RemoveMessageLoop();

    return nRet;
  }
  catch(std::exception& e)
  {
    ::MessageBoxA(0, e.what(), "exception", MB_OK);
    return 1;
  }
}
int Run(LPTSTR lpstrCmdLine = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
	CMessageLoop theLoop;
	_Module.AddMessageLoop(&theLoop);

	wstring			strConfigFile(L"");
	wstring			strWindowTitle(L"");
	vector<wstring>	startupTabs;
	vector<wstring>	startupDirs;
	vector<wstring>	startupCmds;
	int				nMultiStartSleep = 0;
	wstring			strDbgCmdLine(L"");

	ParseCommandLine(
		lpstrCmdLine, 
		strConfigFile, 
		strWindowTitle, 
		startupTabs, 
		startupDirs, 
		startupCmds, 
		nMultiStartSleep, 
		strDbgCmdLine);

	if (strConfigFile.length() == 0)
	{
		strConfigFile = wstring(L"console.xml");
//		strConfigFile = Helpers::GetModulePath(NULL) + wstring(L"console.xml");
//		strConfigFile = wstring(::_wgetenv(L"APPDATA")) + wstring(L"\\Console\\console.xml");
	}

	if (!g_settingsHandler->LoadSettings(Helpers::ExpandEnvironmentStrings(strConfigFile)))
	{
		//TODO: error handling
		return 1;
	}

	// create main window
	NoTaskbarParent noTaskbarParent;
	MainFrame wndMain(strWindowTitle, startupTabs, startupDirs, startupCmds, nMultiStartSleep, strDbgCmdLine);

	if (!g_settingsHandler->GetAppearanceSettings().stylesSettings.bTaskbarButton)
	{
		noTaskbarParent.Create(NULL);
	}

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

	wndMain.ShowWindow(nCmdShow);

	int nRet = theLoop.Run();

	if (noTaskbarParent.m_hWnd != NULL) noTaskbarParent.DestroyWindow();

	_Module.RemoveMessageLoop();

	return nRet;
}
示例#3
0
int Run(LPTSTR lpstrCmdLine = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
	// vds: >>
	HANDLE hOneInstanceMutex = CreateMutex(NULL, FALSE, _T("ConsoleOnInstanceMutex"));
	DWORD oneInstanceResult = GetLastError();
	bool bOneInstance = oneInstanceResult == ERROR_ALREADY_EXISTS; // vds:
	HWND hPrevConsole = FindWindow(MainFrame::GetWndClassInfo().m_wc.lpszClassName, NULL);
	// vds: <<

	CMessageLoop theLoop;
	_Module.AddMessageLoop(&theLoop);

	wstring			strConfigFile(L"");
	wstring			strWindowTitle(L"");
	vector<wstring>	startupTabs;
	vector<wstring>	startupDirs;
	vector<wstring>	startupCmds;
	vector<wstring>	postedCmds; // vds: posted commands
	vector<bool> reusePreviousTabs; // vds: sessions
	int				nMultiStartSleep = 0;
	wstring			strDbgCmdLine(L"");
	WORD			iFlags = 0; // vds

	ParseCommandLine(
		lpstrCmdLine, 
		strConfigFile, 
		strWindowTitle, 
		startupTabs, 
		startupDirs, 
		startupCmds, 
		postedCmds, 
		nMultiStartSleep, 
		strDbgCmdLine,
		iFlags); // vds

	if (strConfigFile.length() == 0)
	{
		strConfigFile = wstring(L"console.xml"); // vds
//		strConfigFile = wstring(L"%APPDATA%\Console\console.xml");
//		strConfigFile = Helpers::GetModulePath(NULL) + wstring(L"console.xml");
//		strConfigFile = wstring(::_wgetenv(L"APPDATA")) + wstring(L"\\Console\\console.xml"); // vds
	}

	if (!g_settingsHandler->LoadSettings(Helpers::ExpandEnvironmentStrings(strConfigFile)))
	{
		//TODO: error handling
		return -1;
	}

	// vds: >>
	if (g_settingsHandler->GetBehaviorSettings().oneInstanceSettings.bAllowMultipleInstances && !(iFlags & CLF_REUSE_PREV_INSTANCE))
		bOneInstance = false;

	if (iFlags & CLF_FORCE_NEW_INSTANCE)
		bOneInstance = false;
	// vds: <<

	// vds: sessions >>
	bool reuseTab = g_settingsHandler->GetBehaviorSettings().oneInstanceSettings.bReuseTab;
	while (reusePreviousTabs.size() < startupTabs.size()) {
		reusePreviousTabs.push_back(reuseTab);
	}
	// vds: sessions <<

	// vds: sessions >>
	if (!bOneInstance) {
		SessionsSettings& sessionsSettings = g_settingsHandler->GetSessionsSettings();
		if (sessionsSettings.bRestoreTabs) {
			for (unsigned int j = 0; j < sessionsSettings.sessionDataVector.size(); ++j) {
				shared_ptr<SessionData> sessionData = sessionsSettings.sessionDataVector[j];

				startupTabs.insert(startupTabs.begin() + j, sessionData->strTabTitle);
				startupDirs.insert(startupDirs.begin() + j, sessionData->strWorkingDir);
				startupCmds.insert(startupCmds.begin() + j, L"");
				postedCmds.insert(postedCmds.begin() + j, L"");
				reusePreviousTabs.insert(reusePreviousTabs.begin() + j, false);
			}
		}
	}
	// vds: sessions <<

	// create main window
	NoTaskbarParent noTaskbarParent;
	MainFrame wndMain(strWindowTitle, startupTabs, startupDirs, startupCmds, postedCmds, reusePreviousTabs, nMultiStartSleep, bOneInstance, strDbgCmdLine); // vds:

	if (!g_settingsHandler->GetAppearanceSettings().stylesSettings.bTaskbarButton && !bOneInstance) // vds:
	{
		noTaskbarParent.Create(NULL);
	}

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

	// vds: >>
	// vds: This variable has to be in the scope of the theLoop.Run() call otherwise the message is destroyed before it arrive to destination.
	wstring rebuildCommandLine;
	if (!bOneInstance) {
		wndMain.ShowWindow(nCmdShow);
	}
	else {
		// Give 3 seconds to exchange the DDE messages.
		wndMain.SetTimer(TIMER_TIMEOUT, 3000, NULL);
		rebuildCommandLine = BuildCommandLine(
			strConfigFile, 
			strWindowTitle, 
			startupTabs, 
			startupDirs, 
			startupCmds, 
			postedCmds,
			nMultiStartSleep, 
			strDbgCmdLine,
			iFlags);

#ifdef USE_COPYDATA_MSG
		// Search for running instance of console
		if (hPrevConsole) {
			COPYDATASTRUCT stCopyData;
			stCopyData.dwData = eEC_NewTab;
			stCopyData.cbData = (rebuildCommandLine.size() + 1) * sizeof(wchar_t);
			stCopyData.lpData = reinterpret_cast<void*>(const_cast<wchar_t*>(rebuildCommandLine.c_str()));

			SendMessage(hPrevConsole, WM_COPYDATA, 0, (LPARAM)(LPVOID)&stCopyData);
		}
#else
		::PostMessage(wndMain.m_hWnd, WM_SEND_DDE_COMMAND, reinterpret_cast<WPARAM>(hPrevConsole), reinterpret_cast<LPARAM>(rebuildCommandLine.c_str()));
#endif
	}
	// vds: <<
	int nRet = theLoop.Run();

	if (noTaskbarParent.m_hWnd != NULL) noTaskbarParent.DestroyWindow();

	_Module.RemoveMessageLoop();

	// vds: >>
	if (hOneInstanceMutex) {
		CloseHandle(hOneInstanceMutex);
		hOneInstanceMutex = NULL;
	}
	// vds: <<

	return nRet;
}