在 WinCE 中编组从 c++ 到 c# 的结构数组

Marshalling array of structures from c++ to c# in WinCE

本文关键字:结构 数组 c++ WinCE      更新时间:2023-10-16

我正在尝试在Windows CE程序和紧凑框架2.0上将以下结构从c ++封送到c#。我在弦的编组方面遇到了很多困难。

我有这个 c++ 代码:

#define Console_Parameters_MAX 50
struct AllParameters {
  Parameter Parameters[Console_Parameters_MAX];
} ;
struct Parameter { 
  int Index;
  int Id; 
  char Value[20];
};
extern "C" {
   BOOL GetAllConsoleParameters(AllParameters *pItem);
}

这是相应的 C# 代码:

[StructLayout(LayoutKind.Sequential)]
public struct AllParameters {
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
  public Parameter[] Parameters
}
[StructLayout(LayoutKind.Sequential)]
public struct Parameter {
  public int Index;
  public int Id;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
  public byte[] Value;
}
[DllImport("exemple.dll", SetLastError = true)]
public static extern bool GetAllConsoleParameters([MarshalAs(UnmanagedType.Struct)] ref AllParameters pItem);

这就是我调用它的方式:

AllParameters item = new AllParameters();
if (AppAPI.GetAllConsoleParameters(ref item)) {
   var array = item.Parameters;
}

当我调用 GetAllConsoleParameters 时,我得到异常 NotSupportedException。我尝试了许多配置,但没有成功。

谁能就如何实现它提供建议?

提前致谢

这在Windows桌面上对我有用。您可能必须在 C DLL 和 C# DllImport 属性中将调用约定更改为 Cdecl,因为我在这里读到 Cdecl 是 Windows CE 的标准:https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingConvention(v=vs.110).aspx

C 代码:

extern "C" {
  __declspec(dllexport) BOOL __stdcall GetAllConsoleParameters(AllParameters *pItem)
  {
    pItem->Parameters[0].Index = 0;
    pItem->Parameters[0].Id = 42;
    CopyMemory(&pItem->Parameters[0].Value[0], "Hello World", 12);
    pItem->Parameters[1].Index = 1;
    pItem->Parameters[1].Id = 43;
    CopyMemory(&pItem->Parameters[1].Value[0], "Hello World 43", 15);
    return TRUE;
  }
}

C# 代码:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct Parameter
{
    int Index;
    int Id;
    //char Value[20];
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
    string Value;
};
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct AllParameters
{
    //Parameter Parameters[Console_Parameters_MAX];
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
    Parameter[] Parameters;
};
class Program
{
    [DllImport("MarshalC.dll", CallingConvention = CallingConvention.StdCall)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetAllConsoleParameters(ref AllParameters pItem);
    static void Main(string[] args)
    {
        var size = Marshal.SizeOf<AllParameters>();
        AllParameters all = new AllParameters();
        bool result = GetAllConsoleParameters(ref all);
    }
}

我会这样做

        [StructLayout(LayoutKind.Sequential)]
        public struct AllParameters {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
            public Parameter[] Parameters;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct Parameter {
          public int Index;
          public int Id;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
          public byte[] Value;
        }
        [DllImport("exemple.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
        public static extern bool GetAllConsoleParameters(ref IntPtr pItem);

        static void Main(string[] args)
        {
            AllParameters allParameters = new AllParameters();
            allParameters.Parameters = new Parameter[50];
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(allParameters));
            int z = Marshal.SizeOf(allParameters);
            if (GetAllConsoleParameters(ref ptr))
            {
                Marshal.PtrToStructure(ptr, allParameters);
                Parameter[] parameters = allParameters.Parameters;
            }
        }

按照我的解决方案,C ++代码:

/* - not used
#define Console_Parameters_MAX 50
struct AllParameters {
  Parameter Parameters[Console_Parameters_MAX];
} ;
*/
struct Parameter { 
  int Index;
  int Id; 
  char Value[20];
};
extern "C" {
   BOOL GetAllConsoleParameters(Parameter pItem[], int size);
}

和相应的 C# 代码:

/* - not used
[StructLayout(LayoutKind.Sequential)]
public struct AllParameters {
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
  public Parameter[] Parameters
}
*/
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct Parameter {
  public int Index;
  public int Id;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
  public byte[] Value;
}
[DllImport("exemple.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool GetAllConsoleParameters([MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1), Out] ConsoleParameter[] myStruct, int size);

和代码调用:

ConsoleParameter[] item = new ConsoleParameter[50];
if (AppAPI.GetAllConsoleParameters(item, 50)) {
   var array = item;
}

非常感谢您的帮助