c++windowscommand-lineprogram-entry-pointwinmain

Command line arguments parsing is different in Windows XP vs Windows 10


Consider the command line argument:

D:\work\test.exe -a -p

My C++ application's main() receives the arguments as follows:

  1. Windows 10

    argc = 3  
    
    argv[0] = "D:\work\test.exe"
    
    argv[1] = "-a" 
    
    argv[2] = "-p"
    
  2. Windows XP

    argv = 3    
    
    argv[0] = "test.exe"
    
    argv[1] = "-a"   
    
    argv[2] = "-p"
    

The argument argv[0] is being parsed differently. My application needs them to be same.
Is there any way to get the entire path in Windows XP as well?
I have tried both main and winmain and the result is the same.


Solution

  • You can determine the full path of the application's .exe file like this:

    WCHAR *path = new WCHAR [32768]; // allow for long path names on Win 10
    GetModuleFileNameW (NULL, path, 32768);
    ...
    delete [] path;
    

    Documentation