开发者

C++ enum in .net app crashes

In my C#.Net app I use a C++ DLL with DllImport.

The C++ DLL contains an enum definition:

enum mode { A, B, C };

class myClass {
    char name[512];
    mode myMode; //variables beneath this line cause crash
    char pass[512];
};

I defined the same enum in my C#.Net app:

public enum mode { A, B, C };

Now if I access a variable of myclass that is defined after the myMode line, I get a memory corruption error:

getName();  //ok
getPass(); //error

extern "C" LPCTSTR FAR PASCAL EXPORT getPass() { return myC->pass; }

C# wrapper:

[DllImport(DLLNAME)]
public static extern string GetPass();

As a workaround I use int as type for开发者_如何学运维 myMode and everything works. But I am curious how to do it right.


You wrote:

// .cpp
extern "C" LPCTSTR FAR PASCAL EXPORT getPass() { return myC->pass; }

// .cs
[DllImport(DLLNAME)]
public static extern string GetPass();

Ew... Should this be

[DllImport(DLLNAME)]
public static extern string getPass();

I don't know whether C# is case-sensitive or not, but AFAIK it is.

Another trick that I can't understand is LPCTSTR. Should it be LPCSTR because of usage of char[]? And, hmm... Does C# really require PASCAL convention?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