开发者

C++ enum in .net app crashes

开发者 https://www.devze.com 2023-02-20 18:05 出处:网络
In my C#.Net app I use a C++ DLL with DllImport. The C++ DLL contains an enum definition: enum mode { A, B, C };

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

精彩评论

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