Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Having problem using C code in C++ using Qt
Forum Updated to NodeBB v4.3 + New Features

Having problem using C code in C++ using Qt

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 886 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    md2012
    wrote on 8 Apr 2018, 17:09 last edited by md2012 4 Aug 2018, 17:11
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Apr 2018, 17:43 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • A Offline
        A Offline
        ambershark
        wrote on 9 Apr 2018, 06:59 last edited by
        #3

        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.

        My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

        1 Reply Last reply
        3

        1/3

        8 Apr 2018, 17:09

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved