Hi Folks,
I've been looking around all day and I've tried numerous suggestions, but to no avail. I've got a struct in C# defined as:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct GenericTrackRecord<T>
  {
    public Int32 hasValue;
    public T value;
  }
[StructLayout(LayoutKind.Sequential, Pack = 1)]
  public struct OTXFixDetailsStruct
  {
    public string CRS;
  }

  public partial class TrackFeeder : Form
  {
    internal const string DLL_LOC = @"C:/some_place/something.dll";

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    unsafe private struct OTXCoordinates
    {
      public double Altitude;
      public double Longitude;
      public double Latitude; 
      public string MGRS;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private unsafe struct OTXFixDetails
    {
      public string CRS;
      public string FixType;
      public GenericTrackRecord<Int32> ExternalFix;
      public GenericTrackRecord<double> HDOP;
      public string HorizontalAccuracy;
      public GenericTrackRecord<Int32> Motion;
      public GenericTrackRecord<Int32> SVS;
      public GenericTrackRecord<Int32> TTF;
      public string NumSats;
      public string SBAS;

    }
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct OTXEventDetails
    {
      public string EventDetails;
    }

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    private struct TrackRecord
    {
      public Int32 Index;
      public Int64 TimeStamp;
      public OTXCoordinates Coordinates;
      public OTXFixDetails FixDetails;
    }
...

During run-time I create a number of these structs and stick them in an array, which I want to pass to my C++ dll. In C# I have

[DllImport(DLL_LOC, EntryPoint = "request_advanced_search")]
private static extern void request_advanced_search(Int32 iDataSize, ref OTXTrackRecord[] recordBlock, AdvancedSearch searchCriteria);

(ignore the AdvancedSearch part, it works fine)

On the C++ side I've got

extern "C" {
#define FUNCTION __declspec(dllexport)
}

FUNCTION void __stdcall request_advanced_search(int iDataSize,TrackRecord *& recordBlock,AdvancedSearch searchCriteria)
{...}

When I walk through the code I find the data being sent from C# to C++ is as it should be, but when I look at the data on the C++ side I either get garbage or all 0's (or nulls).

In C# I've tried various techniques; using ref, out and [Out], and trying to pass an IntPtr, but nothing has worked. I can get a single record across since I've got another function that takes a reference to a single record and displays the contents, and that works fine. It's when I try to pass an array of these records that it blows up.

Thoughts?

Thanks in advance!

Recommended Answers

All 2 Replies

Just guessing if an OTXTrackRecord in C# has the same signature than a TrackRecord in C++

Hello lolafuertes,

I've made some changes since this post and I found the solution was to specify the length of the strings on the C# side and define char[] on the C++ side of the same length. I was swamped with other things otherwise I'd have mentioned *this* problem is solved... now, getting the data back to C# has been nothing short of a pain! So if anyone has any advice for that problem, I'm all eyes. I started another thread on this, located here:

http://www.daniweb.com/forums/thread344606.html

Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.