Sign in to follow this  
rhumbaflappy

Delphi and SimConnect

Recommended Posts

Hi all.I have a conversion of the SimConnect.h file that seems to work with Delphi.I've used it with Delphi7 and the new win32 Turbo Delphi Explorer ( FREE ):http://www.turboexplorer.com/mirror(Note: you cannot have both Turbo Delphi Explorer and Turbo C++ Explorer installed at the same time... use one or the other.)A test file that includes all the needed support files here:http://webpages.charter.net/ludowr/DelphiOpenClose.zipIncluded in the zip is a MANIFEST file, a RES file and an RC file, along with SimConnect_4.pas ( the H file conversion ).The above four files must be in the project's base folder so the compiler can find them. OpenClose.dpr is the actual program. The code is pretty simple.The compiled OpenClose.exe is a native win32 file that will find the SimConnect.dll on your computer... so it is truly "stand-alone". No further playing around with the manifest file, etc... :) It needs no runtime libraries, no .NET, and can be placed anywhere on your computer.What OpenClose.exe does is simply open a connection to FSX via SimConnect, and then close it. It's a command-line program, so run it from a command window ( CMD.exe ).SimConnect_4.pas is my 4th attempt to get this thing working... I'm sure it has errors, but we have a start. If anyone else can improve it, feel free to post it here ( or at Arno's website ) for testing! Hopefully within a month or two, we'll have a solid version. So, we now have SimConnect working in the Delphi environment... lots of fun for RAD Windows form developers, and programmers that like Delphi.( Jacky! Please test this with Delphi 6... it should work, and hopefully will get you started. )Dick

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

Hi Jacky.This example ( SimConnect_4.pas ) has only a few functions implemented! There are actually 68 functions in the DLL to call. So we have 65 more to go... and the syntax may be wrong in what I have.Make a list of what you need for calls, and we'll see if we can add them."What should I give as fEpsilon and DatumID ?"The SDK indicates 0 would work.Dick

Share this post


Link to post
Share on other sites

Hi Jacky.YES!!! Anything you can do to improve the use of SimConnect using Delphi is most welcome.Truthfully, I've become much more comfortable with C#. But it seems a shame that Delphi would be left in the cold. And this thread could be a good place to exchange improvements and additions to the eventual ( and final ) SimConnect.pas file. :)Dick

Share this post


Link to post
Share on other sites

Dick:Thank you so much for providing us Delphi programmers a way into SimConnect. I downloaded openclose5 and had a look at it. I sort of understand it. I haven't got my copy of FSX yet, so that may clear things up more when I look at that. In the mean time, is there any chance of you updating the openclose program in line with Brian G's tutorials? That would help my understanding along heaps!Thanks againScott

Share this post


Link to post
Share on other sites

Hi Scott.Jacky ( JAB ) has pretty much taken over the SimConnect.pas development. He is improving the "include" file, and is experimenting with it. My involvement is mostly from curiosity as to if it would work... and it appears it does. By NO means am I a good Delphi programmer.I actually program with C# ( when I'm forced into it ). It just seemed a shame that Delphi was left out of the SimConnect development loop, as several programers prefer it.Dick

Share this post


Link to post
Share on other sites

Hello,Yes, I am working on Dick's SimConnect.pas (actually version 6).At the moment, my test program is able to Open / Close, Load Flight / SaveFlight, Get one aircraft information ... I have problem to makeSimConnect accept a second "AddToDataDefinitions" (???) but I am surethat I will get the trick ... Do not hesitate to contact me if you are interested ...A+Jacky

Share this post


Link to post
Share on other sites

Jacky:Good on ya for taking your time to provide a Delphi interface to FSX. I have done some Delphi programming (mostly database stuff), but not so much windows API stuff. However If you need some help please don't hesitate to ask for assistance.

Share this post


Link to post
Share on other sites

Hi Scott,Some progress : I can now get several data from SimConnect in one shot using SimConnect_GetNextDispatch.I would like now the get data using a callback (defined by SimConnect_CallDispatch.Here is the actual declare : Function SimConnect_CallDispatch( hSimConnect: THandle; pfcnDispatch: TDispatchProc; pContext: Pointer ) : LongInt; StdCall;which is the Delphi translation for HRESULT SimConnect_CallDispatch( HANDLE hSimConnect, DispatchProc pfcnDispatch, void * pContext );but I am unable to buid this "TDispatchProc".Here is how it looks in C: void CALLBACK DispatchProc( SIMCONNECT_RECV* pData, DWORD cbData void * pContext );All the test I have made result in a crash as soon as this DispatchProc is called ... Can you help me ? Send me a nail if you want the Delphi sources.A+Jacky

Share this post


Link to post
Share on other sites

All,Having got a copy of the latest version of SimConnect.pas from Jacky, and made some changes, I've now got it working both using polled transfers (looping on SimConnect_CallDispatch) and using Message driven events.I used the "Tagged Data" example from the SDK and have a version of this running in a Delphi Windows application.I plan to convert more for the demos soon, hopefully post the code on a convenient website.Ken

Share this post


Link to post
Share on other sites

Hi Dick,I just go it working without loops !!!Preview : In the MainForm declaration : > Procedure SimConnectEvent (Var Msg:TMessage); Message WM_User;SimConnect opening :> If SimConnect_Open(hSimConnect,'Test',Handle,WM_USER,0,0)<>S_Ok Then ...The procedure :> Procedure TfMain.SimConnectEvent;> Begin> ...> Here I call SimConnect_GetNextDispatch and process it > ...> End;A+Jacky

Share this post


Link to post
Share on other sites

I've uploaded a zip of where I am so far (I'm now using this to write the interface program I really needed to talk to a real avionics display).This version allows you to either use the busy-wait polling method or the windows event approach (I kept the busy wait to show what the SDK example does - of course a real app will want to use the windows message).I plan to convert more examples.Feedback welcome!Ken.http://forums.avsim.net/user_files/165010.zip

Share this post


Link to post
Share on other sites

Ken:Had a look at your files (briefely). Some thoughts are as followsFirstly, thank you for sharing your work with the rest of us. I for one need all the ideas (help) I can get!Secondly, you probably haven't got that far yet but some of the data types are wrong. the following are classed as follows // SIMCONNECT_DATAType_WAYPOINT SIMCONNECT_DATA_WAYPOINT = record Latitude: double; Longitude: double; Altitude: double; Flags: LongInt; ktsSpeed: double; percentThrottle: double; end; // SIMCONNECT_DATA_LATLONALT SIMCONNECT_DATA_LATLONALT = record Latitude: double; Longitude: double; Altitude: double; end; // SIMCONNECT_DATA_XYZ SIMCONNECT_DATA_XYZ = record x: double; y: double; z: double; end;in the SIMCONNECT.h they are classed as float which is a 4 byte value whereas Doubles are 8 bytes. (Update,I'm wrong here, they ARE doubles in the original Oooooooops!)Thirdly, just a particular worry of mine. Passing enumerated types in C++ works differently than in Delphi. In C++ it is not really clear from the MSDN doco how they are passed, but in Delphi"NoteEnumerated constants without a specific value have RTTI:" from the help file. I'm worried that sending uninitialized enumerated types in Delphi may have side effects.Finally: I tried nesting the records as you have done:SIMCONNECT_RECV_EXCEPTION = record Header: TSimConnectRecv; dwException: DWORD; dwSendID: DWORD; dwIndex: DWORD; end;But I think I will return my records to copied fields as the nesting is going to cause further dereferencing in the programming. I've always agreed with the guy (name escapes me) who design the SR71 "Simplicate and add lightness".Hope this helps. BTW I also will post my version when it's fit for human consumption.

