P3D v4 Desperately trying to make SimConnect with C ++ work in managed code

#1
(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:
Code:
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:
Code:
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 in C++ (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
 
Last edited:
#2
You should take the time to read through the SimConnect code examples, understanding what steps are taken to work with SimConnect and why each step is needed. Just having a SimConnect object doesn't mean you'll get any messages from SimConnect.
 
#3
Thanks for answer,
what i did following Client_Event.cs example from SDK 4.3 before looking for help, however at least some msg like SIMCONNECT_RECV_ID_OPEN or SIMCONNECT_RECV_ID_QUIT should be received also without registering, or i missing something ?
Do you have some working code example ?
thanks in advance
/Mario
 
#5
Thanks for reply,
not yet, since i interpreted those steps as registering callbacks to events.
Since connection with the sim seems to be established correctly (the try/catch work as expected with the sim running or not) guessed that at least a WM_USER_SIMCONNECT should be received in window queue, which does not happen.
Moreover i have some difficulties to translate C# code, ie:
Code:
// listen to connect and quit msgs
simconnect.OnRecvOpen += new SimConnect.RecvOpenEventHandler(simconnect_OnRecvOpen);
simconnect.OnRecvQuit += new SimConnect.RecvQuitEventHandler(simconnect_OnRecvQuit);
into C++ managed code.
A code snippet as example will be very appreciate,
tia
/Mario
 
#6
Since you're not telling SimConnect to actually do anything (send event, return data, event, etc)... it's not going to do anything. The C++ examples are still your best guide, though I suspect using manged code in C++ was not the best choice for using SimConnect.
 
#7
Since you're not telling SimConnect to actually do anything (send event, return data, event, etc)... it's not going to do anything. The C++ examples are still your best guide, though I suspect using manged code in C++ was not the best choice for using SimConnect.
I have done several non-managed SimConnect clients so far, what you say does not match with what i know, ie:
Code:
// Connect to the SimConnect's Server
HRESULT hr = SimConnect_Open(&hSimConnect, "TF104G", NULL, 0, 0, 0);
....
// Give to the SimConnect's Server our SimConnect's callback pointer
SimConnect_CallDispatch(hSimConnect,SimSkunkWorksCB, NULL);
is what is needed to receive several events such as:
Code:
SIMCONNECT_RECV_ID_OPEN
SIMCONNECT_RECV_ID_EXCEPTION
SIMCONNECT_RECV_ID_EVENT_FRAME,
SIMCONNECT_RECV_ID_QUIT
etc..
i'm quite sure that constructing a SimConnect object gives also a SimConnect server connection and as the SDK says overriding DefWndProc makes the explicit SimConnect_CallDispatch() in managed code unnecessary.
Moreover i have done an half-step forward guessing the syntax to establish an event callback:
Code:
//
Sim_Connect->OnRecvOpen += gcnew SimConnect::RecvOpenEventHandler(OnRecvOpen);
...
// in mainform.h scope
using namespace LockheedMartin::Prepar3D::SimConnect;
extern "C" {   
void OnRecvOpen(SimConnect^ sender, SIMCONNECT_RECV_OPEN ^ data) {  return;  }   
};
it compiles and links correctly, however OnRecvOpen(), as i expected, has been never called since no WM_USER_SIMCONNECT msg pass trough DefWndProc()
In understand you do not have any C++ code snippet to show me, so thanks anyway, hope there is someone other that can help me to get my result or definitely shows me that is impossible.
sincerely
/Mario
btw:
i suspect C++ is not the best choice for SimConnect managed code either, otherwise i would not ask for help here, however i love challenges
 
#8
Been watching this thread, and I now need to take the bait. :stirthepo
What are you trying to do? Is this a console app or GUI. I assume GUI with MainForm_Load example code and trying to emulate a Windows default proc.
What does C++/CLI (or rename C++/CX or now C++/WinRT) provide you that C# does not?, or straight C++? Performance? why not C++ DirectX 12 and Win2D
Are you attacking this from converting C++ examples or C# examples? Assume from C# as your examples emulate C# Managed Client Event sample.

Your right it is a challenge.

Maybe I will try this out and see what's what.
 
#9
In understand you do not have any C++ code snippet to show me, so thanks anyway, hope there is someone other that can help me to get my result or definitely shows me that is impossible.
Are you being intentionally obtuse? The same SDK web pages provided by Lockheed-Martin has 31 C++ examples to draw from. How much more do you truly need? If you seek C++ managed... you are definitely on your own. Why do I need to provide a C++ code snippet that shows exactly what the L-M SDK SimConnect C++ examples do?
 
#10
Thanks for reply,
My goal is to make an external GUI application in C++ (an aircraft configurator) able to talk with the sim via SimConnect.
Actually the only way i could have is an inelegant file cross talk.
Yes actually i use client_event.cs as a start point, no language preferences, my route to C++ is because i know it and no time or will to install C# compiler and learn a new language, no intention to attack anybody. (if i have understood well, my english is far to be good)
Actually i could:
- get a SimConnect connection
- override DefWndProc where WM_MESSAGES flow trough nicely
- establish an event callback, OnRecvOpen(), that should entrap SIMCONNECT_RECV_ID_OPEN, however differently from C# example no WM_USER_SIMCONNECT msg pass trough overridden DefWndProc so OnRecvOpen() won't be ever called.
Next step will try to get SimConnect::ReceiveDispatch() working to see if i can trap SimConnect messages flow.
tia
/Mario
 
Last edited:
#11
Are you being intentionally obtuse? The same SDK web pages provided by Lockheed-Martin has 31 C++ examples to draw from. How much more do you truly need? If you seek C++ managed... you are definitely on your own. Why do I need to provide a C++ code snippet that shows exactly what the L-M SDK SimConnect C++ examples do?
You are intentionally rude and prove you do not know what you're talking about and you did not understand anything from the beginning, there is not a single C ++ example of SimConnect managed code, that would not compile using C++/CLI and .NET framework, the only examples of managed code are on C#.
Better if you had said right away that you were not able, I asked you three times if you had an example of SimConnect managed code in C ++, you never showed it to me for the simple reason that you are not able to.
Hope there are some moderator here, unfair and rude comments should not be allowed.
 
#12
no intention to attack anybody. (if i have understood well, my english is far to be good)
Sorry - my statement
Are you attacking this from converting C++ examples or C# examples?
means - How are you trying to solve your problem, by converting C++ examples or C# - not attacking people.

Have you set the platform option in the project to ver 14 (ie. C++ 2015) as P3D is compiled in 2015, also set to x64? Maybe that is why there is no P3D simconnect communication. I will have to work on it see if I can get it to at least do the
 

n4gix

Resource contributor
#13
There must be a reason why L-M did not provide any examples of "C++ Managed" code. I suspect as did Ed that this is because it is not really suited for this purpose. There are over 30 examples of pure C++ code provided however.
 

ddawson

Resource contributor
#14
There must be a reason why L-M did not provide any examples of "C++ Managed" code. I suspect as did Ed that this is because it is not really suited for this purpose. There are over 30 examples of pure C++ code provided however.
My thoughts would be to write a native C++ dll and call it from the managed app.
 
#15
Sorry - my statement means - How are you trying to solve your problem, by converting C++ examples or C# - not attacking people.

Have you set the platform option in the project to ver 14 (ie. C++ 2015) as P3D is compiled in 2015, also set to x64? Maybe that is why there is no P3D simconnect communication. I will have to work on it see if I can get it to at least do the
thanks for reply, I'm working on ver 141 (2017) and x64, with /clr option
I'm trying using c# as a rough guide, discovered, as i expected, SimConnect::RecvOpenEventHandler() and others accepts function pointers as a pure C call, that's why i have embedded it in extern "C" { };
/Mario
 
Last edited:
#16
There must be a reason why L-M did not provide any examples of "C++ Managed" code. I suspect as did Ed that this is because it is not really suited for this purpose. There are over 30 examples of pure C++ code provided however.
Thanks for reply,
that's what i'm looking for to decide to either give up or succeed; 30 example of non-managed C++ code are unuseful.
/Mario
 
#17
My thoughts would be to write a native C++ dll and call it from the managed app.
Thanks Doug,
that's could be a feasible route, all in all there are few functions to export, remain to check if it could be loaded into a managed code.
/Mario
btw
don't know if you received my answer about arrestor, i had some failure on my SMTP, however thanks a lot, will test soon, also interested in x64 version.
 
#20
I was able to get FSW to recompile x64, simconnect.lib and simconnect.h from P3D, however I cannot debug it or run it. There is a simconnectDebug.lib for P3D. Just copy over the simconnect.lib and .h from P3D. But I am also using VS2015 platform v140. and .Net 4.52 - think P3D 4.3 needs 4.6 Anyways - plugging along.

Ya know I saw FSWSlew before and even downloaded it ....... I hate getting old.:banghead:
 
Top