• 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.

FSX Data exchange between gauges, C++

Messages
31
Hello there,

I'm trying to find a better solution on how to exchange data between FSX gauges. Simply exchanging L:vars is not sufficient. Say, there are the structures/classes in C++ and I need the other gauge to read (and sometimes write) these data.
I tried different ways, for ex., created a common structure containing pointers and tried with including it in the 'ImportTable', but it doesn't work in FSX by some reason, the sim stops calling a gauge callback.

Maybe anyone could suggest something?
Thanks in advance!
 
dynamic linking.

You can export functions from your dlls that can provide pointers to the classes and structures you wish to interact with.

First, it helps to condense a few things into a macro:


Code:
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif

Then, your exported function looks something like this:

Code:
EXPORT int __cdecl MyFunc(int arg1, int** arg2)

Adjust your parameters as needed

Now, in your other dll that needs to access this function, you first declare your function prototype like this:

Code:
typedef int (__cdecl *fMyFunc)(int, int**);

Notice it matches the parameters of your exported function.

Then you can define it where you wish to use it, along with the HMODULE object which you will get it from:

Code:
fMyFunc MyFunc = NULL;
HMODULE hMyDll = NULL;

To access it:
Code:
GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"MyDll.dll", &hMyDll);

if(hMyDll)
     MyFunc = (fMyFunc)GetProcAddress(hMyDll, "MyFunc");

Then, to call your function:

Code:
int x = 0;
int * pMyvar = NULL;

if(MyFunc)
    MyFunc(x, &pMyvar);
 
Thank you a lot, Naruto-kun!
I really forgot about that option with dll exports. I thought those DLLs would have different address space, causing things to get complicated.
 
Wow... total overkill of epic proportion!!!

Take a moment and learn about FS shared variables in C/C++ You can pass any type of variable as defined in the gauges.h file... including PVOID. None of that export overhead required at all.
 
@Choobe:

FSX gauges are in fact subelements of a single .dll; taking this into account any data type can be shared between them using default program structures. Unless you expressly want to define those gauges in different .dlls?

Tom
 
Last edited:
For passing data between gauge DLLs I suggest you look into the following:

register_var_by_name
unregister_var_by_name
initialize_var_by_name

Like I said... DLL export is serious overkill for the task.
 
dll exports are pain in the ass if you have many functions and complex API. The best way is to use virtual methods.

you define a class in DLL with just methods:

common header file (used in two DLLs)

class myVirtualClass
{
public:
virtual void method1() {};
virtual void method2() {};
};

now id DLL1, you should have:

class myDLL1Class: public myVirtualClass{
public:
virtual void method1() { // TO DO IMPLEMENTATION};
virtual void method2() { // TO DO IMPLEMENTATION};
} myDLLobject;

and you only need to export JUST ONE function that will return a pointer to myDLLobject:

EXPORT void* __cdecl getmyDLLClass1()
{
return &myDLLobject;
}

in the second DLL, you use common header with myVirtualClass defined

have one variable

myVirtualClass* DLL2object;
end get the pointer using exported function as Naruto specified:

GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, L"MyDll.dll", &hMyDll);

if(hMyDll)
MyFunc = (fMyFunc)GetProcAddress(hMyDll, "getmyDLLClass1");

and get the object from DLL1:
DLL2object = (myVirtualClass *) (MyFunc)();

and now you can call all methods from your DLL1 code, e.g.
DLL2object->method1();
DLL2object->method2();

please note, you should not have any member variable that you share, only virtual methods you can call ... This technique is used very often in DLL (or shared libs in lunux/mac) communications. You can find many articles on the subject. Excuse me for any mistakes I might put when writing this simple fast guide, but the concept is OK for complex API.

good luck

M.
 
Ed was replying to the OP as well, which was made clear to me by his reiteration of the same observation: "Like I said... DLL export is serious overkill for the task." ;)

Prior to the introduction of the "multi-gauge" concept, this was the only method to share data between the primitive single "gauges" of the time. It was fairly common practice to have one gauge that essentially served as a 'master' in that it created all of the shared variables, and handled all of the logic as well.
 
