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

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

问题描述

我的应用程序包含带有非托管C dll调用的C#代码。
在我的C#代码中,我有一个对象/类,其属性都是系统类型,如字符串和int以及我定义的其他对象。



喜欢将这个复杂的(Graph.cs)对象传递给我的C(dll)代码,你会在这里提出什么实现?



我已经尝试移动结构,但是我没有这样做与其他任何字符串和int。



谢谢。



代码:

  public Class Grpah {

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

public Class TupleCollection {

IList< Tuple> _采集;

}

public Class Tuple {

Globals.TupleType m_ToppleType;
ArrayList m_Parameters;

}

public class TupleArgs {

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

}

public class RefCollection {

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

}


解决方案



如何:使用PInvoke的元组合结构



我认为最简单的方法是修改本机代码,使其能够使用CLR类型。



现在,您几乎肯定使用Visual Studio,希望它是VS2005或更高版本。这意味着虽然您现有的本地代码在C中,但您可以选择深入一些C ++。而且不仅如此 - 你还有C ++ / CLI。



所以我会制作一个新的C ++ / CLI DLL,并将C库链接到它,以便它可以调用C代码。然后在C ++ / CLI库中写一个薄的翻译层:它会暴露真正的CLR类(用 ref class 编写),并调用本地C代码。 >

例如在C头文件中:

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

在C ++ / CLI中:

  public ref class FooWrapper 
{
static void DoSomethingWithFoo(Foo ^ foo)
{
//分开Foo类并传递片段到C函数

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

在C#中:

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

...

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


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天全站免登陆