windows 在旧式 main() 的参数中分解 WinMain 的 cmdLine

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2346328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:01:58  来源:igfitidea点击:

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

c++cwindowswinapi

提问by uu.

I want to convert WinMain's cmdLineargument to argcand argvso I can use the argument parsing function I wrote for console applications.

我想将WinMaincmdLine参数转换为argcargv所以我可以使用我为控制台应用程序编写的参数解析函数。

This would be trivial except that I want to support "quotes" too. For example:

除了我也想支持“引用”之外,这将是微不足道的。例如:

test.exe test1 test2 "testing testing"

test.exe test1 test2 "testing testing"

should be

应该

argv[0] = "test.exe"; argv[1] = "test1"; argv[2] = "test2"; argv[3] = "testing testing";

I realize that cmdLine doesn't have the program name (the argv[0]); this doesn't matter I can use a dummy value.

我意识到 cmdLine 没有程序名称(argv[0]);这没关系我可以使用虚拟值。

I was thinking of doing it with a regex, (("[^"]+")\s+)|(([^\s]+)\s*)I'm not sure how well it would work though.. Probably not very well? Is there any function to do that in the windows api? Thanks

我想用正则表达式来做,(("[^"]+")\s+)|(([^\s]+)\s*)但我不确定它的效果如何..可能不是很好?windows api 中是否有任何功能可以做到这一点?谢谢

采纳答案by sean e

CommandLineToArgvWlooks like it would be helpful here.

CommandLineToArgvW看起来在这里会有帮助。

回答by Denis K

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

如果您使用的是 Microsoft 编译器,则有公共符号__argc__argv__wargvstdlib.h. 这也适用于使用 Microsoft 运行时库的 MinGW。

回答by Tomas Tintera

Based on Denis K response

基于 Denis K 响应

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

请参阅:https: //msdn.microsoft.com/library/dn727674.aspx

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

这将特定于 Windows 的入口点添加到您的应用程序的经典起点:

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char*, int nShowCmd)
{
    return main(__argc, __argv);
}

回答by user3757954

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

如果你想要普通的 int argc, char** argv 参数,你必须自己做。

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);
}