如何使用 Visual Studio 2010 在 Win32 项目中隐藏控制台窗口
我需要在应用程序启动期间创建一个没有控制台窗口或(任何其他窗口)的 exe 应用程序.
I need to create an exe application with no console window or (any other window) during the start up of the application.
我为此尝试了以下方法:
I tried the below for this:
- 使用 Visual Studio 2010 创建了一个 Win32 控制台应用程序作为一个空项目.
- 在项目中添加了一个头文件stdafx.h"
- 向项目添加了一个 cpp 文件并添加了以下代码.
项目设置是默认的visual stduio.
- Using Visual Studio 2010, created a Win32 Console Application as an Empty Project.
- Added a header file "stdafx.h" to the project
- Added a cpp file to the project and added the below code.
The project settings are visual stduio default.
#include "stdafx.h"
#include <windows.h>
#include "TlHelp32.h"
#include <iostream>
#include <string>
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
return 0;
}
以上代码编译良好.
但是如果我将字符集更改为使用 Unicode 字符集",则会出现以下编译错误.
But if I change the Character Set to "Use Unicode Character Set", I am getting the following compilation error.
错误 C2731:'WinMain':函数不能重载
error C2731: 'WinMain' : function cannot be overloaded
我正在 Windows 7 64 位计算机和 Visual Studio Build 平台作为 x64 构建应用程序.
I am building the application on a Windows 7 64 bit computer and Visual Studio Build platform as x64.
预先感谢您的帮助.
是的,当您使用有效的 UNICODE 构建时,入口点采用 Unicode 字符串作为命令行参数.所以这需要一个不同的声明:
Yes, when you build with UNICODE in effect then the entrypoint takes a Unicode string for the command line argument. So that requires a different declaration:
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nShowCmd)
或者您可以使用 #include <tchar.h>
以便它以任何方式工作,这些天没有太多意义:
Or you can use #include <tchar.h>
so it works either way, not much point to it these days:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)