C++ _tWinMain 运行 不止一次?

C++ _tWinMain run more than one times?

我正在使用 DuiLib 和 Cef。
我喜欢我的 _tWinMain 调用了 2 次。
1:When 我打开应用程序,main() 被调用了;
2 当我创建一个 Window 时,main() 被调用了 2nd。

这是一些代码:

vector<wstring> StartArgs;
map<wstring, wstring> argsMaps;
void GetArgMap() {
    for (int i = 1; i < StartArgs.size(); ++i) {
        wstring argStr = StartArgs[i];
        vector<wstring> argVec;
        split(argStr, L':', argVec);
        if (argVec.size() >= 2) {
            wstring k = argVec[0];
            wstring v = argVec[1];
            argsMaps.insert(map<wstring, wstring>::value_type(k,v));
        }
    }
}

wstring GetArgForKey(wstring key) {

    wstring ret = argsMaps[key];
    if (lstrcmpW(ret.c_str(), L"")) {
        return ret;
    }
    else {
        return L"null";
    }
}

bool CheckParamAvialble(wstring param) {
    if (lstrcmpW(param.c_str(), L"null")) {
        return true;
    }
    else {
        return false;
    }
}

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine,
    int nCmdShow)
{   

    ShowMessageBox(L"this is test alert!", L"WARNING");
    int argCount = 0;
    LPWSTR cmdline = GetCommandLine();
    //cmdline is  [m:1 h:0 a:500000 n:xxx c:xxx u:usera o:TOKEN]
    LPWSTR* szArgList = CommandLineToArgvW(cmdline, &argCount);
    for (int i = 0; i < argCount; ++i) {
        wstring a = szArgList[i];
        StartArgs.push_back(a);
    }

    if (argCount >= 7) {
        wstring mString = GetArgForKey(L"m");
        if (!CheckParamAvialble(mString))
        {
            ShowMessageBox(L"arg m is wrong!", L"Error");
            return 0;
        }
        /*
            do something with other args
        */
            CPaintManagerUI::SetInstance(hInstance);
            InitResource();
            HRESULT Hr = ::CoInitialize(NULL);
            if (FAILED(Hr)) return 0;
            CefMainArgs args(hInstance);
            CefRefPtr<SimpleApp> app(new SimpleApp);
            int exitCode = CefExecuteProcess(args, app, NULL);
            if (exitCode >= 0)
            {
                return exitCode;
            }
            CefSettings settings;
            CefInitialize(args, settings, app.get(), NULL);
            CefRefPtr<CefCommandLine> command_line;
            command_line = CefCommandLine::CreateCommandLine();
            command_line->AppendSwitch("no-proxy-server");
            MainForm *pFrame = new MainForm(_T("Forms\MA_MainForm.xml"));
            if (pFrame == NULL) return 0;
            pFrame->Create(NULL, _T("MainForm"), UI_WNDSTYLE_FRAME, WS_EX_STATICEDGE | WS_EX_APPWINDOW, 0, 0, 600, 800);//when progrom go here,i got 2nd MessageBox[this is test alert!]
            pFrame->CenterWindow();
            CefRunMessageLoop();
            CefShutdown();
            return 0;
    }
    else 
    {
        ShowMessageBox(L"something wrong and exit", L"Error");
        return 0;
    }
}

奇怪的是
第二次调用 main() 不会触发我的断点,它只会再次显示消息框[这是测试警报!]。
如果我显示另一个 window,它将显示第三个消息框[这是测试警报!]
只是多次显示消息框,没有中断,没有异常。
非常感谢。

您是否阅读过 Chromium Embedded Framework 的工作原理?它将创建多个进程,通常是通过产生主可执行文件的额外副本。发生这种情况时,您将获得一个显示 "this is a test alert" 对话框的不同进程。由于这是一个不同的进程,除非您将调试器配置为调试任何派生的子进程,否则您的断点将不会命中。

结果是您所看到的是预期的行为。