C#和C之间传递对象 [英] Passing objects between C# and C

查看:240
本文介绍了C#和C之间传递对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是由C#code与非托管的C DLL调用。
在我的C#code我有一个对象/类,其中它的属性都是系统类型,如字符串,int和其他对象我已经定义了。

我想这个复杂的(Graph.cs)对象传递给我的C(DLL)code,执行什么你会建议在这里?

我曾尝试移动结构,但我不与任何其他然后串和INT这样做。

感谢。

code:

 公共类Grpah {    TupleCollection m_TupleCollection;
    INT m_nGeneralIndex;
    布尔m_bPrintWRF;
    串m_sLink;
}公共类TupleCollection {    IList的<元组GT; _采集;}公共类组{    Globals.TupleType m_ToppleType;
    ArrayList的m_Parameters;}公共类TupleArgs {    公共字符串值{获得;组; }
    公共Globals.PAS PAS;
    公共RefCollection RefCollection {搞定;组; }
    公众诠释时间{搞定;组; }}公共类RefCollection {    公开名单< INT>同义词集合{搞定;组; }
    公共Globals.PAS PAS;}


解决方案

尝试:

如何:使用的PInvoke元帅结构

我觉得你取得进步的最简单的方法是修改本机code,给它以CLR类型的工作能力。

现在,你几乎可以肯定使用Visual Studio,并希望这是VS2005或更高版本。这意味着,尽管现有的原生code为C,你必须深入到一个小C ++的选项。不仅如此 - 你也有C ++ / CLI

所以我会用一个新的C ++ / CLI DLL,和你的C库链接到它,这样它可以调用到C code。然后写在C ++ / CLI库薄薄的转换层:它会暴露真实的CLR类(以引用类写),并调用到本机C code。

例如。在C头:

 无效do_something_with_foo_data(INT A,INT B,INT C);

在C ++ / CLI:

 公开引用类FooWrapper
{
    静态无效DoSomethingWithFoo(美孚^富)
    {
        //挑分开Foo类并传递给C函数的作品        do_something_with_foo_data(foo->一种,foo-> B,foo-> C);
    }
};

在C#中:

 公共类Foo
{
    公众诠释A {搞定;组; }
    公众诠释B {搞定;组; }
    公众诠释13C {搞定;组; }
}...无功富=新的Foo {A = 1,B = 2,C = 3};
FooWrapper.DoSomethingWithFoo(富);

My application consist of C# code with unmanaged C dll calls. In my C# code I have an object/class where its properties are both system types such as string and int and other objects I have defined.

I would like to pass this complex (Graph.cs) object to my C (dll) code, what implementation would you suggest here?

I have tried moving structs but I fail to do so with anything other then string and int.

Thanks.

Code:

public Class Grpah {

    TupleCollection m_TupleCollection;
    int m_nGeneralIndex;       
    bool m_bPrintWRF;
    string m_sLink;  
}

public Class TupleCollection {

    IList<Tuple> _collection;

}     

public Class Tuple {

    Globals.TupleType m_ToppleType;        
    ArrayList m_Parameters;

}

public class TupleArgs {

    public string Value { get; set; }       
    public Globals.PAS PAS;
    public RefCollection RefCollection { get; set; }
    public int Time{ get; set; }      

}

public class RefCollection {

    public List<int> SynSet{ get; set; } 
    public Globals.PAS PAS;

}

解决方案

Try:

How to: Marshal Structures Using PInvoke

I think the easiest way for you to make progress is to modify the native code, giving it the ability to work with CLR types.

Now, you're almost certainly using Visual Studio, and hopefully it's VS2005 or later. This means that although your existing native code is in C, you have the option to delve into a little C++. And not only that - you also have C++/CLI.

So I would make a new C++/CLI DLL, and link your C library to it, so that it can call into the C code. Then write a thin translation layer in the C++/CLI library: it will expose true CLR classes (written with ref class) and will call onto the native C code.

e.g. in a C header:

void do_something_with_foo_data(int a, int b, int c);

In C++/CLI:

public ref class FooWrapper
{
    static void DoSomethingWithFoo(Foo ^foo)
    {
        // pick apart the Foo class and pass the pieces on to the C function

        do_something_with_foo_data(foo->A, foo->B, foo->C);
    }
};

In C#:

public class Foo
{
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
}

...

var foo = new Foo { A = 1, B = 2, C = 3 };
FooWrapper.DoSomethingWithFoo(foo);

这篇关于C#和C之间传递对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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