[RESOLVED] Desperately trying to make SimConnect with C ++ work in managed code

simskunkworks
Posts: 59
Joined: Sun Dec 15, 2013 6:30 pm

[RESOLVED] Desperately trying to make SimConnect with C ++ work in managed code

Postby simskunkworks » Thu Nov 22, 2018 8:53 am

(Using Visual Studio 2017)

I've had some success using the CLR and the Forms even though in the P3D SDK there is no mention of C ++ but only for C#.
I could connect with the SimConnect server:
private: System::Void MainForm_Load(System::Object^ sender, System::EventArgs^ e) {
try
{
Sim_Connect = gcnew SimConnect(safe_cast<String^>("tf104gconfig"), IntPtr::Zero, 0, nullptr, 0);
}
catch (Exception ^ex)
{
String^ message = "Could not connect with sim\napplication will close.";
String^ caption = "TF-104 G CONFIGURATOR";
MessageBoxButtons buttons = MessageBoxButtons::OK;
MessageBox::Show(this, message, caption, buttons);
Sim_Connect = nullptr;
Application::Exit();
}
}
i could also set a message queue:
protected: virtual void DefWndProc(Message% m) override {
printf("\nMessage:%d", m.Msg);
if (m.Msg == WM_USER_SIMCONNECT){ //0x0402
if (Sim_Connect != nullptr)
Sim_Connect->ReceiveMessage();
}
else
System::Windows::Forms::Form::DefWndProc(m);
}
Queue seems to work well entrapping all WM messages but nothing coming from the SimConnect server, so i'm stuck here.
P3D v4 SDK seems to be of little help since the translation from C# to C++ is not straightforward.
My goal is to make an external application (an aircraft configurator) able to talk with the sim via SimConnect.
Actually the only way i could have is an inelegant file cross talk.
Thanks in advance to anyone who can give me a cue or better an example code
Mario Motta

BenBaron
Posts: 72
Joined: Fri Jan 16, 2015 7:51 am

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby BenBaron » Thu Nov 22, 2018 11:28 am

Hi Mario,

not that I am an expert on the matter, but if you're trying to write a purely external program in connection with WinForms and SimConnect, why do you want to use C++/CLI, in the first place?

Why not sticking with C#, which is completely managed and where a SimConnect wrapper is available?

From my understanding of things, C++/CLI would only make sense trying if you'd wanted to make use of WinForms in conjunction with an in-process loaded .dll, as this wouldn't work with a purely managed assembly. But even then, I am not sure if there was a working approach to the combination P3D/C++/CLI.

Again, I am not an expert and maybe not understanding of what you're trying to accomplish, but from what you are writing I don't get why you'd want to bother with the potential hassle, if with C#, you have a perfectly working solution at hand.

All the best, Benny

User avatar
WarpD
Posts: 1442
Joined: Mon Feb 14, 2011 5:29 am

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby WarpD » Thu Nov 22, 2018 1:36 pm

There are a great many C++ samples for SimConnect: http://www.prepar3d.com/SDKv4/LearningCenter.php Under SDK, SimConnect API, Samples
Ed Wilson
Senior Developer
Mindstar Aviation

simskunkworks
Posts: 59
Joined: Sun Dec 15, 2013 6:30 pm

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby simskunkworks » Fri Nov 23, 2018 8:13 pm

Hi Mario,
not that I am an expert on the matter, but if you're trying to write a purely external program in connection with WinForms and SimConnect, why do you want to use C++/CLI, in the first place?
Why not sticking with C#, which is completely managed and where a SimConnect wrapper is available?
From my understanding of things, C++/CLI would only make sense trying if you'd wanted to make use of WinForms in conjunction with an in-process loaded .dll, as this wouldn't work with a purely managed assembly. But even then, I am not sure if there was a working approach to the combination P3D/C++/CLI.
Again, I am not an expert and maybe not understanding of what you're trying to accomplish, but from what you are writing I don't get why you'd want to bother with the potential hassle, if with C#, you have a perfectly working solution at hand.
All the best, Benny
Hi Ben,
thanks for reply, my goal is, as said, is to write in managed C++/CLI an external GUI application using WinForms (an aircraft configurator) able to talk with the sim via SimConnect.
The reason why i do not want use the C# is basically a practical reason, no will to learn it and no will to install the c# compiler, but also i'm looking for the answer to this question: "is possible write C++/CLI managed code using WinForms as a SimConnect client, or the only possible way is to use C# ?"
having such answer i can decide if either give up or succeed.
So far i could get some step forward:
- i can connect with SimConnect server
- i can override DefWnd proc where i can entrap all WM_ message except the WM_USER_SIMCONNECT (0x402) that is what i need.
- i can establish some callbacks like void OnRecvOpen(SimConnect^ sender, SIMCONNECT_RECV_OPEN ^ data) but since there is no SimConnect message passing trough DefWndProc is actually unuseful.
May be some LM developer may give the answer to above question.
/Mario

BenBaron
Posts: 72
Joined: Fri Jan 16, 2015 7:51 am

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby BenBaron » Sat Nov 24, 2018 7:46 am

Hi Mario,

I can see the point, where using WinForms and P3D in conjunction with C++/CLI in an internal scenario might come in usefull , so maybe I will do some experimenting next week and come back to you. Of course it would be nice to know, if this can be worked out.

Maybe, unil then, some of the devs can jump in on the issue.

Greets, Benny

simskunkworks
Posts: 59
Joined: Sun Dec 15, 2013 6:30 pm

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby simskunkworks » Sat Nov 24, 2018 10:55 am

