加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

获取从本机dll到c#应用程序的结构数组

发布时间:2020-12-13 20:42:35 所属栏目:Windows 来源:网络整理
导读:我有一个C#.NET 2.0 CF项目,我需要在本机C DLL中调用一个方法.此本机方法返回TableEntry类型的数组.在调用本机方法时,我不知道数组有多大. 如何从原生DLL到C#项目获取表?以下是我现在所拥有的. // in C# .NET 2.0 CF project[StructLayout(LayoutKind.Seque
我有一个C#.NET 2.0 CF项目,我需要在本机C DLL中调用一个方法.此本机方法返回TableEntry类型的数组.在调用本机方法时,我不知道数组有多大.

如何从原生DLL到C#项目获取表?以下是我现在所拥有的.

// in C# .NET 2.0 CF project
[StructLayout(LayoutKind.Sequential)]
public struct TableEntry
{
    [MarshalAs(UnmanagedType.LPWStr)] public string description;
    public int item;
    public int another_item;
    public IntPtr some_data;
}

[DllImport("MyDll.dll",CallingConvention = CallingConvention.Winapi,CharSet = CharSet.Auto)]
public static extern bool GetTable(ref TableEntry[] table);

SomeFunction()
{
    TableEntry[] table = null;
    bool success = GetTable( ref table );
    // at this point,the table is empty
}


// In Native C++ DLL
std::vector< TABLE_ENTRY > global_dll_table;
extern "C" __declspec(dllexport) bool GetTable( TABLE_ENTRY* table )
{
    table = &global_dll_table.front();
    return true;
}

谢谢,
PaulH

当编组从原生到托管的未知大小的数组时,我发现最佳策略如下

>在托管代码中将数组键入IntPtr
>让本机代码返回数组和size参数.
>手动将IntPtr中的数据封送到托管端的自定义结构.

因此,我会对您的代码进行以下更改.

本机:

extern "C" __declspec(dllexport) bool GetTable( TABLE_ENTRY** table,__int32* pSize )
{
    *table = &global_dll_table.front();
    *pSize = static_cast<int32>(global_dll_table.size());
    return true;
}

管理:

[DllImport("MyDll.dll",CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool GetTable(out IntPtr arrayPtr,out int size);

public static List<TableEntry> GetTable() {
  var arrayValue = IntPtr.Zero;
  var size = 0;
  var list = new List<TableEntry>();

  if ( !GetTable(out arrayValue,out size)) {
    return list; 
  }

  var tableEntrySize = Marshal.SizeOf(typeof(TableEntry));
  for ( var i = 0; i < size; i++) {  
    var cur = (TableEntry)Marshal.PtrToStructure(arrayValue,typeof(TableEntry));
    list.Add(cur);
    arrayValue = new IntPtr(arrayValue.ToInt32() + tableEntrySize);
  }
  return list;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读