visual-studio Windows unicode 命令行 argv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4101864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:42:10  来源:igfitidea点击:

Windows unicode commandline argv

c++visual-studiowinapiqtunicode

提问by Martin Beckett

So getting into the new millenia I rewrote my c++ code with:

所以进入新千年我重写了我的 C++ 代码:

int main(int argc, wchar_t **argv)

If built with either Unicode or MBCS options then when the app is run with a commandline arg, either directly or by dbl-click the filenames passed to argv[] are unreadable = in some mixture of chinese fonts.

如果使用 Unicode 或 MBCS 选项构建,那么当应用程序使用命令行参数运行时,直接或通过 dbl-click 传递给 argv[] 的文件名是不可读的 = 在某些中文字体的混合中。

Thanks for the comments - I will try and summaris(z)e them here for the search engine.

感谢您的评论 - 我会尝试在此处为搜索引擎汇总 (z)e 它们。

  1. wmain(int argc,char **argv)can only be used for a commandline (subsystem:console) app

  2. int winMain(int argc, wchar_t **argv)works for a gui (subsystem:windows) but the gui replaces it with it's own entry point. In the case of Qt this doesn't work

    qtmaind.lib(qtmain_win.obj) : error LNK2019: unresolved external symbol _main referenced in function _WinMain@16

  1. wmain(int argc,char **argv)只能用于命令行(子系统:控制台)应用程序

  2. int winMain(int argc, wchar_t **argv)适用于 gui(子系统:windows),但 gui 用它自己的入口点替换它。在 Qt 的情况下,这不起作用

    qtmaind.lib(qtmain_win.obj):错误 LNK2019:未解析的外部符号 _main 在函数 _WinMain@16 中引用

The solution seems to be use main(int arc,char **argv)or main(int argc,wchar_t**argv)but ignore the argv. Then call QApplication with argv or NULL - the argv is ignored as Qt internally calls GetCommandLine().
Then use app.arguments to return the parsed arguments.
These can then be converted back into wchar with Qt's string functions if needed.

解决方案似乎是使用main(int arc,char **argv)main(int argc,wchar_t**argv)忽略 argv。然后使用 argv 或 NULL 调用 QApplication - argv 被忽略,因为 Qt 内部调用 GetCommandLine()。
然后使用 app.arguments 返回解析后的参数。
如果需要,然后可以使用 Qt 的字符串函数将这些转换回 wchar。

 QApplication app(argc, (char**)argv);  or  QApplication app(argc,NULL);  
 QStringList args = app.arguments();

Sorry I didn't originally flag this Qt because I didn't think that was relevant.
If somebody wants to edit this to also include how to do this in MFC - please do.

抱歉,我最初没有标记这个 Qt,因为我认为这无关紧要。
如果有人想编辑它以包括如何在 MFC 中执行此操作 - 请执行。

回答by Mark Ransom

You need to name your entry point wmain: http://msdn.microsoft.com/en-us/library/fzc2cy7w(VS.80).aspx

您需要命名您的入口点wmainhttp: //msdn.microsoft.com/en-us/library/fzc2cy7w(VS.80).aspx

回答by Nemanja Trifunovic

You can use GetCommandLinefunction for that purpose.

为此,您可以使用GetCommandLine函数。

回答by Praetorian

Try this:

试试这个:

#include <tchar.h>

int _tmain( int argc, TCHAR **argv )
{
  return 0;
}

_tmainis defined as wmainwhen compiled with the UNICODE option and as mainwhen compiled with the MBCS option.

_tmain定义为wmain使用 UNICODE 选项main编译时和使用 MBCS 选项编译时。