Hi Mario,
I can see the point, where using WinForms and P3D in conjunction with C++/CLI in an internal scenario might come in usefull , so maybe I will do some experimenting next week and come back to you. Of course it would be nice to know, if this can be worked out.
Maybe, unil then, some of the devs can jump in on the issue.
Greets, Benny
Thanks a lot, meantime i have routed other tracks:
1. using this:
https://blogs.msdn.microsoft.com/abhina ... ogramming/
tried to mix managed and unmanaged code, it compiles but does not link with unmanaged SimConnect.Debug.lib
2. installing C++/CLR Windows Form for Visual Studio 2017
same as above
so i'm again stuck.
Meantime i have made half step forward succeeding to establish callbacks such as:

Code: Select all

// needed to "demangle" C++ // otherwise linker can't see references in // LockheedMartin::Prepar3D::SimConnect.dll using namespace LockheedMartin::Prepar3D::SimConnect extern "C" { void OnRecvOpen(SimConnect^ sender, SIMCONNECT_RECV_OPEN ^ data) { return; } void CallDispatch(SIMCONNECT_RECV ^ event, unsigned long arg2) { return; } };
..... somewhere on mainform scope

Code: Select all

// never called since no WM_USER_SIMCONNECT flows into DefWndProc Sim_Connect->OnRecvOpen += gcnew SimConnect::RecvOpenEventHandler(OnRecvOpen); /* not used, however it compiles and is called properly SignalProcDelegate^ ref = gcnew SignalProcDelegate(CallDispatch); Sim_Connect->ReceiveDispatch(ref); */
regards
/Mario

BenBaron
Posts: 72
Joined: Fri Jan 16, 2015 7:51 am

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby BenBaron » Mon Nov 26, 2018 3:02 pm

Hi Mario,

as I was curious, I just did a small quick and dirty test with a C++/CLI dll loaded by P3D and managed to get the SimConnect server sending messages to my DefWndProc. I don't know if those things really are the causing problems at your side, but here is what I made different:

1.
I didn't link to SimConnect.lib, as I didn't see the point in doing so, if you could just use the managed wrapper to SimConnect. Just referencing the LockheedMartin.Prepar3D.SimConnect.dll and doing

Code: Select all

using namespace LockheedMartin::Prepar3D::SimConnect;
in code was enough for me to get it to work. So no need for header and .lib there.

2.

Opening the connection, your were doing:

Code: Select all

Sim_Connect = gcnew SimConnect(safe_cast<String^>("tf104gconfig"), IntPtr::Zero, 0, nullptr, 0);
Like this, I also didn't get the SimConnect messages routed into the forms window procedure, but changing it to this:

Code: Select all

Sim_Connect = gcnew SimConnect(safe_cast<String^>("tf104gconfig"),this->Handle, 0, nullptr, 0);
,

it finally worked. At least my OnOnRecvOpen Event Handler now gets called correctly and I guess I could go on working normally from here, if I wanted. In hindsight, this does make sense as you have to supply the SimConnect constructor with a window handle as otherwise I guess the server doesn't know where the messages should be sent to.

Again, I didn't test with an external program, as this is not of much interest to me, but maybe my findings help you anyways.

Greets, Benny

simskunkworks
Posts: 59
Joined: Sun Dec 15, 2013 6:30 pm

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby simskunkworks » Mon Nov 26, 2018 6:49 pm

Thanks a lot Ben,
will give a try soon and report
/Mario

simskunkworks
Posts: 59
Joined: Sun Dec 15, 2013 6:30 pm

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby simskunkworks » Mon Nov 26, 2018 8:26 pm

Ben,
just tried your work around, no joy, still DefWnd procedure do not receive any WM_USER_SIMCONNECT message :-(

had previously tried something like before using:

Code: Select all

gcnew SimConnect(safe_cast<String^>("tf104gconfig"), Handle,WM_USER_CONNECT,nullptr,0);
instead this->Handle (anlogous), did not work either.
May be some projects options ?
May i have your code snippet ?
tia
/Mario

BenBaron
Posts: 72
Joined: Fri Jan 16, 2015 7:51 am

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby BenBaron » Tue Nov 27, 2018 1:47 pm

Mario,

mhh..thats weird..I now even tried and converted my project from .dll to .exe even then, running externally, I am perfectly receiving the OnRecvOpen and OnRecvClose calls sent from the SimConnect server.

To be honest, I don't know where to start looking on your side. I would offer to just send you my small test repository, so you can take a look for yourself and try figuring out, why it is not working. Basically, it is just a simple form with a connect button and two message boxes signalling the OnRecvOpen and OnRecvClose event handlers being called.

But it was created with Visual Studio 2017 community, so I don't know, if it was of any use for you?

Greets, Benny

simskunkworks
Posts: 59
Joined: Sun Dec 15, 2013 6:30 pm

Re: Desperately trying to make SimConnect with C ++ work in managed code

Postby simskunkworks » Tue Nov 27, 2018 2:33 pm

Ben,
i have to correct myself, i could have DefWndProc entrapping WM_USER_SIMCONNECT, it was my fault (a private member Handle with a name clash with SimConnect.Handle).
However OnRecvOpen() not called yet, guess a faulty signature, will continue to investigate.
However your code snippet will be very appreciated, i'm using 2017 Community either.
You can send it to info@simskunkworks.com
thanks
/Mario

simskunkworks
Posts: 59
Joined: Sun Dec 15, 2013 6:30 pm

Re: Desperately trying to make SimConnect with C ++ work in managed code (SOLVED)

Postby simskunkworks » Tue Nov 27, 2018 8:01 pm

Thanks to Ben, all is working now, i have nice managed SimConnect code on C++
/Mario


Return to “Software Development Kit (SDK) Questions”

Who is online

Users browsing this forum: No registered users and 5 guests