编组结构体数组指针的最佳方法

发布于 2024-08-17 23:46:28 字数 1298 浏览 5 评论 0原文

我正在从 C++ 调用函数,该函数返回一个指向结构数组的指针,但我遇到了问题,因为我是这个操作/实现的新手。

我的 C++ 代码:

// My C++ Structs
typedef struct _MainData {
 double  dCount;
 DataS1         *DS1;
 int  iCount1;
 DataS2         *DS2;
 int  iCount2;
}MainData;

typedef struct _DataS1 {

 unsigned int uiCount1; 
 unsigned int uiCount2; 
 int  iCount;
 void  *pA; 
 void  *pB; 

} DataS1;

typedef struct _DataS2 {
 unsigned int uiCount1; 
 unsigned int uiCount2;    
 unsigned int uiCount3;    
 unsigned int uiCount4;   
 double  dCount; 
 int  iCount1;     
 char  strLbl[64];
} DataS2;

// My C++ Function
MainData* GetData(const int ID)
{
        MainData* mData;
        int iLength = Get_Count();
        mData = new MainData[iLength];
        for(int x = 0;x < VarCounter; x++)
        {
            // Codes here assign data to mData[x]
        }
        return mData;
}

问题: 如何在 C# 中调用 C++ 函数 GetData?

我当前的 C# 代码是:

[DllImport(".\\sdata.dll")]
[return: MarshalAs(UnmanagedType.LPArray)]
private static unsafe extern MainData[] GetData(int ID);

// The struct MainData in my C# side is already "Marshalled"...

//My function call is here:
MainData[] SmpMapData = GetData(ID);

当我编译它时,出现异常: “无法封送‘返回值’:托管/非托管类型组合无效。”

抱歉,编码不好......请帮忙......

I'm calling functions from C++ that returns a pointer to an array of struct and I'm having problems since I'm new to this operation/implementation.

My C++ codes:

// My C++ Structs
typedef struct _MainData {
 double  dCount;
 DataS1         *DS1;
 int  iCount1;
 DataS2         *DS2;
 int  iCount2;
}MainData;

typedef struct _DataS1 {

 unsigned int uiCount1; 
 unsigned int uiCount2; 
 int  iCount;
 void  *pA; 
 void  *pB; 

} DataS1;

typedef struct _DataS2 {
 unsigned int uiCount1; 
 unsigned int uiCount2;    
 unsigned int uiCount3;    
 unsigned int uiCount4;   
 double  dCount; 
 int  iCount1;     
 char  strLbl[64];
} DataS2;

// My C++ Function
MainData* GetData(const int ID)
{
        MainData* mData;
        int iLength = Get_Count();
        mData = new MainData[iLength];
        for(int x = 0;x < VarCounter; x++)
        {
            // Codes here assign data to mData[x]
        }
        return mData;
}

Question:
How can I call the C++ function GetData to C#?

My current codes in C# are:

[DllImport(".\\sdata.dll")]
[return: MarshalAs(UnmanagedType.LPArray)]
private static unsafe extern MainData[] GetData(int ID);

// The struct MainData in my C# side is already "Marshalled"...

//My function call is here:
MainData[] SmpMapData = GetData(ID);

When I compiled it, there's an exception:
"Cannot marshal 'return value': Invalid managed/unmanaged type combination."

Sorry for the poor coding... Please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-08-24 23:46:28

我尝试做完全相同的事情,但由于时间不够而没有成功,但我在过程中学到了一些东西:

  1. 在 C# 中分配内存
  2. 要传递结构数组,结构必须是 blittable

祝你好运,我无法让它工作。

I tried to do exactly the same thing but didn't succeed due to lack of time but I learned something in process:

  1. Allocate memory in C#
  2. To pass array of structs, struct must be blittable.

Good luck with that, I couldn't make it to work.

不如归去 2024-08-24 23:46:28

一个问题是编组器不知道 C++ 代码返回的数组中有多少项。另一种方法可能是使用两个 C++ 方法 - 一个返回项目数,另一个返回给定索引的单个 MainData。

在 C# 方面,您的结构是什么样的?

由于您同时在 C++ 和 C# 端进行编码,因此使用 C++/CLI 而不是 PInvoke 来连接它们可能更容易。

One problem is that the marshaller doesn't know how many items are in the array returned by the C++ code. An alternative approach could be to have two C++ methods - one which returns the number of items, and one which returns a single MainData given an index.

What do your structures look like on the C# side?

Since you are coding both the C++ and C# side, it may be easier to use C++/CLI to interface them instead of PInvoke.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文