Share this post


Link to post
Share on other sites

Hi All:As promised, here's my version of what Dick has kindly provided. The callback works and also I have it responding to windows messages sent by simconnect. It has been tested with Delphi 7As always, your comments and suggestions (+ve and -ve) are welcome.http://forums.avsim.net/user_files/165044.zipBTW: Dick, how did you create SimConnect.res?

Share this post


Link to post
Share on other sites

Scott,I think I got most of the double/single, DWORD/LongInt issues sorted out (though I was left with one "unsigned long" where I'm still puzzled as to why it is declared as that)I didn't like the header construct much, but I don't like repeating the fields explictly everywhere either (the C version embeds then without having to get all the declarations the same). I'm still thinking about how I want to do this.It seemed to me that the "header" portion was only likely to be refernced before you established which type of record you had, and once you (re-)mapped the correct type you would be unlikely to access it again. In that case it would not matter that these (essentially spaceholders) were in a subrecord.You'll note, as pointed out by Jacky in an e-mail, that some of the "header" elements have the wrong type - I'd missed that some are derived from the "event" variant.I'm not sure which of the enumerated types you are referring to - the parameters are defined as DWORD, and I pass Ord(Enum) - I did this to keep the example as close to the SDK version as I could.In practice for real software I'd probably just pass a named constant. (Though the enumerated type trick does enure that the values in any group are unique).Hopefully we can eventually get one version of "SimConnect.Pas" that we can all agree on.My short term aim was to get FS X driving a real aircraft Digital Map/Situation Awareness display ready for a demonstration and I got that far today (having only received FSX on Wednesday). Getting the initial version from Jacky gave me a good start.Netx week I want to try and convert more of the Examples (in my "spare" time" to verify more of the headers (and provide a starting point for others).I'm not sure my current version is "fit for human consumption", but it seemed better to get it out soon and encourage feedback/cooperation.Ken

Share this post


Link to post
Share on other sites

Ken:Sorry, I didn't mean to imply that YOUR version wasn't "fit for human consumption" only mine. I tend to be a very messy programmer. As for the enums passed, again sorry 'bout the implication, I didn't actually see that in your code but was passing it along as (**Siren sounds***) Warning to all and sundry as the original C++ header uses enums extensively, and I thought I could save somebody some grief somewhere along the line.I agree with your sentiments about dereferencing the header elements once only, but I still think it's better to have the repeating fields for code readability (just an opinion). I tried to emulate what the C code does by declaring the structures as classes, but came a gutser on that as the dwSize field got swallowed somewhere by Delphi. Jacky has a nice demo program that illustrates what happens.

Share this post


Link to post
Share on other sites

Hi Ken.Yes, you're on the right track, I believe. A single version of SimConnect.pas would be great. Between you, Scott and Jacky, I think that could be done. Experimentation is going to be the key, as some elements of SimConnect may not be exactly what the SDK proclaims. If you read some of the SimConnect C++ and FSUIPC posts in various forums, you'll see what I mean. Pete Dawson ( FSUIPC )is the best source for info here. Even though he doesn't deal with Delphi ( or C++ ), he knows the Simconnect structure better than anyone outside the Aces team.Dick

Share this post


Link to post
Share on other sites

I didn't take your reply to say there was a problem with mine - I was saying that I think there is!Quite a few things still need to be sorted out, and I hope that getting more of the examples to work will highlight that.The problem will be keeping track of what doesn't actaully work as advertised in the C version, so as to avoid tryiong to fix it in the Delphi version.Ken

Share this post


Link to post
Share on other sites

Ken & Jacky:If Ken hasn't already accomplished testing the examples, I propose:(a) Using Jacky's header as the "standard" as Jacky has a very clean and readable programming style(:( We divide the examples between the three of us to test and report back on any problems and proposed fixes/workarounds.Comments? Suggestions?Dick, I specifically did not include you in this, as you expressed a preference in an earlier posting, not to be involved further with Delphi. On a personal note, I cannot thank you enough for getting this thing started as I for one would not have known how to do what you did regards the manifest file.

Share this post


Link to post
Share on other sites

I've noted that it is possible to use a variant record to cover all these related types (only two fields need to be renamed to prevent duplicate names).This means that the received paramater can be of this (Pointer) type and then no re-casting or extra indirection is required.The downside is that the nested "case"s within the variant mean you'd have to look at the orignal non-variant equivalent to work out which fields are valid for each type!I've added this type to my version, but I'm not certain that I'll use it.Ken

Share this post


Link to post
Share on other sites

Scott,I'm reluctant to do this, since there are many types in Jacky's version (at least the last one I've seen) which need to be corrected, which I have already done in my copy (of Jacky's version).Also I've added Delhi-style T.. and P.. equivalents which I prefer (and Jacky doesn't)I always format all of my files using DelForExp, which has a "Borland" setting that enforces the Borland submission standard code layout. I have long since given up debating on whether one coding style standard is better or worse than another (I used to be Chief Software Engineer for an Aerospace company, and it always amazed me how many "religious wars" this topic could kick off!)I do find things much more readable when everything conforms to the same standard though. Personally, I find the style of the original SimConnect.pas less readable, simply because it is different.From what Jacky has said (in e-mail) I think he is too busy with his own schedule to do work on the examples (and converting them probably doesn't help what he is trying to achieve).I intend to work on those examples that directly help my project first (Tagged Data was most critical for me) before hopefully moving on to the remainder as time permits.The examples that are next on my list (in no particular order) are:AI Objects and WaypointsAI TrafficClient EventRequest DataSet DataTracking ErrorsHTH,KenP.S. Did you get an explanation of how the manifest file came to be?

Share this post


Link to post
Share on other sites

Jacky,Firstly, no need to apologise for your English - it is fine!1.1) Fine - probably rules out the (cumbersome) variant structure method I experimented with.1.2) Having both is what I intend/want too.1.3) I don't feel strongly about the "header" either way. The variant record that I've created meets your objective (and mine of not repeating the same declaration) but is pretty unreadable due to the number of nested cases required, needs two field name changes and conflicts with 1.1 above. Pity there is no "clean" solution for supporting inheritance in records. I have yet to find an example where I need to refer to a subfield of the header, so it makes no difference to my code either way. In my latest version, I've done a global replace of the "header" with a commented block declaration, that cross-refers back to where the declaration comes from for clarity.2) I never write in C or C++, and avoid them whenever I can - but this has resulted in me translating a lot of source code to Delphi so that I can develop it further. I'm therefore comfortable with doing more of the examples.I'll post the latest version, with another couple of converted examples soon.Ken(Who can't type for toffee, and always forgets to check for mis-types until after hitting Submit...)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this