例如很多年前我用C++ 6.0做的一个类 
class sampleclass
{
   int a,b,c;
    public int  fun1();
{
   return  a+b-c;}
  public int  fun2();
{
   return  a*b-c;}...}
已经把他包装成一个dll (C++ 6.0)现在在C#的环境中 我想把这个类倒进来 ,在代码中 也能 写出 像 sampleclass aa=new sampleclass(); 进行类似aa.fun1(); aa.fun2();的操作
我想问在C#中怎么实现这样的效果啊。 对在c++中的dll文件?编译有什么要求?

解决方案 »

  1.   

    参考:http://blog.csdn.net/gisfarmer/archive/2009/02/18/3904527.aspx
      

  2.   

    //以调用fun1 为例
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    namespace ConsoleApplication3
    {
        class Program
        {        [DllImport("dll路径", EntryPoint = "fun1", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
            public static extern int fun1(string str);        static void Main(string[] args)
            {
                try
                {          
                    int result = fun1();
                    Console.WriteLine(result);
                    Console.Read();            }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.Read();
                }
            }    }
    }测试下,看可以吗,有什么问题再问我。
      

  3.   

    [DllImport("yourdll")]   
      

  4.   

    如果是普通dll,类对象几乎调用不了,否则,com也不会是基于接口了
      

  5.   

    如果使用MFC扩展extern类。就没法在C#中调用了。
      

  6.   

    to 3 楼 我测试了一下 编译可以
    但是运行时报 找不到dll中的 fun1 入口?
    dll中到底应该怎么写 这个类呢?