Environment: VC6, SP5, 95/98/NT/Win2K
It seems to be working, but don't trust it completely. If you want to test some files, please back them up first.
One day, for some reason I wanted to make a Win32 console program that doesn't pop up the console windows. I tried many ways and I found it! Linker options (/SUBSYSTEM:Windows /ENTRY:main) can do that, but I wanted to change the already compiled program's subsystem, so I examined the binary file and found it.
Probably, Windows (OS) checks the PE (Portable Executable) file's IMAGE_OPTIONAL_HEADER.Subsystem. There are some other values, but only we need IMAGE_SUBSYTEM_WINDOWS_GUI(2) or IMAGE_SUBSYSTEM_WINDOWS_CUI(3). If the value is ..._GUI, console windows doesn't pop up; but, if the value is ..._CUI, console windows pops up.
The following picture shows one PE file's hex dump. The subsystem's value is highlighted (3, so CUI) but doesn't have absolute offset.
Where to Use this Code
If you change console -> gui, you can get away from the console window, and all console I/O functions (ex:printf) would fail. If you change gui -> console, you can use console I/O functions for debugging. Although you may need this rarely, generally the console program's size is smaller than the GUI with its default setting.
You can find the PE specs in MSDN's Sepcifications Section. WINNT.H contains IMAGE_SUBSYSTEM_WINDOWS_GUI, IMAGE_SUBSYSTEM_WINDOWS_CUI.
MiscellanyThe Linker options (/SUBSYSTEM:Windows /ENTRY:main) method may have been on codeguru already, but I didn't know. Oops! It could be a waste of labor, but it's still helpful. When I found the solution, SOMEONE said why don't you post it on codeguru. So, I looked for how to (this the first time for me) and where to post, and I found that. (Thanks to JaeJun Oh for some inspiration. And he is the SOMEONE.)
DownloadsDownload demo project - 11.8 Kb
Download source - 5.32 Kb