c++stdcall

What is advantage/use of __stdcall?


Before posting this question, I have already read some documentation, SO answers and watched some videos to understand __stdcall. So far I have understood that it's a calling convention and specifies to push parameters from right to left on the stack. This also signifies when to clear the stack.

But I am still not able to understand the advantage and cases where I should use __stdcall.

I have come across the following code, which is called when an OPC-UA client is closed. I see that this should follow the __stdcall calling convention but why? What may have happened if __stdcall was not specified for the following method?

OpcUa_StatusCode __stdcall opcua_client::onShutdownMessage(OpcUa_Handle hApplication, OpcUa_Handle hSession, OpcUa_String strShutdownMessage, void* extraParam)
{
    opcua_client* pOpcClient = NULL;
    if (extraParam)
    {
        try
        {
            pOpcClient = qobject_cast <opcua_client*>((QObject*)extraParam);
            throw(55);
        }
        catch (int exeption)
        {

        }
    }

    OpcUa_StatusCode uStatus = OpcUa_Good;

    QString strShutDownMsg = QString::fromUtf8(OpcUa_String_GetRawString(&strShutdownMessage));
    bool bOK;
    OpcUa_StatusCode uClientLibStatus = uClientLibStatus = strShutDownMsg.toUInt(&bOK, 16);

    if (pOpcClient)
    {
        if (uClientLibStatus > 0)
            pOpcClient->process_onShutdownMessage(uClientLibStatus);
    }
    else
    {
        return uStatus;
    }

    return uStatus;
}

Solution

  • opcua_client::onShutdownMessage looks like callback function. That is, a function that is sent to some API which calls the function at a later time. The callback function must then have the calling convention expected by the API, in this case __stdcall. Since __stdcall is the default calling convention on the Win32 platform, it does not have to be specified when building for Win32.