2012-03-13 122 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())。