Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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 :

    //.Pro
    QT -= gui
    
    CONFIG += c++11 console
    CONFIG -= app_bundle
    
    LIBS += -lrasapi32
    
    DEFINES += QT_DEPRECATED_WARNINGS
    
    SOURCES += main.cpp
    
    
    //Main
    #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);
            return;
        }
    
        // 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");
                break;
            case RASCS_DeviceConnected:
                printf ("Device connected.\n");
                break;
            // Add other connection activities here
    //        ...
            default:
                printf ("Unmonitored RAS activity.\n");
                break;
        }
    }
    
    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);
               return;
           }
    }
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Dialup();
        return a.exec();
    }
    
    

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


  • Lifetime Qt Champion

    Hi,

    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