2012-03-13 59 views
0

我无法实现正确的方式来使用C++/CLI中使用CSharp实现此原型。从C#调用C++/CLI公共参考类的静态成员

C++/CLI实现:

// MyClassLib.h 
#pragma once 
using namespace System; 

namespace MyClassLib 
{ 
    public ref class MyClass 
    { 
    public: 
     int Increment(int number); 
     static int SIncrement(int number); 
    }; 
} 
// This is the main DLL file. 

#include "stdafx.h" 
#include "MyClassLib.h" 

namespace MyClassLib 
{ 
    int MyClass::Increment(int number) 
    { 
     return number+1; 
    } 
    int MyClass::SIncrement(int number) 
    { 
     return number+1; 
    } 
} 

用法实现:

using System; 
using System.Runtime.InteropServices; 
using MyClassLib; 

namespace UseClassLib 
{ 
    class Program 
    { 
     [DllImport("MyClassLib.dll")] 
     public static extern int SIncrement(int number); 

     static void Main(string[] args) 
     { 
      MyClass mc = new MyClass(); 

      int number = 1; 
      number = mc.Increment(number); // increment ok 

      number = SIncrement(number); // System.EntryPointNotFoundException here 
     } 
    } 
} 

回答

2

number = MyClass.SIncrement(number); 和没有P /调用

4

DllImportAttributeNATIVE祁门功夫TS。你的C++/CLI类不是原生的(ref class),所以你不必以这种方式导入它。只需添加对MyClassLib.dll的引用,并将其用作标准,普通,简单的.NET类(像使用C#类一样调用MyClass.SIncrement())。