现在有一个DLL,用VC++写的。需要MFC类库支持。这个类不是COM组件,无法直接引用。我如何从C#调用C++的Dll中类中的方法?我现在有个头文件。但是这个头文件只能由VC++打开。
class __declspec(dllexport) KernelProcess
{
  KernelProcess();
  virtual ~KernelProcess;
  void Init();
  int Method_1(....);
  int Method_2(....);
}应该是接口声明

解决方案 »

  1.   

    参考
    http://www.cnblogs.com/Chase/archive/2010/05/31/1748596.html
      

  2.   

    MFC类库现在的Windows都自带的吧,跟MFC无关吧
    你的DLL有没有导出函数呢?
    用Depends.exe查看一下呢,若没有导出,没办法吧……
      

  3.   

    如果没有导出函数,就自己用C++包装一下这个实现吧,C++肯定是能调用这个DLL的,然后由你包装的DLL提供导出函数,让C#来调用吧;
      

  4.   


    根据这个类库的作者所说,这个DLL需要MFC支持。我也尝试过了。如果创建一个VC++6.0的WIN32工程(没有MFC支持)编译会出现错误。尝试Depends察看过了。但是似乎没有任何可用信息。
      

  5.   

    那种dll叫做"扩展dll"(http://msdn.microsoft.com/en-us/library/h5f7ck28(v=vs.80).aspx)。它本身有很多的限制。因为要调用它,需要知道类的具体内存布局,而不同的编译器可以有不同的具体实现(虚函数表的实现,多重继承的实现等等)。同样,C#也不能知道它的具体布局。建议是
    1、用C++写一个常规dll,C#调用常规dll,常规dll调用扩展dll。
    2、不用那个扩展dll。
      

  6.   

    是否类似于这类的调用?
    [DllImport("coredll.dll")]
            public static extern uint GetTickCount();
      

  7.   

    应该这个比较好了
    “用C++写一个常规dll,C#调用常规dll,常规dll调用扩展dll。”
      

  8.   

    由于需要MFC组件支持,没有办法直接导入到C#里
      

  9.   

    用VC写个中间DLL吧,过渡一下
      

  10.   

    这种VC的带类的dll,又不是com类的dll,最好的办法就是自己在写个dll,封装她。
    和上述的几位意见一致。
      

  11.   

    去看下这篇文章,或许对你有用 
    http://topic.csdn.net/u/20110413/18/8dbe1747-1cf8-4f5b-8475-972b2d5064a5.html
      

  12.   

    楼主,还不结贴啊!楼上很多人都提出用中间DLL封装的方式了!先用VC创建一个C的DLL(中间DLL),然后用C#调用用C创建的DLL,也可以用C#再封装一次,也就是说,有两个过度DLL,使用时直接调用C# DLL,这样调用更加简单。贴段代码如下:
    C++ DLL:#ifdef __cplusplus    // If used by C++ code, 
    extern "C" {          // we need to export the C interface
    #endif#ifdef ZKADLL_EXPORTS
    #define ZKADLL_API __declspec(dllexport)
    #else
    #define ZKADLL_API __declspec(dllimport)
    #endif
      namespace ZKA
      {    class ZKADLL_API ZM30X
        {
        public:
          ZM30X();
          ~ZM30X();
          bool OpenDevice(string ipaddr);
        }
      }用C封装的DLL:#ifdef __cplusplus
    extern "C" {
    #endif#ifdef ZKADLL_EXPORTS
    #define ZKADLL_API __declspec(dllexport)
    #else
    #define ZKADLL_API __declspec(dllimport)
    #endif
    namespace ZKA{
    ZM30X* CreateZM30X()
    {
    return new ZM30X();
    } void DisposeZM30X(ZM30X* zm30x)
    {
    if(zm30x != NULL)
    {
    delete zm30x;
    zm30x = NULL;
    }
    } BOOL ZM30X_OpenDevice(ZM30X* zm30x, char* ipaddr)
    {
    if(zm30x != NULL)
    {
    return zm30x->OpenDevice(ipaddr);
    }
    else
    {
    return OBJECT_NOT_EXIST;
    }
    }
    }用C#封装的DLL:namespace ZKA
    {
        /// <summary>
        /// ZKA.ZM30X
        /// </summary>
        public class ZM30X : IDisposable
        {
            [DllImport("ZKA32.dll")]
            static private extern IntPtr CreateZM30X();        [DllImport("ZKA32.dll")]
            static private extern void DisposeZM30X(IntPtr zm30x);        [DllImport("ZKA32.dll", EntryPoint = "ZM30X_OpenDevice")]
            static private extern bool CppOpenDevice(IntPtr zm30x, string ipaddr);        /// <summary>
            /// creat object
            /// </summary>
            /// <returns>
            /// none
            /// </returns>
            public ZM30X()
            {
                // We have to Create an instance of this class through an exported function
                this.pNativeObject = CreateZM30X();
            }        /// <summary>
            /// dispose object
            /// </summary>
            /// <returns>
            /// none
            /// </returns>
            public void Dispose()
            {
                Dispose(true);
            }        /// <summary>
            /// dispose object
            /// </summary>
            /// <returns>
            /// none
            /// </returns>
            protected virtual void Dispose(bool bDisposing)
            {
                if (this.pNativeObject != IntPtr.Zero)
                {
                    // Call the DLL Export to dispose this class
                    DisposeZM30X(this.pNativeObject);
                    this.pNativeObject = IntPtr.Zero;
                }            if (bDisposing)
                {
                    // No need to call the finalizer since we've now cleaned
                    // up the unmanaged memory
                    GC.SuppressFinalize(this);
                }
            }        /// <summary>
            /// This finalizer is called when Garbage collection occurs, but only if the IDisposable.Dispose method wasn't already called.
            /// </summary>
            /// <returns>
            /// none
            /// </returns>
            ~ZM30X()
            {
                Dispose(false);
            }        #region Wrapper methods        /// <summary>
            /// Open device
            /// </summary>
            /// <returns>
            /// true - open device succeed, false - open device failed
            /// </returns>
            public bool OpenDevice(string ipaddr)
            {
                return CppOpenDevice(this.pNativeObject, ipaddr);
            }
        }
    }接下来直接调用就可以了。