Thread: Exporting functions via a struct

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735

    Question Exporting functions via a struct

    Anyone tried this before? would I need to make an API macro at all or is the below example fine?

    Code:
    typedef struct apihandle apihandle_t;
    typedef struct apiname {
      int (*begin)( apihandle_t *handle );
      int (*end)( apihandle_t *handle );
    } apiname_t;
    
    int apinameOpen( apiname_t* api );
    int apinameOpen( apiname_t* api );
    For the why of this it's because this api will wrap around multiple routes to doing the same thing (like there is OpenGL & DirectX) and a struct seems the simplest way t, basically it attempts to acquire one route and if it fails attempts to acquire the next.

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Before I forget about it again, someone reported an issue with the code I posted a LONG time ago on this thread: Potentially useful code
    but I forgot to take a look at it, well now I have and have fixed the issue, for the sake of forcing the existance of macros like INT_DIG this code can be used/referenced, detecting the bits will need an alternative macro though.

    Code:
    #include <stdio.h>
    #include <limits.h>
    #include <inttypes.h>
    
    typedef unsigned int ui_t;
    typedef long long ll_t;
    typedef unsigned long long ull_t;
    
    #define B10_MOD10(BITS) ((BITS) % 10)
    #define B10_DIV10(BITS) (((BITS) / 10) * 3)
    #define B10_BIT2DIG(BITS) ( B10_DIV10(BITS) + \
        ((B10_MOD10(BITS) == 0) ? (B10_DIV10(BITS) != 0) : \
        ((B10_MOD10(BITS)/3) + ((B10_MOD10(BITS)%3)!=0)) ))
    
    ui_t base10_bit2dig( ui_t bits ) {
        ui_t a = bits % 10;
        ui_t b = (bits / 10) * 3;
        return b + ((a == 0) ? (b != 0) : ((a/3) + ((a%3) != 0)));
    }
    
    int main() {
    static ui_t const cap = (sizeof(ll_t)*CHAR_BIT);
        ui_t i = 0u, d, e;
        ull_t v = 0u;
        while ( i < cap ) {
            d = base10_bit2dig(i);
            e = B10_BIT2DIG(i);
            printf( "BITS: %02u; DIGS: %02u; EDIG: %02u; VAL: %llu\n", i++, d, e, v );
            v <<= 1;
            v |= 1u;
        }
        return 0;
    }

  3. #3
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Sure, that's a fairly common way to do object-oriented C.

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735
    Thanks, currently doing this without a compiler (long overdue system cleanup in progress) so couldn't check. BTW what process APIs are there? I already know about tlhelp.h, psapi.h & the dirent.h/stdio.h combi for "/proc/" - basically I'm tired of there being no crossplatform API/framework for this process stuff so I thought to make one (GitHub - awsdert/paw: Process API Wrapper) and am trying to use the system API to forfull a simple API that I can use on any system (then I'll try to translate CheatEngine into C)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-23-2015, 01:38 AM
  2. struct pointers and functions
    By alfaceor in forum C Programming
    Replies: 8
    Last Post: 10-20-2009, 09:11 AM
  3. Struct passing to and from functions!
    By sebpinski in forum C Programming
    Replies: 11
    Last Post: 10-29-2008, 11:45 AM
  4. Exporting aliased functions
    By abachler in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2008, 10:06 AM
  5. visual studio 5: exporting functions in a dll
    By techi_talk in forum C Programming
    Replies: 1
    Last Post: 08-07-2006, 09:58 AM

Tags for this Thread