1

I have been coding in C# for many years. My experience in C++ is limited.

BACKGROUND: I have a C++ project in VS 2017 that is almost complete. My project employs the use of a Contour Shuttle device. To receive event notifications in my project from the device, I have to register for device event notifications by way of a windows style CALLBACK function. The SDK provided with the device has all the methods and definitions required to communicate with the device (I have already created a similar project in C# using the same SDK using multicast delegates, and it works excellent).

MY PROBLEM: I know that I have to use pointers to my methods to get the CALLBACK to work. I have been trying for 4 WEEKS to get my code to run. No success. The only success I have had is if I use a global static void. I can then register and receive events perfectly fine, BUT since it is a static global void I cannot access the methods and variables in my class.

I just do not understand how to do it properly. Here is my code:

Header File: (provided with SDK)

#ifndef _SHUTTLESDK_LIB_
#define _SHUTTLESDK_LIB_

#ifdef __cplusplus
#define SHUTTLESDK_API extern "C"
#else
#define SHUTTLESDK_API
#endif

typedef void (CALLBACK *SHUTTLEEVENTPROC)(DWORD event, UCHAR status, WORD type, WORD devno);

SHUTTLESDK_API int WINAPI Shuttle_Register_Callback_Global(SHUTTLEEVENTPROC shuttleproc, WORD type, WORD devno);

SHUTTLESDK_API int WINAPI Shuttle_Unregister_Callback(WORD type, WORD devno);

code file (only relevant code included):

...//

Shuttle_Plugin::void Start()
{
   if (Shuttle_Register_Callback_Global(*ShuttleCallback, 3, 1) != SHUTTLESDK_OK)
   {
       //Handle the error here...
       return;
   }
}

Shuttle_Plugin::void CALLBACK ShuttleCallback(DWORD event, UCHAR status, WORD type, WORD devno)
{
    //Events from callback should emit values here... 
}

//...

This will ONLY work if I declare the CALLBACK ShuttleCallback method in the global scope.

I understand that I am supposed to supply a pointer to my CALLBACK function, but I do not understand pointers that well yet.

Can someone please kindly show me the correct way to implement this in my project?

I have basically been thinking about / working on / testing code on this for 4 weeks, and I cannot explain to you the frustration I am having.

Foot Note: Yes, I have searched StackOverflow, Google, all the usual go-to forums. There are none that I can relate back to my specific case. Everyone uses ''FOO BAR notation'' and describes and debates the best way to do it, or the better way to do it, but I can't seem to find a simple, contextual example to help me out.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • Are you asking how to pass a class member as the callback? Or it is related to a `namespace`? – Manuel Jul 02 '20 at 11:43
  • `SHUTTLEEVENTPROC` is pointer to free function. A class member function pointer is a different incompatible type. You need to find a different way to achive what you want. – 463035818_is_not_a_number Jul 02 '20 at 11:50
  • [Pointers to member functions are very strange animals](https://devblogs.microsoft.com/oldnewthing/20040209-00/?p=40713). – IInspectable Jul 02 '20 at 11:53
  • @user13510154 What you are attempting is simply not possible. The SDK expects a C-style function pointer, not a C++ non-static class method. So, for the callback, you MUST use either 1) a free-standing function, 2) a `static` class method, or 3) a thunk. There is no other option. And since the SDK doesn't appear to let you pass user-defined data to the callback, to call the method with a `this` parameter correctly, you MUST use either 1) a global variable, 2) a thread-local variable (if the callback runs in the same thread that calls `Shuttle_Register_Callback_Global()`, or 3) a thunk. – Remy Lebeau Jul 02 '20 at 16:13
  • @RemyLebeau THANK YOU so much for this informative comment. Now I can relate what I am doing to get the function working. I never knew about thunk. I have been reading and using example code to practice with for most of the day and feel i am on the correct path now. One last question please... is thunk thread safe? – user13510154 Jul 03 '20 at 05:59

0 Answers0