Important: Please read the Qt Code of Conduct -

Having problem using C code in C++ using Qt

  • Hello dear Qt developers.
    I need some C++ expertise help over here.
    Well I'm back to work with my vpn client for Windows platform and want to use Ras library (ras.h) to handle my connection.
    found good examples here in C language:
    Ras link 1
    well when i use library in synchronous mode every thing is fine but there will be no error handling or any controls over connection status so there is a part 2 to the guide that i attached.
    in asynchronous mode there will be a notifier that can handle every thing pretty well about ras connections.
    and i get this errors:

    error: cannot convert 'char*' to 'LPWSTR {aka wchar_t*}' for argument '2' to 'DWORD RasGetErrorStringW(UINT, LPWSTR, DWORD)'
             RasGetErrorString((UINT)dwError, szRasString, 256);


    error: invalid conversion from 'void (__attribute__((__stdcall__)) *)(UINT, tagRASCONNSTATE, DWORD) {aka void (__attribute__((__stdcall__)) *)(unsigned int, tagRASCONNSTATE, long unsigned int)}' to 'LPVOID {aka void*}' [-fpermissive]
         if ((Ret = RasDial(NULL, NULL, &RasDialParams, 0, &RasDialFunc, &hRasConn)) != 0)

    Here is the full code which is isolated in separated project :

    QT -= gui
    CONFIG += c++11 console
    CONFIG -= app_bundle
    LIBS += -lrasapi32
    SOURCES += main.cpp
    #include <QCoreApplication>
    extern "C" {
    #include <ras.h>
    #include <raserror.h>
    #include <stdio.h>
    // Callback function RasDialFunc()
    void WINAPI RasDialFunc(UINT unMsg, RASCONNSTATE rasconnstate, DWORD dwError)
        char szRasString[256]; // Buffer for error string
        if (dwError)
            RasGetErrorString((UINT)dwError, szRasString, 256);  //Error #1 triggers here
            printf("Error: %d - %s\n",dwError, szRasString);
        // Map each of the RasDial states and display on the
        // screen the next state that RasDial is entering
        switch (rasconnstate)
            case RASCS_ConnectDevice:
                printf ("Connecting device...\n");
            case RASCS_DeviceConnected:
                printf ("Device connected.\n");
            // Add other connection activities here
    //        ...
                printf ("Unmonitored RAS activity.\n");
    void DialUp() //
        DWORD Ret;
        RASDIALPARAMS RasDialParams;
        HRASCONN hRasConn;
        // Always set the size of the RASDIALPARAMS structure
        RasDialParams.dwSize = sizeof(RASDIALPARAMS);
        hRasConn = NULL;
        // Setting this field to an empty string will allow
        // RasDial to use default dialing properties
        lstrcpy(RasDialParams.szEntryName, L"test");
        lstrcpy(RasDialParams.szPhoneNumber, L"867-5309");
        lstrcpy(RasDialParams.szUserName, L"jenny");
        lstrcpy(RasDialParams.szPassword, L"mypassword");
        lstrcpy(RasDialParams.szDomain, L"mydomain");
        if ((Ret = RasDial(NULL, NULL, &RasDialParams, 0, &RasDialFunc, &hRasConn)) != 0)  //Error #2 triggers here
               printf("RasDial failed with error %d\n", Ret);
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        return a.exec();

    any idea,tip,document and references would be welcome.

  • Lifetime Qt Champion


    You are using the wrong type to get the message as the error states.
    Use what is suggested and in a second step you can transform it in a QString if needed.

  • Moderators

    The first error you are mixing unicode (wide char) and regular strings, which of course you can't do. @SGaist explained that above already, just giving you a bit more of a hint on why your types were wrong.

    The second error means you are mixing calling conventions. So you are passing a pointer to a function that is defined with stdcall calling convention to one that is probably cdecl. The offending item is the WINAPI in the definition. That is basically just a __stdcall redefinition. It's what is setting your calling convention on that call back function.

Log in to reply