Callback to a Non static Member function in C++ need to be called from a DLL
-
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
-
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
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.
-
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
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 ...