现在有一个C++的类,怎么在C#里去调用它?
做DLL还是怎么滴?
有些数据类型怎么对应,比如说C#里的string直接传过去,就能自动转化成C++的std::string吗?
有没有有过类似经验的大侠,给点经验之谈?

解决方案 »

  1.   

    通过C++/CLI做一次封装.
    C++/CLI可以实现MixCode,C++和C++/CLI的代码混用,C++/CLI的程序集可以直接被C#工程所引用string --> std::string无法直接转换
    不过用C++/CLI的话可以这样写:
    std::string cstring;
    String^ cliString = gcnew String(cstring.c_str());
      

  2.   

    差不多是这个样子的。
    要引用一个类库。
    using System.RunTime.Inter...[DllImport("xxxdll.dll")]
    private static extern int xxx(value:string);
      

  3.   


    具体一点呢,能给个例子吗?比如说://Car.h
    class Car{
       public:
          void Car(std::string name);
          void Run(int length);
       private:
          ...
    }class BMW : public Car{
       public:
       ...
       private:
       ...
    }//Car.cpp
    .........
    怎么编译成DLL并在C#里调用?
      

  4.   

    c++的做成com组件,提供c#使用。否则无法使用c++的类或接口。只能使用c导出函数。
      

  5.   

    如果说你有c++的源代码,建议你使用C++/CLI,如果只有dll那也就只能用PInvoke.
    现实的项目中,我们自己的要求是能避免做PInvoke就避免.使用C++/CLI效率比起PInvoke不是一倍两倍的关系,而是有本质性的提高.这是一张测试数据表:重复次数      C# 程序    C++程序 
    50,000      61        10 
    500,000     600       70 
    1,000,000   1162      140 
    5,000,000   6369      721 C++/CLI:第一流的CLI语言 
      

  6.   

    您可以参考这个帖子
    c#调用DLL
      

  7.   

    当然您必须得把C++类封装好生成一个DLL文件才可以被C#调用。调用的方式在楼上有,数据类型的对应是有平台处理的。
      

  8.   

    我最近也在搞这样一个类似的,我的方法是用c#调用dll,一开始会遇到数据类型封装不正确的问题,但后来发现那都是小问题,还遇到内存受限制的问题(因为我在64位机上开发),最后发现,都不是问题,耗上时间,多查资料,一切都容易解决。
    参考的资料主要来自msdn,google,百度,论坛等。
      

  9.   

    数据转换调用。net的marshal。。using System.Runtime.InteropServices;
    using namespace System;
    using namespace System::Runtime::InteropServices;public value struct Point
    {
    public:
        property int X;
        property int Y;
    };
    extern bool CloseHandle(IntPtr h);int main()
    {
        // Demonstrate the use of public static fields of the Marshal
        // class.
        Console::WriteLine(
            "SystemDefaultCharSize={0},SystemMaxDBCSCharSize={1}",
            Marshal::SystemDefaultCharSize,
            Marshal::SystemMaxDBCSCharSize);    // Demonstrate the use of the SizeOf method of the Marshal
        // class.
        Console::WriteLine("Number of bytes needed by a Point object: {0}",
            Marshal::SizeOf(Point::typeid));
        Point point;
        Console::WriteLine("Number of bytes needed by a Point object: {0}",
            Marshal::SizeOf(point));    // Demonstrate how to call GlobalAlloc and 
        // GlobalFree using the Marshal class.
        IntPtr hglobal = Marshal::AllocHGlobal(100);
        Marshal::FreeHGlobal(hglobal);    // Demonstrate how to use the Marshal class to get the Win32
        // error code when a Win32 method fails.
        bool isCloseHandleSuccess = CloseHandle(IntPtr(-1));
        if (!isCloseHandleSuccess)
        {
            Console::WriteLine(
                "CloseHandle call failed with an error code of: {0}",
                Marshal::GetLastWin32Error());
        }
    };// This is a platform invoke prototype. SetLastError is true,
    // which allows the GetLastWin32Error method of the Marshal class
    // to work correctly.    
    [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
    extern bool CloseHandle(IntPtr h);// This code produces the following output.
    // 
    // SystemDefaultCharSize=2, SystemMaxDBCSCharSize=1
    // Number of bytes needed by a Point object: 8
    // Number of bytes needed by a Point object: 8
    // CloseHandle call failed with an error code of: 6
      

  10.   

    可以做成DLL或者OCX等接口的形式(建议少用ocx),同是MS的东西,数据类型很好对应的
      

  11.   

    做com麻烦,你还是使用C++在包一层吧。
    在c++里写个简单的函数出口,只要几个参数就行的。
    然后在这个函数内部调用C++类,就行实例化,等操作。
    这样就可以避免C#调用C++的类。