1

以下はc++ dllクラスです

class A
{
  public: 
   int __thiscall check(char *x,char *y,char *z);
  private:
   B *temp;
};

class B
{
  friend class A;
  Public:
   B();
   B(string x,string y,string z);
   ~B();
  private:
   string x;
   string y;
   string z;
};

c++ dll メソッドの定義は以下のとおりです

__declspec(dllexport) int __thiscall A::check(char *x,char *y,char *z)
{
  temp=new B(x,y,z); //getting error at this point when i am assigning memory to temp
  return 1;
}

c# dllのインポートはこんな感じ

[DllImport("MyDll.dll", CallingConvention = CallingConvention.ThisCall, ExactSpelling = true, EntryPoint = "check")]
public static extern int check(IntPtr val,string x,string y,string z);

c++ dll ビルドは正常に動作しますが、c# が c++ dll メソッドを呼び出すときも見栄えがよく、関数に入ると、メソッドの最初の行で、クラス A でクラスのポインターとして宣言されている一時ポインター用のメモリを作成しようとします。プライベートなB. それが与えているエラーは

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
4

2 に答える 2

0

問題が見つかりました。問題は c++ のチェック関数にあります。一時はこのように作成されるはずです。

int __thiscall A::check(char *x,char *y,char *z)
{
  A *xyz=new A();
  A->temp=new B(x,y,z); // doing this eliminates the issue.
  return 1;
}

これについて私を助けてくれたすべての人に感謝します。

于 2012-06-05T15:20:34.657 に答える
0

は、メンバー メソッドではなく__declspec(dllexport)、クラス (例: class ) にある必要があります。__declspec(dllexport) MyClass

エントリ ポイントは、" " ではなく、マングルされた C++ 名 (例: 2@MyClass@MyMethod?zii) である必要がありますcheckDepends.exe
を 使用して名前を見つけることができます。

于 2012-06-04T18:06:30.793 に答える