【问题标题】:How to hide the console window in a Win32 project using Visual Studio 2010如何使用 Visual Studio 2010 在 Win32 项目中隐藏控制台窗口
【发布时间】:2012-10-24 14:34:12
【问题描述】:
我需要在应用程序启动期间创建一个没有控制台窗口或(任何其他窗口)的 exe 应用程序。
我为此尝试了以下方法:
- 使用 Visual Studio 2010,将 Win32 控制台应用程序创建为空项目。
- 在项目中添加了头文件“stdafx.h”
- 在项目中添加了一个cpp文件并添加了以下代码。
-
项目设置默认为visual stduio。
#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 字符集”,则会出现以下编译错误。
错误 C2731:“WinMain”:函数无法重载
我正在 Windows 7 64 位计算机和 Visual Studio Build 平台上构建应用程序为 x64。
提前感谢您的帮助。
【问题讨论】:
标签:
visual-studio-2010
visual-c++
【解决方案1】:
是的,当您使用有效的 UNICODE 构建时,入口点将 Unicode 字符串作为命令行参数。所以这需要一个不同的声明:
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nShowCmd)
或者你也可以使用#include <tchar.h>,这样不管怎样都可以,这些天没有太多的意义:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
【解决方案3】:
在 Visual Studio 中,创建一个新的“空项目”。添加一个名为“main.cpp”的新源文件。使用以下模板(假设您要使用 Unicode 构建):
/************
main.cpp
************/
#define UNICODE 1
#include <windows.h>
int APIENTRY wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nShowCmd)
{
// Process Return Codes
const int SUCCESS=0, FAILURE=1;
return SUCCESS;
}