Strings comparison

Hi,

I'm working on a Win app that opens another Win app with additional arguments.
The thing is that I don't want a user to be able to open the 2nd one without turning on the 1st one. It should always be opened by app 1. Therefore, app 1 passes some string (password) as an argument to app 2. In app 2 there is comparison between two strings, but for some reason they are seen to be "not equal".

In 2nd app I use following piece of code to compare these two strings:
1
2
3
4
5
6
7
8
9
10
11
LPWSTR *szArgList;
  int argCount;
  wchar_t my_pass[] = L"12345678\0";

  szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);

  //file
  std::ofstream out("output.txt");
  out << wcscmp(my_pass, szArgList[2]);
  out.close();
  //file 

I thought that in the output file I'll see 0 as a result, but I got 1. Do you have an idea what I'm doing wrong?
Last edited on
Since you use wchar_t you also need to use wofstream.
closed account (48bpfSEw)
L"12345678\0"

The "\0" in your string is not required since a string in CPP has allready an \0 at the end.
> Since you use wchar_t you also need to use wofstream.
No, the OP is trying to print out the result of the function wcscmp(). He was not trying to print out any wchar_t string.
Why don't you print the args your program is receiving ?
https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx
Thanks for comprehensive replies.

Necip, thanks for suggestion, I already removed "\0". You are totally correct here.

Thomas1965, indeed I was not printing any wchar_t string. However, I tried your advice and the problem is actually there. In txt file I got:
args: 14
App1 argument 0: G:\bin\cefsimple.exe
App1 argument 1: --type=renderer
App1 argument 2: --no-sandbox
App1 argument 3: --disable-databases
App1 argument 4: --lang=en-US
App1 argument 5: --lang=en-US
App1 argument 6: --log-file=G:\bin\debug.log
App1 argument 7: --device-scale-factor=1
App1 argument 8: --num-raster-threads=2
App1 argument 9: --content-image-texture-target=3553,3553,3553,3553,3553,3553,3553,3553,3553,3553,3553,3553,3553,3553
App1 argument 10: --video-image-texture-target=3553
App1 argument 11: --disable-accelerated-video-decode
App1 argument 12: --channel=7076.1.1397704486\1723621388
App1 argument 13: /prefetch:673131151

As I was using App1 to open App2 which was Chromium open-source version for developers, i.e. CEF (Chromium Embedded Framework), it passed also some arguments I wasn't aware of.

Now the question is: am I able to send some arguments on certain position in command line? Argument 14 (or maybe I should say 15 as we're counting from 0) seems to be first one available.
Last edited on
In general you should not expect command-line arguments to be in any particular order. (If you do, your program will break when the calling program changes its output!)

Find the argument you wish to handle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  int argCount;
  LPWSTR *szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
  if (!szArgList)
  {
    ...
  }

  std::u16string tag = "/prefetch:";
  std::u16string pwd;
  for (int n = 1; n < argCount; n++)
    if (std::u16string( szArgList[ n ] ).compare( 0, tag.size(), tag ) == 0)
    {
      pwd = szArgList[ n ] + tag.size();
      break;
    }
  if (pwd.empty())
  {
    ...
  }

  //use pwd 

Hope this helps.
Duoas, thanks for advice. I'll follow it.

In the end, it was the problem of the architecture of 2nd app (CEF) as I mentioned earlier. I was getting arguments of incorrect part of the program, i.e. renderer, instead of browser itself. After reorganizing some part of code I got proper arguments that I actually was passing from the 1st app.

Thanks for your help. Couldn't make it without help of all of you.
Last edited on
Topic archived. No new replies allowed.