P/Invoke Problem: Native exception when writing to a reference type

S

simon.hudson1

Hi All,

I'm writing a C++ dll which takes two parameters, both pointers to
long, modifies them and returns an int (status).

The problem is, as soon as I modify the long parameters I get a native
exception (0xc0000005).

My C++ function is declared as :

int __declspec(dllexport) CMyLib::GetCurrentPos(long* latitude, long*
longitude);

....implemented as:

int CMyLib::GetCurrentPos(long* latitude, long* longitude)
{
*latitude = 0; // test vals
*longitude = 0;

return 0;
}

My C# reference is:

[DllImport("MyLib.dll", EntryPoint = "GetCurrentPos")]
private static extern int GetCurrentPosition(ref int latitude,
ref int longitude);

And the C# call is:

int latitude = 0;
int longitude = 0;

if (GetCurrentPosition(ref latitude, ref longitude) == 0)
{
...
}

I'm pretty sure I'm screwing up the marshalling but I have no idea how.
From the C# end I have tried int, Int32 and long. I've also tried
changing both ends to double, just for the hell of it. In all cases, I
got the same native exception.

Any thoughts, help, advice would be very much appreciated.



Simon
 
S

simon.hudson1

More info:

The stack is definitely fubarred.

In C#, before the call:-

? &latitude
0x2603f20c
latitude: 0
? &longitude
0x2603f208
longitude: 0

Inside the dll function:-

+ this 0x2603f20c {CMyLib*} CMyLib*
+ latitude 0x2603f208 {long int*} long int*
+ longitude 0x00008800 {long int*} long int*

How in god's name did I manage to do that? More to the point, how do I
fix it?

Many TIA,


Simon
 
G

Guest

I'm confused by your code. It looks like you have a class named CMyLib, and
it exposes a function named GetCurrentPos (I also don't like the mix of long
and int, it looks like different data types, even though they're not).

If that's the case, then there's an inherent problem. There's no instance
of the CMyLib class created anywhere, so you can't really expect to be able
to call methods on a class. For marshaling you need to either move your
implementation to a C function, or make a class factory.
 
S

simon.hudson1

Thanks for the reply, Chris.

Yes, I hadn't understood that P/Invoke is not intended for use with
instance methods. After taking the function out of the class
definition...

extern "C" __declspec(dllexport) int WINAPI GetCurrentPos(long*
latitude, long* longitude)

....things started to behave much better. There are other, quite
fundamental problems elsewhere, though (this was intended as a wrapper
for the TomTom SDK, but I've decided to bin it and buy a managed
wrapper).

Thanks,

Simon
 
G

Guest

Agai, I'd change them all to int types. It just looks wrong to see it
taking in long and returning int. Both are 32 bit values.

-Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top