Hide the implementation code of the Console program window in VC _c language

Source: Internet
Author: User
Tags posix win32

Here's how to hide the console window for the Win32 console application
Because this method is implemented by setting the compiler's link switch, let's look at the compilation
The link switch option for the device (that is, the linker option).
First, let's take a look at linker's/subsystem options.
The syntax for 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 option tells the operating system how to run the executable file
Console
Win32 character-mode applications, where this type of application produces a DOS-like
The console window of the window, if the main function in the application is main () or wmain (), by default
The application is a console application

Extensible Firmware Interface

and CPU specific architecture related to a parameter option, is not commonly used, not in detail here.

If you are interested in this, you can access the Intel home page to view related content
Native
Device drive options, 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:
The type of application does not generate the console window, which is created by the user itself, in short
is a standard Win32 application whose entry address is the WinMain () function or the address of the wWinMain () function.
If you define the main function as WinMain or wWinMain in the application type, by default the application is a

Win32 Application!
WindowsCE

Applications running on Windows CE

Major and minor (optional):

The

Major and minor version numbers, which are optional, which are decimal integers between 0~65535
You can see from above that if we build a Win32 console application, the/SUBSYSTEM option for linker should be
Console, can be in the VC development environment Project->setting->link->project option to see!
Next let's look at how the application works!
We know the program written in VC, the runtime is required to support the CC runtime. When we run a C/s program, the
Linker first looks for the application's startup function, for example:
If you set up a console program, The compiler gets the link switch that is the form
/subsystem:console/entry:maincrtstartup (ANSI)
/subsystem:console/entry:wmaincrtstartuup (Unicode)
If you set up a Win32 application, the compiler gets the link switch will be the form
/subsystem:windows/entry:winmain (ANSI)
/sbusystem:windows/ Entry:wwinmain (Uincode)
The two forms above can be seen again in Project->setting->link->project option
The above subsystem and entry do not need to be set, if you only set the/subsystem:console
, then the default entry switch should be/entry:maincrtstartup by default
instead, If you define the main function in your application, by default, your/subsystem switch
should be/system:console
by default/subsystem and/entry switches are matched, which is
The console corresponds to mainCRTStartup or wmainCRTStartup
Windows corresponds to WinMain or wWinMain
But we can also make them not match by manual modification
For example, we can change this

Copy Code code as follows:

#pragma COMMENT (linker,/subsystem:windows/entry:maincrtstartup)//Set entry address
int main (int argc, char* argv[])
{
MessageBox (NULL, hello, notice, MB_OK);
return 0;
}

By default, when the linker sees the Windows option under/subsystem, it automatically looks for WinMain or wWinMain
But we force the entry address to be specified so that the default console window is hidden when the program is run!
The above is set in code using the #pragma directive, and one is directly in the development environment

Project->setting->link->project option in manual change!

Write so much, oneself are a little feel disorderly, have no way, have not written any article before, so wording may not be very good, hope everybody forgive me.

1: If the console program is already written and cannot be changed, it is OK. Write an API program, do not draw a window, and then use CreateProcess to invoke the written console program, the attribute set to Sw_hide can be.
2: Can not use the console to write (Consloe), to use WinMain to do the entrance on it, do not draw the window, others will be invisible. You just have to find a way to hide your process in the taskbar.
3: If it is the console program, use the API function GetStdHandle () to obtain the console program's window handle, and then in the hidden window
4://This sentence to hide the console
#pragma comment (linker, "/ Subsystem:\ "Windows\"/entry:\ "Maincrtstartup\")
If you want to write a console program, use this
to 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.