1

I am writing a small C++ program which gets as input from the program command line, a string to later be passed to another function.

The problem is that this function expects to get a LPWSTR (wchar_t*) type variable, and the regular char** argv is an array of elements from the type of char*.

Is there any way to get the argv variable as a LPWSTR* type?

Thanks

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Ohav
  • 371
  • 2
  • 13
  • Are you asking how to transform a string `char` into a string of `wchar_t`? Maybe this helps: https://stackoverflow.com/questions/11576846/convert-ascii-string-to-unicode-windows-pure-c/11576927 – Jabberwocky Sep 08 '21 at 12:58
  • @Jabberwocky Thanks. This is another approach but seems like the one with `GetCommandLineW` and `CommandLineToArgvW` is simpler in the case of the argv array. – Ohav Sep 08 '21 at 13:10
  • 5
    To get the argv variable as a `LPWSTR*`, use `wmain` instead of `main`. – Raymond Chen Sep 08 '21 at 15:30
  • 4
    Your options: `1` Use [wmain](https://learn.microsoft.com/en-us/cpp/c-language/using-wmain) as pointed out by Raymond. `2` Use the symbols [__argv, __wargv](https://learn.microsoft.com/en-us/cpp/c-runtime-library/argc-argv-wargv) provided by Microsoft's CRT implementation. `3` Call [CommandLineToArgvW](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw) on the return value of [GetCommandLineW](https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getcommandlinew). – IInspectable Sep 08 '21 at 16:35

1 Answers1

3

A simple solution can be acheived by combining both GetCommandLineW() and CommandLineToArgvW() in the following way:

int main() {
    int argc = 0;
    LPWSTR* pArgvW = CommandLineToArgvW(GetCommandLineW(), &argc);
    // use pArgvW as needed...
    LocalFree(pArgvW);
    ...
    return 0;
}

Reading GetCommandLineW and CommandLineToArgvW documentation might also help.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ohav
  • 371
  • 2
  • 13