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. How would you guys connect C Library/Function (incl. Callback) to your c++ code?
Forum Updated to NodeBB v4.3 + New Features

How would you guys connect C Library/Function (incl. Callback) to your c++ code?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.0k Views 1 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.
  • S Offline
    S Offline
    SeppyQT
    wrote on 1 Jul 2019, 11:24 last edited by
    #1

    Dear guys,

    this question is rather theoretic question but I have already encountered this problem.

    There are some C Libraries like CURL, OpenSSL, WolfSSL, aso, which have Functions which expect a Function pointer as Argument.

    Since you can't pass Member Function Pointers and you can't pass Lambdas with captures in it, how do you work around?

    as Example:

    SSL_CTX_set_cookie_verify_cb(ctx, &verify_cookie);
    

    this function accepts a callback Function pointer

    int(*)(SSL*,unsigned char*,unsigned int);
    

    In most curl functions you can work around by passing this pointer as void* userdata to a static or standalone function which forwards back to class. But in this example it is not possible. How do you implement C Functions in your C++ code?

    Greetings

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 1 Jul 2019, 13:14 last edited by mpergand 7 Feb 2019, 10:33
      #2

      Hi,

      The simplest method is to use a static function with the correct signature in a cpp class .

      here what i'm doing for two callbacks functions from the Mac Coreaudio midi API.

      static void  MIDINotifyProc(const MIDINotification *msg, void *refCon);
      static void  MIDIReadProc(const MIDIPacketList *pkList, void *refCon, void *con);
      

      You can implement a pure C function as well, but you may need to declare the function as extern "C" for correct linkage (not tested, hope i'm not wrong :-) )

      1 Reply Last reply
      5

      1/2

      1 Jul 2019, 11:24

      • Login

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