Fix console incorrectly showing on Windows Blender startup in some cases.

The console window is hidden by default, but we need to avoid this when
starting from the command prompt, because it would hide the window you just
typed the command in.

Previously it would check if Blender was started from "explorer.exe" to
determine that, but that wasn't working for application launchers like
Appetizer or Colibri. Instead we now check if the process ID is the same as
the process ID of the console window, which appears to work reliably.

Reviewed By: brecht

Differential Revision: https://developer.blender.org/D122
This commit is contained in:
Masakazu Ito 2013-12-31 00:08:07 +01:00 committed by Brecht Van Lommel
parent a1c740a420
commit 3d40e3f9db

@ -1386,49 +1386,43 @@ void GHOST_SystemWin32::putClipboard(GHOST_TInt8 *buffer, bool selection) const
}
}
static bool isStartedFromCommandPrompt()
{
HWND hwnd = GetConsoleWindow();
if (hwnd) {
DWORD pid = (DWORD)-1;
GetWindowThreadProcessId(hwnd, &pid);
if (pid == GetCurrentProcessId())
return true;
}
return false;
}
int GHOST_SystemWin32::toggleConsole(int action)
{
switch (action)
{
case 3: //hide if no console
case 3: // startup: hide if not started from command prompt
{
DWORD sp = GetCurrentProcessId();
HANDLE ptree = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 e = {0}; e.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(ptree, &e)) {
do { //Searches for Blender's PROCESSENTRY32
if (e.th32ProcessID == sp) {
sp = e.th32ParentProcessID;
Process32First(ptree, &e);
do { //Got parent id, searches for its PROCESSENTRY32
if (e.th32ProcessID == sp) {
if (strcmp("explorer.exe", e.szExeFile) == 0)
{ //If explorer, hide cmd
ShowWindow(GetConsoleWindow(), SW_HIDE);
m_consoleStatus = 0;
}
break;
}
} while (Process32Next(ptree, &e));
break;
}
} while (Process32Next(ptree, &e));
if (isStartedFromCommandPrompt()) {
ShowWindow(GetConsoleWindow(), SW_HIDE);
m_consoleStatus = 0;
}
CloseHandle(ptree);
break;
}
case 0: //hide
case 0: // hide
ShowWindow(GetConsoleWindow(), SW_HIDE);
m_consoleStatus = 0;
break;
case 1: //show
case 1: // show
ShowWindow(GetConsoleWindow(), SW_SHOW);
m_consoleStatus = 1;
break;
case 2: //toggle
case 2: // toggle
ShowWindow(GetConsoleWindow(), m_consoleStatus ? SW_HIDE : SW_SHOW);
m_consoleStatus = !m_consoleStatus;
break;