• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

Questions about pointer to pData in MyDispatchProc1

Messages
10
Country
china
I've read in p3d sdk documentation that pData is a buffer and struct SIMCONNECT_RECV_OPEN is inherited from SIMCONNECT_RECV

C++:
void CALLBACK MyDispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void* pContext)
{
  switch(pData->dwID)
  {
    case SIMCONNECT_RECV_ID_OPEN:
      // enter code to handle SimConnect version information received in a SIMCONNECT_RECV_OPEN structure.
      SIMCONNECT_RECV_OPEN *openData = (SIMCONNECT_RECV_OPEN*) pData;
      break;

But why in line 7, the pointer type can be SIMCONNECT_RECV_OPEN? What's exactly the detail of pData?
Since I'm fresh to C++, forgive me for asking some basic questions.
 
Messages
26
Country
netherlands
Hi!
Yes, this is indeed the effect of "is inherited from". If you search for the SIMCONNECT_RECV_OPEN struct in the Learning Center (aka the Windows Help file of the SDK), then you'll see it is declared as:
struct SIMCONNECT_RECV_OPEN : public SIMCONNECT_RECV

Effectively that means that the struct starts with all the contents of SIMCONNECT_RECV before continuing with what you see in this declaration. So, if you have a pointer-to-SIMCONNECT_RECV_OPEN, you can always safely cast it to a pointer-to-SIMCONNECT_RECV. This holds for all possible extensions. So, to do the reverse cast, as is happening here, you need to know which version it is. That is why it first checks the dwID field to see which variant it is. In "real" modern C++, you would use dynamic_cast<SIMCONNECT_RECV_OPEN *>(pwData) if the message was properly "new"ed and has a type-code, or static_cast<SIMCONNECT_RECV_OPEN *>(pwData) if you want to tell the compiler you just know it is correct. The cast used here is (very) old-school C-type casting.

The pointer pData is declared as "SIMCONNECT_RECV* pData", and you can find the SIMCONNECT_RECV struct in the SDK documentation:

Code:
struct SIMCONNECT_RECV {
    DWORD  dwSize;
    DWORD  dwVersion;
    DWORD  dwID;
};
 
Messages
10
Country
china
Hi!
Yes, this is indeed the effect of "is inherited from". If you search for the SIMCONNECT_RECV_OPEN struct in the Learning Center (aka the Windows Help file of the SDK), then you'll see it is declared as:
struct SIMCONNECT_RECV_OPEN : public SIMCONNECT_RECV

Effectively that means that the struct starts with all the contents of SIMCONNECT_RECV before continuing with what you see in this declaration. So, if you have a pointer-to-SIMCONNECT_RECV_OPEN, you can always safely cast it to a pointer-to-SIMCONNECT_RECV. This holds for all possible extensions. So, to do the reverse cast, as is happening here, you need to know which version it is. That is why it first checks the dwID field to see which variant it is. In "real" modern C++, you would use dynamic_cast<SIMCONNECT_RECV_OPEN *>(pwData) if the message was properly "new"ed and has a type-code, or static_cast<SIMCONNECT_RECV_OPEN *>(pwData) if you want to tell the compiler you just know it is correct. The cast used here is (very) old-school C-type casting.

The pointer pData is declared as "SIMCONNECT_RECV* pData", and you can find the SIMCONNECT_RECV struct in the SDK documentation:

Code:
struct SIMCONNECT_RECV {
    DWORD  dwSize;
    DWORD  dwVersion;
    DWORD  dwID;
};
nice explanation! Thanks a lot!
I think I've got the point but to ensure my understanding is correct, I will repeat my understanding and could you please point out if I make any mistake?

Since SIMCONNECT_RECV_OPEN is inherited from SIMCONNECT_RECV, it actually contains more elements inside. And initially, the data buffer pData points to actually contains everything that SIMCONNECT_RECV_OPEN has, but since it's type is SIMCONNECT_RECV, I can't access members like uEventID which is unique to SIMCONNECT_RECV_OPEN.
And when I have casted the pointer, uEventID becomes available.
 
Messages
2,063
Country
us-ohio
SIMCONNECT_RECV_OPEN inherits from SIMCONNECT_RECV... not "is inherited from". Think of SIMCONNECT_RECV_OPEN (and all of the other structs defined as descendants of SIMCONNECT_RECV) as a genetic relative that inherits all that SIMCONNECT_RECV has to offer and brings it's own unique additions. The purpose of this approach is to allow the API to define a single struct type that it passes back a pointer to... so there's exactly one function declaration that supports all possible variations and only requires the programmer to typecast as appropriate to support them. The ID value in the SIMCONNECT_RECV allows the programmer to know exactly which type of struct the pointer actually points to and thus be able to make the proper typecast to access the true struct.
 
Messages
26
Country
netherlands
nice explanation! Thanks a lot!
I think I've got the point but to ensure my understanding is correct, I will repeat my understanding and could you please point out if I make any mistake?

Since SIMCONNECT_RECV_OPEN is inherited from SIMCONNECT_RECV, it actually contains more elements inside. And initially, the data buffer pData points to actually contains everything that SIMCONNECT_RECV_OPEN has, but since it's type is SIMCONNECT_RECV, I can't access members like uEventID which is unique to SIMCONNECT_RECV_OPEN.
And when I have casted the pointer, uEventID becomes available.
I won't go into the "how to correctly say this in English". Yes, you seem to understand correctly. As long as the pointer in in its generic form, only the common fields can be accessed. Once casted to the more specific form the compiler will accept you referring to the other fields. 👍
 
Top