Use Unicode and Hide the Console in Windows

本文介绍如何在Windows环境下使用Unicode编写程序,特别是在Visual Studio中设置程序为Unicode的方式。文章还详细解释了如何处理非英文参数,并提供了一个使用wchar_t类型的wmain函数示例,以及如何在隐藏控制台的情况下正确链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 If one would like to make his program being able to accept the arguments which is not English, the Unicode is requisite. That would make your software be more international. ( For me, it is instinct to use Chinese as file name and so forth.)

 

   To use Unicode in windows, One could set your program as unicode in Visual Studio:

 

 

 

 

That your could use unicode in your code.

 

If your program arguments could be Chinese, the main should be the form:

 

 

int wmain(int argc, wchar_t *argv[]) 

 

 

in windows with unicode.

 

 

but, if you would like to hide console (for example, if you use Qt librarie with Visual Studio), the well-known solution :

 

 

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

 

 

would not work.

 

there would be :

 

 

error LNK2001: unresolved external symbol _main

 

 

To solve it as valid is very tricky, that is :

 

 

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup")

 

 

To demostrate i, the code be :

 

 

#include <windows.h>

#include <locale.h>
#include <wchar.h>



#ifndef _DEBUG
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:wmainCRTStartup")
#endif

int wmain(int argc, wchar_t *argv[]) 
{
 int k;
 
 k = 1;
 while(k < argc)
 { 
  wchar_t bufferW[256];
  memset(&bufferW[0], 0, 256*sizeof(wchar_t));
  wcsncpy(&bufferW[0], argv[k], 256);  

#ifdef _DEBUG  
  setlocale(LC_ALL, "");
  wprintf(TEXT("%s\n"), &bufferW[0]);
  
#else
  MessageBox(NULL, &bufferW[0], TEXT("參數"), MB_OK);
#endif
  k++;
 }/*while*/

 return 0;
}/*wmain*/

 

 

and set the input argument as :

 

 

 

(Of course, do not forget set the character set as Unicode in the configuration properties -> General)

 

In the debug mode, the output would be in the console :

 

 

 

In the release mode, the console would be hidden, and the output be :

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值