I was trying to write a C++ program, including a header file, and a cpp file, plus, I am using the SimConnect dll.
Let's say I have a class called MyClass, which contains some functions.
Inside one of my functions, I call this SimConnect function:
SIMCONNECTAPI SimConnect_CallDispatch(HANDLE hSimConnect, DispatchProc pfcnDispatch, void * pContext);
Also, I wrote this function:
void __stdcall MyClass::myDispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
As far as I understood, I should send this function as the second parameter to the SimConnect function SimConnect_CallDispatch I talked about before.
I read an example which came with the SimConnect SDK which is doing exactly what I am doing, while I get an error, and they don't. The only thing that's different is that they wrote their whole code in one page, while I tried to split the code into header + body.
When I try to do as I said above, and write
SimConnect_CallDispatch(hSimConnect, myDispatchProc, NULL);
I receive this error (compiler is having trouble in myDispatchProc part):
argument of type "void (__stdcall TransmitData::*)(SIMCONNECT_RECV *pData, DWORD cbData, void *pContext)" is incompatible with parameter of type "DispatchProc"
So clearly, it's a casting-like error. As I said, this code would've worked if I didn't split my program into two separate files. Why doesn't it work now?
Although this is not a recent question, I'll leave a reference in case anyone else stumbles upon this.
The SimConnect_CallDispatch function requires a reference to a static, callback function with a specific interface, i.e. (SIMCONNECT_RECV* pData, DWORD cbData, void* pContext)
The nature of the static function is that it won't have access to the variables of the instance. So to get around this problem, all of the examples out there don't bother with the complexity of .h and .cpp files and just put everything in one file.
Let's say I have a class "MyClass" with separate .h and .cpp files. This is how I solved this issue,
Define the default/standard static callback function for SimConnect in in the .h file, e.g.
void static CALLBACK MyDispatchProcRD(SIMCONNECT_RECV *pData, DWORD cbData, void *pContext);
To handle the processing of the callback in the actual instance and not through the static function, define a custom instance function in the .h file e.g.
void DispatchProc(SIMCONNECT_RECV* pData, DWORD cbData);
Normally this is where you'd implement all of your SimConnect callback handling, but in this case we are just going to cast the context reference to your class and pass it on to our custom instance function for further processing, e.g.
void CALLBACK MyClass::MyDispatchProcRD(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext) {
MyClass* procThis = reinterpret_cast<MyClass*>(pContext);
procThis->DispatchProc(pData, cbData);
}
This is where you're going to implement all of your SimConnect handling. As this is now an instance function, you have access to the variables of the instance.
void MyClass::DispatchProc(SIMCONNECT_RECV* pData, DWORD cbData) {
switch (pData->dwID) {
case SIMCONNECT_RECV_ID_SIMOBJECT_DATA: {
SIMCONNECT_RECV_SIMOBJECT_DATA* pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA*)pData;
...
Just remember to pass the reference to your instance, i.e. this, when your are calling SimConnect dispatch, i.e.
SimConnect_CallDispatch(hSimConnect, MyDispatchProcRD, this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With