views:

522

answers:

2

Hey
I'm getting this weird error on some stuff I've been using for quite a while. It may be a new thing in Visual Studio 2010 but I'm not sure.
I'm trying to call a unamanged function written in C++ from C#.
From what I've read on the internet and the error message itself it's got something to do with the fact that the signature in my C# file is not the same as the one from C++ but I really can't see it.
First of all this is my unamanged function below:
TEngine GCreateEngine(int width,int height,int depth,int deviceType); And here is my function in C#:
[DllImport("Engine.dll", EntryPoint = "GCreateEngine", CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr CreateEngine(int width,int height,int depth,int device);

When I debug into C++ I see all arguments just fine so thus I can only think it's got something to do with transforming from TEngine (which is a pointer to a class named CEngine) to IntPtr. I've used this before in VS2008 with no problem.
I hope my problem is clear enough for you guys to understand.

+2  A: 

Maybe the problem lies in the calling convention. Are you sure the unmanaged function was compiled as stdcall and not something else ( i would guess fastcall ) ?

PeterK
Sorry but I don't really understand that. I compiled exactly as I said on my question. I didn't add any __std or anything like that. Before it worked very well without it.EDIT: but now apparently adding __std in the function prototype and declaration fixes it. Thanks
Sanctus2099
A: 

Did you ever get a solution? I'm experiencing the same problem. Works fine in 2008 but not 2010. I reduced the test to a simpler method and still get the 2010 error but no error in 2008. C++ signature is: int TestMethod(int op1); C# signature is [DllImport("TstLib")] public static int TestMethod(int op1) I now have the method doing nothing and just returning a zero and I still get the 2010 error.

Kenny Demers