How to close/hide the console window when window form is open?
- Hi,
Am developing application using VC++ on VS2005. My application needs to switch to console mode if the commandline parameters are mentioned on the command prompt console else to windows form dialog mode. for eg:
test.exe --help
on command prompt should display on command console(black backgrnd console window) and if cmd is
test.exe
window form should be opened.
This has been accomplished by creating the project of the type window form and then project properties->system->subsystem to Console (/SUBSYSTEM:CONSOLE)
But now the issue what i am facing are
1> when window form is opened with ex cmd test.exe, black color console window is also opened.
2> when console is closed, window form also closes
solutions i need correspondingly are
1> how to hide/close the console window when windows form is open?
2> can the Can console and window form controls be made independent? if yes how?
Searched and tried lots of solutions provided over net, but of less help.
Help on this will be appreciated.
Thanks,- 編集済みtechoptimist 2009年11月2日 13:12
回答
- There is a serious issue with "attachconsole()" if you want to run your application on Windows 2000. My application using attachconsole started crashing on Windows 2000. Pls go through this link
http://msdn.microsoft.com/en-us/library/ms681952(VS.85).aspx
Earlier my application supported and switched between windows forms and console interfaces depending upon the parameters passed to it. As alternative to attachconsole issue & its redirection headaches, go thru this article
http://msdn.microsoft.com/en-us/magazine/cc164023.aspx
But because of attachconsole drawback, I had to completely modify my application splitting it into two different applications leaving the user to choose the kind of interface he/she wants to use. The only effort here would be to educate user that app1.exe is for GUI and app2.exe for commandline.
Hope this also helps for the people facing "no entry point found for attachconsole in kernel32.dll" on Windows 2000.- 回答としてマークtechoptimist 2009年11月19日 4:07
すべての返信
- Hello,
Have you seen http://www.codeproject.com/KB/debug/debugcon.aspx?
This example show tow to dynamically create an show console window.
So, in you case you should:
1. switch back the system proiperty.
2. in InitInstance before your dialog is created and shown:
a. Check Command Line.
b. if Console is needed - create and show it.
c. Write to console what you need to
d. return FALSE
if console is not necessary just skip the creating.
Good Luck! - Thanks!!
But thats a MFC code. Am not sure whether it can be used in managed VC++ code or have I missed out something. Including the classes defined on that link produced below error
Linking...
Console.obj : error LNK2028: unresolved token (0A000297) "extern "C" struct HMENU__ * __stdcall GetSystemMenu(struct HWND__ *,int)" (?GetSystemMenu@@$$J18YGPAUHMENU__@@PAUHWND__@@H@Z) referenced in function "public: void __thiscall CConsole::DisableClose(void)" (?DisableClose@CConsole@@$$FQAEXXZ)
Console.obj : error LNK2028: unresolved token (0A0002AE) "extern "C" int __stdcall DeleteMenu(struct HMENU__ *,unsigned int,unsigned int)" (?DeleteMenu@@$$J212YGHPAUHMENU__@@II@Z) referenced in function "public: void __thiscall CConsole::DisableClose(void)" (?DisableClose@CConsole@@$$FQAEXXZ)
Console.obj : error LNK2028: unresolved token (0A0002B7) "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const *,wchar_t const *)" (?FindWindowW@@$$J18YGPAUHWND__@@PB_W0@Z) referenced in function "public: struct HWND__ * __thiscall CConsole::GetHWND(void)" (?GetHWND@CConsole@@$$FQAEPAUHWND__@@XZ)
Console.obj : error LNK2028: unresolved token (0A0002F7) "extern "C" int __stdcall ShowWindow(struct HWND__ *,int)" (?ShowWindow@@$$J18YGHPAUHWND__@@H@Z) referenced in function "public: void __thiscall CConsole::Show(bool)" (?Show@CConsole@@$$FQAEX_N@Z)
Console.obj : error LNK2028: unresolved token (0A0002FD) "extern "C" int __stdcall DrawMenuBar(struct HWND__ *)" (?DrawMenuBar@@$$J14YGHPAUHWND__@@@Z) referenced in function "public: void __thiscall CConsole::DisableClose(void)" (?DisableClose@CConsole@@$$FQAEXXZ)
Console.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall FindWindowW(wchar_t const *,wchar_t const *)" (?FindWindowW@@$$J18YGPAUHWND__@@PB_W0@Z) referenced in function "public: struct HWND__ * __thiscall CConsole::GetHWND(void)" (?GetHWND@CConsole@@$$FQAEPAUHWND__@@XZ)
Console.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall ShowWindow(struct HWND__ *,int)" (?ShowWindow@@$$J18YGHPAUHWND__@@H@Z) referenced in function "public: void __thiscall CConsole::Show(bool)" (?Show@CConsole@@$$FQAEX_N@Z)
Console.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall DrawMenuBar(struct HWND__ *)" (?DrawMenuBar@@$$J14YGHPAUHWND__@@@Z) referenced in function "public: void __thiscall CConsole::DisableClose(void)" (?DisableClose@CConsole@@$$FQAEXXZ)
Console.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall DeleteMenu(struct HMENU__ *,unsigned int,unsigned int)" (?DeleteMenu@@$$J212YGHPAUHMENU__@@II@Z) referenced in function "public: void __thiscall CConsole::DisableClose(void)" (?DisableClose@CConsole@@$$FQAEXXZ)
Console.obj : error LNK2019: unresolved external symbol "extern "C" struct HMENU__ * __stdcall GetSystemMenu(struct HWND__ *,int)" (?GetSystemMenu@@$$J18YGPAUHMENU__@@PAUHWND__@@H@Z) referenced in function "public: void __thiscall CConsole::DisableClose(void)" (?DisableClose@CConsole@@$$FQAEXXZ)
fatal error LNK1120: 10 unresolved externals
11 error(s), 1 warning(s)
- Yep,
Actually was not clear from your initial message that you are talking about Managed VC++.
But, from provided demo you actually need only the CConsole class (console.cpp and console.h), and due to this is written in pure Win32 - the you can include it in your project or easelly convert it to managed VC++.
Good Luck! - Please see the C# example. I guess you'll convert it easely to VC++
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Con
{
class Ext_Console
{
static bool console_on = false;
public static void Show(bool on, string title)
{
console_on = on;
if ( console_on )
{
AllocConsole();
Console.Title = title;
// use to change color
Console.BackgroundColor = System.ConsoleColor.White;
Console.ForegroundColor = System.ConsoleColor.Black;
}
else
FreeConsole();
}
public static void Write(string output)
{
if ( console_on )
Console.Write(output);
}
public static void WriteLine(string output)
{
if ( console_on )
Console.WriteLine(output);
}
}
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
}
// USAGE
Ext_Console.Show(true,"Title of console");
Ext_Console.Write("console output ");
Ext_Console.WriteLine("console output");
- ThanX Dumitru!! This worked for me. Now i have complete control over opening & closing console in Windows form application. But the issue i now(while running on command prompt e.g test --help) see is messages, which i try to display in the application, get deffered until console is closed(Freeconsole()) as shown below
C:\Temp\test --help <-- ENTER key -- line 1
C:\Temp\f - file name -- line 2
O - operation type -- line 3
T - file type -- line 4
USAGE: -- line 5
test [-f (filename)] [-O(operation type)] [-T (file type)] -- line 6
I do not get the prompt after line 6 since its already returned at line 2.
My code is very simple. below is the psuedocode
Ext_Console.Show(true,"Title of console");
if(argv == "--help"){
Ext_Console.Write(" ....");
}
Ext_Console.Show(false,nullptr);
Any idea, why is this?
Thanks - Still nobody is able to solve the issue on console behaviour??
"ThanX Dumitru!! This worked for me. Now i have complete control over opening & closing console in Windows form application. But the issue i now(while running on command prompt e.g test --help) see is messages, which i try to display in the application, get deffered until console is closed(Freeconsole()) as shown below
C:\Temp\test --help <-- ENTER key -- line 1
C:\Temp\f - file name -- line 2
O - operation type -- line 3
T - file type -- line 4
USAGE: -- line 5
test [-f (filename)] [-O(operation type)] [-T (file type)] -- line 6
I do not get the prompt after line 6 since its already returned at line 2.
My code is very simple. below is the psuedocode
Ext_Console.Show(true,"Title of console");
if(argv == "--help"){
Ext_Console.Write(" ....");
}
Ext_Console.Show(false,nullptr);
Any idea, why is this?
" - Seems visual Studio 2005 has no answer for this issue of attachconsole()...Anyways, Thanks for the help!!
- There is a serious issue with "attachconsole()" if you want to run your application on Windows 2000. My application using attachconsole started crashing on Windows 2000. Pls go through this link
http://msdn.microsoft.com/en-us/library/ms681952(VS.85).aspx
Earlier my application supported and switched between windows forms and console interfaces depending upon the parameters passed to it. As alternative to attachconsole issue & its redirection headaches, go thru this article
http://msdn.microsoft.com/en-us/magazine/cc164023.aspx
But because of attachconsole drawback, I had to completely modify my application splitting it into two different applications leaving the user to choose the kind of interface he/she wants to use. The only effort here would be to educate user that app1.exe is for GUI and app2.exe for commandline.
Hope this also helps for the people facing "no entry point found for attachconsole in kernel32.dll" on Windows 2000.- 回答としてマークtechoptimist 2009年11月19日 4:07

