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

后端 未结 4 738
旧巷少年郎
旧巷少年郎 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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 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);
    }
    

提交回复
热议问题