Many thanks to everyone!

Tom,
sorry for leaving it unclear. I meant the gauges are located in different DLL files.


WebSimConnect,
What's the advantages of this DLL exports over the variables sharing suggested by WarpD? Say, using the register_var_by_name()/initialize_var_by_name() one could simply share a pointer to a class instance, and then call its methods.
I understand your suggestion might be better for security concerns. Something else?


WarpD,
I wonder is there a difference on using this approach (register_var_by_name()/initialize_var_by_name()) and accessing the L:vars (set_named_variable_typed_value()/register_named_variable() etc) ?
Say, from the performance point of view. Once I heard when using the L:vars the gauges get laggy if the vars amount increases too much. Not sure if that's true.
Besides, are there any limitations?
 
I have yet to find any limitations on accessing variables across DLLs using the built in methods. They are not processed by FS as far as I can tell, simply added to a 'list' for other gauges to locate and connect to if they know what they're looking for. A L:vars are processed by FS.
 
WebSimConnect,
What's the advantages of this DLL exports over the variables sharing suggested by WarpD? Say, using the register_var_by_name()/initialize_var_by_name() one could simply share a pointer to a class instance, and then call its methods.
I understand your suggestion might be better for security concerns. Something else?

I am not sure whether you can share pointers to structures/classes using FSX variables across different DLLs. I seriously doubt it due to ODR vialation, though never tried it ;) It could work if you use same compiler and your projects are configured identically, however CRT dependency on lcient machine could harm the solution. The safe way (not security, just feasibility, use of compiler, crt version, etc.) is to use vtable as in the example I have shown. If you want to share standard data types, then FSX variables are enough and best choice ...

you will find nice article about the techinique here:
http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL

regards

M.
 
Well....You have to keep in mind what a pointer is. An address. You can type cast it into a unsigned integer if you like, send that value through an L var, and then read that L var, typecast from a double to a unsigned int, which you can then type cast again into your pointer. I prefer to have as few such jumps as possible though.
 
While I would not recommend sharing a class in this manner... sharing data and pointers to structs are fine.

I personally can't imagine why I would want to share a class across a set of gauge DLLs to be honest.
 
indeed, if it is just data structure (not a class), then you can cast the pointer, however the data has to be declared using data_seg pragma ... for classes, only vtable or COM ....

I personally can't imagine why I would want to share a class across a set of gauge DLLs to be honest.

well, to simplify API if your DLLs needs sohisticated communication. Imagine, you write a gauge DLL to be used by others. Then common data might not be sufficient to control it and you would like to have kind of API for the gauge.
 
No the data doesn't have to be declared that way at all.

Since a gauge DLL is for providing a gauge, providing access to any possible class from inside a gauge DLL doesn't make much sense. This isn't a place to be creating complex addons that do more than provide avionics. That's where performance tanks.
 
The best way to "share" variables between gauges is...not to share them in the first place.

It's way better, cleaner and less bug-prone, to have an airplane gauge made of a single .DLL and many sub-gauges, so you can use standard C/C++ visibility rules to share data in the most performing and secure way.

The only time you might need some kind of communication method, is if you want to create an utility module that might be shared with other developers, using some kind of API, in that case there are plenty of different ways to do it, depending which language/s you want to interface with and if you want to share data only locally or perhaps you wanted to be potentially compatible with a network.

But if you are doing all the gauges as part of a single project, I suggest not have them in separate DLLs, but use the multi-gauge structure.
 
I agree though there are times when to stick everything in one C++ project gets a bit messy. I don't usually have more than 3 dlls on my projects. 1 config manager, 1 pit lighting module, and the other one holds all the systems and such.
 
The only time I split something out is when it's a 'generic' type piece of avionics that would be useful in a variety of aircraft... like Mindstar's 430/530 units. They are complete stand-alone units in that they work without needing any other gauges. However, they also provide variables to be used by other gauges such as a HSI or a CDI system.
 
Back
Top