How to remove command line from a compiled program?

本小妞迷上赌 提交于 2019-12-24 10:46:42

问题


I have recently started a tutorial to learn how to code GUI using Windows API and I have come upon an unexpected question which I think is kind of silly. I am using Code::Blocks with OpenWatcom compiler as default and I have created a simple GUI program compiling and linking well. The problem is when I try to launch the program, even from the release version something like the command line shows up behind the window of my program, like I tried to run it through the compile & run option of the Code::Blocks. Is there any way to remove the command line from showing up?

EDIT: It is not a problem with my main definition. This is my main definition: |

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)    

回答1:


In Windows, the PE executable format has a flag that indicates whether the executable mode is "console mode" or "GUI mode". If "console mode", then the OS will attach a console window (opening a new one if necessary) whenever the program is run.

There will be a linker setting somewhere in your build environment that controls whether the EXE you generate is marked as console or GUI.




回答2:


This is controlled by the /SUBSYSTEM linker option. It is currently set to CONSOLE and you need to change it to WINDOWS. The documentation mandates how the main needs to be changed:

Application does not require a console, probably because it creates its own windows for interaction with the user. If WinMain or wWinMain is defined for native code, or WinMain(HISTANCE *, HINSTANCE *, char *, int) or wWinMain(HINSTANCE *, HINSTANCE *, wchar_t *, int) is defined for managed code, WINDOWS is the default.

Specifically for Code::Blocks, the linker option can be changed by this process:

In Code::Blocks simply open the Project->Properties dialog, switch to the "Build Targets" tab and change the "Type" of the build targets you want to modify to "GUI Application" or "Console Application". No need to worry about the "main" function.




回答3:


Instead of a main function, you need to use Win32's standard entry point WinMain:

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow);

See: http://sol.gfxile.net/wintut/ch2.html

Or, if you can't recompile, in Windows 7 you can just do:

START myProgram {enter}

See: http://support.microsoft.com/kb/126410



来源:https://stackoverflow.com/questions/9203539/how-to-remove-command-line-from-a-compiled-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!