6

Is it wrong to use the main function in C++ Visual Studio 2017 as following:

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

as my program can receive special characters.

Ron
  • 14,674
  • 4
  • 34
  • 47
Simz
  • 91
  • 1
  • 4
  • 6
    Yes it's wrong since that's now how `main` is supposed to be declared. If you're on Windows and using Visual Studio and its compiler look into using `wmain` instead (as [documented here](https://msdn.microsoft.com/en-us/library/6wd819wh.aspx)). – Some programmer dude Aug 29 '18 at 15:19
  • @OP do you have `int main(int argc, wchar_t* argv)` or `int main(int argc, wchar_t** argv)`? – NathanOliver Aug 29 '18 at 15:30
  • 2
    Possible duplicate of [What is the difference between wmain and main?](https://stackoverflow.com/questions/2438049/what-is-the-difference-between-wmain-and-main) – phuclv Aug 29 '18 at 15:41
  • @NathanOliver The OPs `wchar_t *argv[]` is exactly the same as `wchar_t **argv`. – Swordfish Aug 29 '18 at 15:43
  • @Swordfish The Q was edited by another user. Originally the user had `int main(int argc, wchar_t* argv)` which is not the same – NathanOliver Aug 29 '18 at 15:45
  • @NathanOliver Sorry then. – Swordfish Aug 29 '18 at 16:05

2 Answers2

6

Please go and read the remarks section on GetCommandLine:

ANSI console processes written in C can use the argc and argv arguments of the main function to access the command-line arguments. ANSI GUI applications can use the lpCmdLine parameter of the WinMain function to access the command-line string, excluding the program name. The main and WinMain functions cannot return Unicode strings.

Unicode console process written in C can use the wmain() or _tmain() function to access the command-line arguments. Unicode GUI applications must use the GetCommandLineW function to access Unicode strings.

To convert the command line to an argv style array of strings, call the CommandLineToArgvW function.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Swordfish
  • 12,971
  • 3
  • 21
  • 43
0

Yes, but if you need to perform input and output operations, perhaps you need to add std::setlocale(LC_ALL, "");, because by default it only supports the most basic English character output. example:

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;
}
Kargath
  • 450
  • 5
  • 15