How do I access arguments for a new Main Function in C++?

半世苍凉 提交于 2020-03-05 02:55:07

问题


I have a new main function that I asked the linker to point to in the Visual Studio.

I can correctly execute this function. However the command line arguments that I am getting is not correct.

Currently this is my signature

int NewMain(int argc, const char* argv[])
    {
    cout << "New Main" << endl;
    for (int i = 0; i < argc; ++i)
        {
        cout << argv[i] << "\n";
        }
    return 0;
    }

However when I use the same function with the standard main, I am getting all the arguments.


回答1:


When you specify an entry point to the linker, the function that gets called is not passed any arguments. Its signature is int entrypoint(void);

If you want the command line, you can retrieve it with GetCommandLine. If you want to parse that into arguments, you can either do so on your own, or use CommandLineToArgvW -- but note that this is only available in a wide-character version, so if you want a standard command line (using chars, not wchar_ts) you have to do that on your own.




回答2:


A community addon from msdn:

Custom Entry Point Function with Command Line Arguments
It is not possible to specify a custom entry point with command line arguments since when you specify such an entry point mainCRTStartup is no longer called and thereby does not provide the entry point with the command line arguments such as int argc and char* argv[].

http://msdn.microsoft.com/en-us/library/f9t8842e%28v=vs.80%29.aspx



来源:https://stackoverflow.com/questions/13921924/how-do-i-access-arguments-for-a-new-main-function-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!