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

Variable issues in managed code (c#)?

Messages
2
Country
unitedstates
I have a bit of managed code (c#) I'm working with to grab sim object data. It was working fine until I added a couple of more variables to my struct (it worked fine without the airline and flight number).

I'm pretty inexperienced when it comes to low level languages, so I'm having trouble seeing where I'm going wrong in this code. It's pretty apparent that I'm having issues with variable size or type due to some of the results I've been getting. I checked the variable formats against the documentation - the two I added are supposed to be string types. The marshal appears to be set properly to me - it matches my data types in the initDataRequest function.

Any ideas? Thanks.

my structure:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct Struct1
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public String atc_id;
public String atc_airline;
public String atc_fltnum;
public int squawk;
public double latitude;
public double longitude;
public double altitude;
public double groundv;
}


from the initDataRequest function:

simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "ATC ID", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "ATC Airline", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "ATC Flight Number", null, SIMCONNECT_DATATYPE.STRING256, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Transponder Code:1", "BCO16", SIMCONNECT_DATATYPE.INT32, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Latitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Longitude", "degrees", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Plane Altitude", "feet", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
simconnect.AddToDataDefinition(DEFINITIONS.Struct1, "Ground Velocity", "knots", SIMCONNECT_DATATYPE.FLOAT64, 0.0f, SimConnect.SIMCONNECT_UNUSED);
 
The string variables don't just "return". Read up on SimConnect_RetrieveString.
 
Now, Ed will probably tell me I'm wrong :D but I've found you need to MarshalAs on a per string basis
Code:
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)>
        Public adf_ident As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)>
        Public adf_name As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)>
        Public nav_ident As String
(etc..)

and then .ToString the result

Code:
(...)
   frmMain.dgData(1, i).Value = avionics.adf_ident.ToString
  frmMain.dgData(1, i).Value = avionics.adf_name.ToString
  frmMain.dgData(1, i).Value = avionics.nav_ident.ToString
(...)

In this case the struct was called 'avionics' and I am writing the result to a datagrid.

Yes Ed, it's vb.Net :rotfl:


[Edit] If you can stand vb.Net, this is my test code based on the original Prepar3D example:


I'm not guaranteeing it will make a lot of sense in places but it does grab the data you're after. It's very modularised, so it may take you ashort time to figure out what's going on.
 
Last edited:
Ah, thanks DragonflightDesign. I thought that the MarshalAs method would work for all strings in the structure. Didn't even think about that. Looking at the MarshalAs c# docs now, that very well could be it, and I'll report back when I get a chance to try it.

WarpD, I'm trying to follow the code, as it's a lot, especially without any real C++ experience. I'll keep reading. Thanks.
 
Ah... sorry... didn't see it was C#... never use the stuff. Dai is correct... you have to define the MarshalAs for each string. I would think that should solve your issue.
 
Back
Top