在主函数中使用wchar_t

using wchar_t in the main function

本文关键字:wchar 函数      更新时间:2023-10-16

在Visual Studio 2017中使用main函数C++如下所示是错误的吗?

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

因为我的程序可以接收特殊字符。

请去阅读GetCommandLine上的备注部分:

用 C 编写的 ANSI 控制台进程可以使用 main 函数的 argc 和 argv 参数来访问命令行参数。ANSI GUI 应用程序可以使用 WinMain 函数的 lpCmdLine 参数来访问命令行字符串,不包括程序名称。main 函数和 WinMain 函数不能返回 Unicode 字符串。

用 C 编写的 Unicode 控制台进程可以使用wmain()_tmain()函数来访问命令行参数。Unicode GUI 应用程序必须使用 GetCommandLineW 函数来访问 Unicode 字符串。

若要将命令行转换为 argv 样式的字符串数组,请调用 CommandLineToArgvW 函数。

是的,但是如果您需要执行输入和输出操作,也许您需要添加std::setlocale(LC_ALL, "");,因为默认情况下它仅支持最基本的英文字符输出。 例:

int wmain(int argc, wchar_t* argv[])
{
std::setlocale(LC_ALL, "");
wstring str = argv[1]; //if argv[1] contains unicode characters, such as"你好"
wcout << str << endl;
return 0;
}