简体   繁体   中英

Marshal structure with integer references to C#

Hi I'm trying to create and marshal the following structure from C# into C++ and maintain the linked reference. I'm unsure how this structure should be defined in C#? In C++ the structure must look like below, with the const reference maintained.

// C++
struct {
        int   a;       // my value
  const int&  b = a;   // my reference to a
}

Does anyone know if this is possible?

Thanks.

Edit:

This is more representative of what I'm trying to accomplish, and as pointed out by @Hans it is not legal C++, but maybe someone can suggest a better path? The system_t is generated in either C++ or C# and passed to C++. My best guess: (if this is even a good design pattern) is to initialize all the references in the system_t constructor in C++. As far as marshaling from C#, it will get complicated.

struct system_t
{
  float    sysSampleRate = 112500.0f;            // Sample rate from receivers.
                                                 // Illegal statement @Hans

  struct tvg_t  // tvg_t is passed to tvg processor 
  {
          float    tvgLinearGain;
    const float&   tvgSampleRate = sysSampleRate;   // Get the rate from system.
                                                    // Illegal statement @Hans
  } tvg;   // Nested tvg_t in system_t.

  //... Many other structures and variables ..//
};

I'd like to find the right design pattern rather than abandoning this and going to a flat structure or passing system_t to every module.

This should work:

[StructLayout(LayoutKind.Sequential)]
public struct MyCStruct {
  public int a;
  public IntPtr b;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM