Breaking down WinMain's cmdLine in old style main()'s arguments

后端 未结 4 736
旧巷少年郎
旧巷少年郎 2020-12-17 09:58

I want to convert WinMain\'s cmdLine argument to argc and argv so I can use the argument parsing function I wrote for con

相关标签:
4条回答
  • 2020-12-17 10:15

    If you want plain int argc, char** argv arguments you have to do it on your own.

    void fetchCmdArgs(int* argc, char*** argv) {
        // init results
        *argc = 0;
    
        // prepare extraction
        char* winCmd = GetCommandLine();
        int index = 0;
        bool newOption = true;
        // use static so converted command line can be
        // accessed from outside this function
        static vector<char*> argVector;
    
        // walk over the command line and convert it to argv
        while(winCmd[index] != 0){
            if (winCmd[index] == ' ') {
                // terminate option string
                winCmd[index] = 0;
                newOption = true;
    
            } else  {
                if(newOption){
                    argVector.push_back(&winCmd[index]);
                    (*argc)++;  
                }
                newOption = false;
            }
            index++;
        }
    
        // elements inside the vector are guaranteed to be continous
        *argv = &argVector[0];
    }
    
    
    // usage
    int APIENTRY WinMain(...) {
        int argc = 0;
        char** argv;
        fetchCmdArgs(&argc, &argv);
    }
    
    0 讨论(0)
  • 2020-12-17 10:19

    CommandLineToArgvW looks like it would be helpful here.

    0 讨论(0)
  • 2020-12-17 10:22

    Based on Denis K response

    See: https://msdn.microsoft.com/library/dn727674.aspx

    This adds Windows specific entrypoint to clasic startpoint of your app:

    int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
    {
        return main(__argc, __argv);
    }
    
    0 讨论(0)
  • 2020-12-17 10:29

    If you are using Microsoft compiler, there are public symbols __argc, __argv and __wargv defined in stdlib.h. This also applies to MinGW that uses Microsoft runtime libraries.

    0 讨论(0)
提交回复
热议问题