void WinConsole::InitAppMode() { SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE); m_instance = (HINSTANCE)GetModuleHandle(0); DWORD processId; GetWindowThreadProcessId(GetConsoleWindow(), &processId); if (GetCurrentProcessId() == processId && g_ArgumentCount == 1) { m_initialArguments = (char**)g_Arguments; m_initialArgumentCount = g_ArgumentCount; // make command line to start in server mode m_defaultArguments = (char**)malloc(sizeof(char*) * 3); m_defaultArguments[0] = (*g_Arguments)[0]; m_defaultArguments[1] = "-s"; m_defaultArguments[2] = nullptr; g_Arguments = (char*(*)[])m_defaultArguments; g_ArgumentCount = 2; m_appMode = true; } else if (GetCurrentProcessId() == processId && g_ArgumentCount > 1) { for (int i = 1; i < g_ArgumentCount; i++) { if (!strcmp((*g_Arguments)[i], "-D")) { break; } else if (!strcmp((*g_Arguments)[i], "-app")) { m_appMode = true; } else if (!strcmp((*g_Arguments)[i], "-auto")) { m_autoParam = true; } else if (!strcmp((*g_Arguments)[i], "-A")) { m_addParam = true; } } if (m_appMode || m_autoParam) { m_initialArguments = (char**)g_Arguments; m_initialArgumentCount = g_ArgumentCount; // remove "-app" from command line int argc = g_ArgumentCount - (m_appMode ? 1 : 0) - (m_autoParam ? 1 : 0); m_defaultArguments = (char**)malloc(sizeof(char*) * (argc + 2)); int p = 0; for (int i = 0; i < g_ArgumentCount; i++) { if (strcmp((*g_Arguments)[i], "-app") && strcmp((*g_Arguments)[i], "-auto")) { m_defaultArguments[p++] = (*g_Arguments)[i]; } } m_defaultArguments[p] = nullptr; g_Arguments = (char*(*)[])m_defaultArguments; g_ArgumentCount = p; } } if (m_addParam) { RunAnotherInstance(); return; } // m_appMode indicates whether the program was started as a standalone app // (not from a dos box window). In that case we hide the console window, // show the tray icon and start in server mode if (m_appMode) { CreateResources(); CheckRunning(); } }
int WINAPI WinMain( HINSTANCE Inst, HINSTANCE PrevInst, LPSTR CmdLine, int Show) { DWORD Result = ERROR_NOT_FOUND; DWORD Index; BOOL FromStartup = FALSE; BOOL Found = FALSE; // // Always load the language library here // Locale::LoadLanguageLibrary(); // // An internal check to see if we're being called from startup // if (!_stricmp(CmdLine, "Startup")) FromStartup = TRUE; else if (!strncmp(CmdLine, "Uninstall ", 10)) RedirectUninstall(CmdLine + 10); // // Walk the dispatch table comparing the passed in command line argument with // the symbolic name for the dispatch handler to be called. // for (Index = 0; ArgumentDispatchTable[Index].Name; Index++) { if (!_stricmp(CmdLine, ArgumentDispatchTable[Index].Name)) { Result = ArgumentDispatchTable[Index].Function(); Log(LOG_SEV_DEBUG, "'%s' returned %lu.", ArgumentDispatchTable[Index].Name, Result); Found = TRUE; break; } } // // If no match was found, display the user interface // if (!Found) { // // Check to see if another instance of WehnTrust is running, if so, // return success // if (CheckRunning()) return ERROR_SUCCESS; Result = Ui::Display( TRUE, TRUE, FromStartup); // // If the user interface needs to restart, such as due to a locale change, // do that now. // if (Result == UI_RESULT_RESTART) { RestartUserInterface( Inst); } } return Result; }