2012-03-13 34 views
0

CSharpを使用してC++/CLIで実装されたこのプロトタイプを使用する正しい方法を実装する際に問題があります。C#からのpublic refクラスの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

DllImportAttributeを呼び出しは、NATIVE imporするためのものですts。あなたのC++/CLIクラスはネイティブではありません(ref class)ので、このようにインポートする必要はありません。単にMyClassLib.dllへの参照を追加し、それを標準、通常、単純な.NETクラス(C#クラスと同様にMyClass.SIncrement()を呼び出します)として使用します。

関連する問題