[VC ++] Hide the console program window

Source: Internet
Author: User
As we all know, when writing a Win32 console application Program By default, there will be a console window similar to the DOS window, but sometimes we just want to run a function in the program Code You do not want to display this console window. After the code is executed, the program automatically exits. The following describes how to hide the console window of the Win32 console application.
This method is implemented by setting the compiler's link switch, so let's take a look at the compilation
(That is, the linker option ).
First, let's take a look at the/subsystem options of linker.
The syntax of this option is as follows:
/Subsystem: {console | efi_application | efi_boot_service_driver |
Efi_rom | efi_runtime_driver | native | POSIX | Windows | WindowsCE}
[, Major [. Minor]
This link shows the operating system how to run executable files.
Console:
Win32 character mode application. This type of application will generate a DOS-like
The console window of the window. If the main function of the application is main () or wmain (),
This application is a console application.
Extensible firmware interface
A parameter option related to the CPU architecture is not commonly used. It is not described here.
If you are interested, visit the Intel homepage to view the relevant content.
Native;
Device drive option. If the/driver: WDM option is set, the link option (native) is the default option.
POSIX:
Applications running on the POSIX Subsystem in Windows NT
Windows:
This type of application does not generate a console window. The window of this type of application is created by the user. In short
Is a standard Win32 application. Its Entry address is the address of the winmain () function or the wwinmain () function.
If you define the main function as winmain or wwinmain In the application, the application is
Win32 application!
WindowsCE:
Applications running on Windows CE
Major and minor (optional ):
The primary version number and minor version number. This option is optional. The value ranges from 0 ~ Decimal integer between 65535
We can see from the above that if we create a Win32 console application, the/subsystem option of linker should be
Console, which can be viewed in project> setting> link> project option in the VC development environment!
Next, let's take a look at how the application runs!
We know that the C/C program is supported by the CC Runtime Library.
The linker first looks for the startup function of the application, for example:
If you create a console program, the compiler's link switch will be in the following form:
/Subsystem: console/entry: maincrtstartup (ANSI)
/Subsystem: console/entry: wmaincrtstartuup (UNICODE)
If you create a Win32 application, the compiler uses the link switch as follows:
/Subsystem: Windows/entry: winmain (ANSI)
/Sbusystem: Windows/entry: wwinmain (uincode)
The preceding two formats can be viewed in project> setting> link> project option.
The above subsystem and entry do not need to be set. If you only set/subsystem: Console
The default entry switch should be/entry: maincrtstartup by default.
Otherwise, if you define the main function in the application, your/subsystem switch is used by default.
It should be/system: Console
By default, the/subsystem and/entry switches match, that is
Console corresponds to maincrtstartup or wmaincrtstartup
Windows corresponds to winmain or wwinmain
However, we can also manually change them to make them not match.
For example, we can change it like this.
# Pragma comment (linker,/subsystem: Windows/entry: maincrtstartup) // set the entry address
Int main (INT argc, char * argv [])
{
MessageBox (null, hello, notice, mb_ OK );
Return 0;
}
When the linker sees the Windows option in/subsystem by default, it will automatically find winmain or wwinmain
However, you must specify the entry address to hide the default Console window when running the program!
The above is set using the # pragma command in the code, and one is directly in the development environment
Manually changed project-> setting-> link-> project option!
Having written so much, I feel a little confused. There is no way. I have never written anything before. Article So the wording may be poor. I hope you will forgive me.
1: If the console program has been written, you cannot change it. Write an API program, do not draw a window, then call the prepared console program with CreateProcess, and set the property to sw_hide.
2: you cannot use the console to write (consloe). You can use winmain as the portal. If you do not draw a window, no one else can see it. you only need to figure out how to hide your process in the taskbar.
3: If it is a console program, use the API function getstdhandle () to obtain the window handle of the console program, and then hide the window
4: // hide the Console
# Pragma comment (linker, "/subsystem: \" WINDOWS \ "/entry: \" maincrtstartup \"")
You need to write the console program to use this
Hide the console window in the console program!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.