MinGW没有wmain入口函数,为了获取宽字符的参数,可以用系统API函数GetCommandLineW。
main.cpp
#include <iostream> #include <windows.h> using namespace std; int main(int argc, const char *argv[], const char *evn[]) { // print w_char ios::sync_with_stdio(false); locale::global(locale("")); setlocale(LC_CTYPE, ""); wcout.imbue(locale("")); wcout << L"This is a test! 这是一个测试!" << endl << endl; //print args wcout << "argc: " << argc << endl; wchar_t **argv1 = CommandLineToArgvW(GetCommandLineW(), &argc); // MinGW w_char args for (int i = 0; i < argc; ++i) { wcout << "argv[" << i << "]: " << argv1[i] << endl; } LocalFree(argv1); wcout << endl; // print evn for (int i = 0; evn[i]!=NULL; ++i) { wcout << "evn[" << i << "]: " << evn[i] << endl; } return EXIT_SUCCESS; }
编译输出:
D:>g++ main.cpp -o test.exe D:>test.exe File1 -o File2 汉字 "a b c" This is a test! 这是一个测试! argc: 6 argv[0]: test.exe argv[1]: File1 argv[2]: -o argv[3]: File2 argv[4]: 汉字 argv[5]: a b c evn[0]: ALLUSERSPROFILE=C:ProgramData evn[1]: APPDATA=C:UsersYKAppDataRoaming evn[2]: CommonProgramFiles=C:Program Files (x86)Common Files evn[3]: CommonProgramFiles(x86)=C:Program Files (x86)Common Files evn[4]: CommonProgramW6432=C:Program FilesCommon Files evn[5]: COMPUTERNAME=YK-PC evn[6]: ComSpec=C:Windowssystem32cmd.exe evn[7]: FP_NO_HOST_CHECK=NO evn[8]: HOMEDRIVE=C: evn[9]: HOMEPATH=UsersYK evn[10]: LOCALAPPDATA=C:UsersYKAppDataLocal evn[11]: LOGONSERVER=\YK-PC evn[12]: NUMBER_OF_PROCESSORS=4 evn[13]: OS=Windows_NT evn[14]: Path=C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:Windows System32WindowsPowerShellv1.0;C:Program Files (x86)IntelOpenCL SDK2.0i nx86;C:Program Files (x86)IntelOpenCL SDK2.0inx64;C:MinGWin evn[15]: PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC evn[16]: PROCESSOR_ARCHITECTURE=x86 evn[17]: PROCESSOR_ARCHITEW6432=AMD64 evn[18]: PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 42 Stepping 7, GenuineIntel evn[19]: PROCESSOR_LEVEL=6 evn[20]: PROCESSOR_REVISION=2a07 evn[21]: ProgramData=C:ProgramData evn[22]: ProgramFiles=C:Program Files (x86) evn[23]: ProgramFiles(x86)=C:Program Files (x86) evn[24]: ProgramW6432=C:Program Files evn[25]: PROMPT=$P$G evn[26]: PSModulePath=C:Windowssystem32WindowsPowerShellv1.0Modules evn[27]: PUBLIC=C:UsersPublic evn[28]: SystemDrive=C: evn[29]: SystemRoot=C:Windows evn[30]: TEMP=C:UsersYKAppDataLocalTemp evn[31]: TMP=C:UsersYKAppDataLocalTemp evn[32]: USERDOMAIN=YK-PC evn[33]: USERNAME=YK evn[34]: USERPROFILE=C:UsersYK evn[35]: windir=C:Windows evn[36]: windows_tracing_flags=3 evn[37]: windows_tracing_logfile=C:BVTBinTestsinstallpackagecsilogfile.log D:>