【问题标题】:How to pass *& and **& parameter to C++ dll from C# code如何从 C# 代码将 *& 和 **& 参数传递给 C++ dll
【发布时间】:2021-07-18 11:58:11
【问题描述】:

我有下面的 C++ dll

函数原型和结构定义

extern "C" int  __declspec(dllexport) DoOPeration(DataStruct *&, DataStructInfo **&);

typedef struct
{
        int   count;
        float CountData;
        bool  flg;
        BYTE* data;
        char   info[100];
}DataStruct;

typedef struct
{
        int   count;
        bool  flg;
        BYTE* data; 
}DataStructInfo;


DoOPeration函数的实现如下

int __declspec(dllexport) ImageAnalyze(DataStruct *&all, DataStructInfo **&part)
{
    int ret = 0;
    try
    {
          if(all->count == 0)
            return(ERR_PARAMERROR);

          for(cnt = 0 ; cnt < all->count  ; cnt++)
          {
             if(part[cnt]->data== NULL)
                return(ERR_NO_IMAGE);
          } 
    }
    catch(int err)
    {
        return(err);
    }
    catch(...)
    {
        return(-2);
    }
    return(ret);
}

与上面对应,我添加了 C# 端代码,如下所示

DLL 导入、结构(类)定义

[DllImport(@"C:\TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int DoOperation(ref DataStruct all, ref DataStructInfo[] part);

[StructLayout(LayoutKind.Sequential)]
public class DataStruct
{
        int   count = 0;
        float CountData = 0;
        bool  flg = 0;
        byte[] data = null;
        string   info = 0;
}

[StructLayout(LayoutKind.Sequential)]
public class DataStructInfo 
{
        int   count = 0;
        bool  flg = 0;
        byte[]  data;   
}

// DoOperation function calling

DataStruct alls = new DataStruct(); // set all required parameter
DataStructInfo[] part= new DataStructInfo[3]; // set all required parameter
int ret = DoOperation(ref alls, ref part);

从c#代码调用C++ dll函数后,我观察到了c++ dll中函数参数的值。

  1. 对于参数 [alls] 值正确传递
  2. 对于参数 [part] 值未正确传递(显示一些垃圾值)

请让我知道有关从 c# 代码传递第二个参数即 (**&) 的信息。

如果我犯了任何其他错误,请告诉我,以便我更正。

注意:我无法更改 C++ dll 代码

【问题讨论】:

  • 您必须在此处进行一些手动编组,因为 C++ 的设计不正确。如果可以,第一步是更改 C++。如果你不能,那么你需要了解&amp; 的含义。
  • c#中new DataStructInfo[3]只为三个结构分配内存,不做"new DataStructInfo()"三次
  • 您最好的选择可能是在您的工具链上自己创建一个(虚拟)dll 来观察您的编译器的作用。有时 & 被实现为跨 dll 行的额外 * 。基本上你需要让你的身边有正确的指针深度。第一步是弄清楚如何改变它(在显式 * 中工作)。其次是尝试一些有根据的猜测(绝对不会使用比符号更多的层)。

标签: c# c++ dllimport dllexport


【解决方案1】:

我通过以下方法得到了解决方案。

C# 端代码

DLL 导入、结构定义

[DllImport(@"C:\TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int DoOperation(ref IntPtr all, ref IntPtr[] part);

[StructLayout(LayoutKind.Sequential)]
public struct DataStruct
{
    public int   count { get; set; }
    public float CountData { get; set; }
    public bool  flg { get; set; }
    public IntPtr data { get; set; }
    public string   info { get; set; }
}

[StructLayout(LayoutKind.Sequential)]
public struct DataStructInfo 
{
    public int   count { get; set; }
    public bool  flg { get; set; }
    public IntPtr  data { get; set; }
}

// First parameter
DataStruct alls = new DataStruct(); // set all required parameter
IntPtr intPtrsAll = new IntPtr();
intPtrsAll = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(DataStruct)));
            Marshal.StructureToPtr<DataStruct>(alls, intPtrsAll, false);

// Second parameter
DataStructInfo[] part= new DataStructInfo[3]; // set all required parameter
IntPtr[] intPtrsParts= new IntPtr[part.Length];

for (int i = 0; i < intPtrsParts.Length; i++)
{
    intPtrsParts[i] = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(DataStructInfo)));
    Marshal.StructureToPtr<DataStructInfo>(part[i], intPtrsParts[i], false);
}

int ret = DoOperation(ref intPtrsAll, ref intPtrsParts);


注意:为了在 C# 端设置 byte[] 数据,我使用了 [IntPtr] 和以下方法

byte[] rowData = GetRawBytes(dataHandler); // function to read byte[] data
int size = Marshal.SizeOf(rowData[0]) * rowData.Length;
data = Marshal.AllocHGlobal(size); // IntPtr property of struct's
Marshal.Copy(rowData, 0, data, rowData.Length);

【讨论】:

    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多