如何将struct的结构从C#DLL传递给C ++? [英] How passing struct of struct from C# DLL to C++ ?

查看:74
本文介绍了如何将struct的结构从C#DLL传递给C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public struct person
{
    string name;
    string age;
    string job;
}

public struct management
{
   Person employee;
   int count_of_employee;
}

public interface ICallmyClass

{
   void Test();
   management[] GetData
   {
     get;
   }
}

public class myclass : ICallmyClass
{
  management[] st;
  int Count = 0;
  
  public management[] GetData
  { 
     get 
     {  
       return st;
     }
  } 

  public void Test()
  {
     st = new management[3];
     
       st[0].person.name =  "Jone1";
       st[0].person.age = "21";
       st[0].person.job = "engineer1";
       st[0].count_of_employee = ++Count;
    
       st[1].person.name =  "Jone2";
       st[1].person.age = "22";
       st[1].person.job = "engineer2";
       st[1].count_of_employee = ++Count;

       st[2].person.name =  "Jone3";
       st[2].person.age = "23";
       st[2].person.job = "engineer3";
       st[2].count_of_employee = ++Count;

  }
}




int main(int argc, char* argv[])
{
   ConInitialize(NULL);
   ICallmyClass *csi = NULL;

   HRESULT hr = CoCreateInstance(CLSID_MYCLASS, NULL, CLSCTX_INPROC_SERVER, IID_ICALLMYCLASS,reinterpret_cast





您好,我的程序中有一些...



我开发了一个关于在C ++环境中调用C#库的简单程序。



所以,我在visual studio中制作了ac#dll,并包含在c ++项目中。我试图从c#dll返回关于字符串,整数的参数。



但我想尝试从c#dll获取一个管理结构数组。所以我在c ++项目中编译,调用SafeArrayAccessData函数时出错。 br />


错误信息是这样的。this-> getData((LPSAFEARRAY *)& pRetVal);以前的格式,是错误的类型库< br $> b $ b



我的代码中的程序是什么?...



剂量任何人都知道这个问题吗?



我尝试了什么:



我试图在谷歌搜索。但它仍然没有任何解决方案..



我在codeproject.com找到了类似的答案

http:// www。 codeproject.com/Questions/134062/C-dll-C-client-pass-array-parameter



http://www.codeproject.com/Articles/16206 / Call-C-code-from-C-and-read-an-array-of-struct-whi



http://www.codeproject.com/文章/ 637320 /修改阵列结构 - 默认 - marshalin



http://www.codeproject.com/Answers/134347/C-dll -C-client-pass-array-parameter#answer2



但它仍然有同样的错误。



Hello, I've a some on my program..

I develop the simple program about calling C# library at C++ environment.

So, i made a c# dll in visual studio, and included at c++ project. and i tried to returned parameter from c# dll about string, integer.

But i want to try to get a "managment" array of struct from c# dll. So I Compiled in c++ project, it was error when call the "SafeArrayAccessData" function.

The error message was like this. "this->getData((LPSAFEARRAY*) & pRetVal); Either a previous format, is the wrong type library "


What is the program in my code??...

dose any one know about this problem?

What I have tried:

I tried to search in google. but it still haven't any solutions..

and i found the similar answers in codeproject.com
http://www.codeproject.com/Questions/134062/C-dll-C-client-pass-array-parameter

http://www.codeproject.com/Articles/16206/Call-C-code-from-C-and-read-an-array-of-struct-whi

http://www.codeproject.com/Articles/637320/Modifying-an-array-of-structures-default-marshalin

http://www.codeproject.com/Answers/134347/C-dll-C-client-pass-array-parameter#answer2

but it still have a same error.

推荐答案

编组复杂数据始终是一个问题,因此我将在一些简单合理的步骤中拆分此任务。



1.初始化新传输(设置计数为0或计数)

2.使用简单数据类型传输每个数据集(比如结构或字符串)

3.结束并完成转移



这是一些示例项目如何完成。
Marshalling complex data is always a problem, so I would split this task in some simple and reasonable steps.

1. init a new transfer (set count to 0 or the the count)
2. transfer every data set with simple data type (like struct or strings)
3. end and finish the transfer

Here is some sample project how it can be done.


这篇关于如何将struct的结构从C#DLL传递给C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