Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Callback to a Non static Member function in C++ need to be called from a DLL

Callback to a Non static Member function in C++ need to be called from a DLL

Scheduled Pinned Locked Moved C / C++ / MFC
c++algorithmsdata-structureshelp
3 Posts 3 Posters 0 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
    San 0
    wrote on last edited by
    #1

    Hi All, I need to implment a callback to a Nonstatic member function of a class. Which will be invoked through a DLL.DLL is an independent library.No dependency with the class. I Implemented with a static function. I need to know how it will be for a non static member function. My code is as follows for static implementation. I need to change it for a non static .Please help. class CCallbackTester { public: static int __stdcall CompareInts(const byte* velem1, const byte* velem2); }; int __stdcall CCallbackTester::CompareInts(const byte* velem1, const byte* velem2) { int elem1 = *(int*)velem1; int elem2 = *(int*)velem2; if(elem1 < elem2) return -1; if(elem1 > elem2) return 1; return 0; } int main(int argc, char* argv[]) { int i; int array[] = {22, 1, 3210, 2109, 1098}; Bubblesort((byte*)array, 5, sizeof(array[0]), &CCallbackTester::CompareInts); cout << "After the sorting\n"; for(i=0; i<5; i++) cout << array[i] << '\n'; getch(); } in DLL Bubblesort is as follows. void DLLDIR CALLBACK Bubblesort(byte* array, int size, int elem_size, CompareFunction cmpFunc) { for(int i=0; i<size; i++) { for(int j=0; j<size-1; j++) { if(1 == (*cmpFunc)(array+j*elem_size, array+(j+1)*elem_size)) { byte* temp = new byte[elem_size]; memcpy(temp, array+j*elem_size, elem_size); memcpy(array+j*elem_size, array+(j+1)*elem_size, elem_size); memcpy(array+(j+1)*elem_size, temp, elem_size); delete [] temp; } } } } Thanks San

    _ R 2 Replies Last reply
    0
    • S San 0

      Hi All, I need to implment a callback to a Nonstatic member function of a class. Which will be invoked through a DLL.DLL is an independent library.No dependency with the class. I Implemented with a static function. I need to know how it will be for a non static member function. My code is as follows for static implementation. I need to change it for a non static .Please help. class CCallbackTester { public: static int __stdcall CompareInts(const byte* velem1, const byte* velem2); }; int __stdcall CCallbackTester::CompareInts(const byte* velem1, const byte* velem2) { int elem1 = *(int*)velem1; int elem2 = *(int*)velem2; if(elem1 < elem2) return -1; if(elem1 > elem2) return 1; return 0; } int main(int argc, char* argv[]) { int i; int array[] = {22, 1, 3210, 2109, 1098}; Bubblesort((byte*)array, 5, sizeof(array[0]), &CCallbackTester::CompareInts); cout << "After the sorting\n"; for(i=0; i<5; i++) cout << array[i] << '\n'; getch(); } in DLL Bubblesort is as follows. void DLLDIR CALLBACK Bubblesort(byte* array, int size, int elem_size, CompareFunction cmpFunc) { for(int i=0; i<size; i++) { for(int j=0; j<size-1; j++) { if(1 == (*cmpFunc)(array+j*elem_size, array+(j+1)*elem_size)) { byte* temp = new byte[elem_size]; memcpy(temp, array+j*elem_size, elem_size); memcpy(array+j*elem_size, array+(j+1)*elem_size, elem_size); memcpy(array+(j+1)*elem_size, temp, elem_size); delete [] temp; } } } } Thanks San

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      A non-static member function is very much dependent on its class. So you cannot use it for callbacks.

      «_Superman_» I love work. It gives me something to do between weekends.

      1 Reply Last reply
      0
      • S San 0

        Hi All, I need to implment a callback to a Nonstatic member function of a class. Which will be invoked through a DLL.DLL is an independent library.No dependency with the class. I Implemented with a static function. I need to know how it will be for a non static member function. My code is as follows for static implementation. I need to change it for a non static .Please help. class CCallbackTester { public: static int __stdcall CompareInts(const byte* velem1, const byte* velem2); }; int __stdcall CCallbackTester::CompareInts(const byte* velem1, const byte* velem2) { int elem1 = *(int*)velem1; int elem2 = *(int*)velem2; if(elem1 < elem2) return -1; if(elem1 > elem2) return 1; return 0; } int main(int argc, char* argv[]) { int i; int array[] = {22, 1, 3210, 2109, 1098}; Bubblesort((byte*)array, 5, sizeof(array[0]), &CCallbackTester::CompareInts); cout << "After the sorting\n"; for(i=0; i<5; i++) cout << array[i] << '\n'; getch(); } in DLL Bubblesort is as follows. void DLLDIR CALLBACK Bubblesort(byte* array, int size, int elem_size, CompareFunction cmpFunc) { for(int i=0; i<size; i++) { for(int j=0; j<size-1; j++) { if(1 == (*cmpFunc)(array+j*elem_size, array+(j+1)*elem_size)) { byte* temp = new byte[elem_size]; memcpy(temp, array+j*elem_size, elem_size); memcpy(array+j*elem_size, array+(j+1)*elem_size, elem_size); memcpy(array+(j+1)*elem_size, temp, elem_size); delete [] temp; } } } } Thanks San

        R Offline
        R Offline
        Rick York
        wrote on last edited by
        #3

        One way is to do this is like many callback procedures do - they pass along a user specified argument as a void pointer (PVOID). You can pass a pointer to one of your objects as the argument and use a static member of the same class for the callback procedure. Then in the callback procedure cast the void pointer to a pointer to your object and call your member method. As an example, your callback procedure could look something like this :

        class YourClass
        {
        static int CompareCallback( ctchar * item1, ctchar * item2, void *ptr )
        {
        if( ! ptr )
        return 0;
        YourClass * pObject = (YourClass *)ptr;
        return pObject->Compare( item1, item2 );
        }
        };

        BTW : you can use this technique in a wide variety of callback scenarios like for thread procedures or window enumeration procedures or ...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups