PInvoke with return type long problem

S

Steve

I'm trying to call some unmanaged methods from a DLL. I did this awhile
ago, a couple years ago... I'm a little rusty. I will show you what I'm
dealing with:

<unmanaged function signature>
STATUS_T WINAPI MSP430_Initialize(CHAR* port, LONG* version);

</unmanaged function signature>

STATUS_T is a long (typedef long STATUS_T)
CHAR is a char (winnt.h)
LONG is a long(winnt.h)

Here is my c# signature:
<c# signature>
[DllImport("MSP430.dll")]

static extern long MSP430_Initialize(string port, out long version);

</c# signature>

This doesn't crash (it did before I added the 'out' keyword) but it also
doesn't return a valid value. If I make the same call from a unmanaged c
app I get a return result of: 0
In my c# app I get: 8589934592


I'm not seeing the problem. I know PInvoke can be a tricky bastard... but
this seems pretty straightforward. Anyone have any ideas for me?

Thanks for reading,
Steve
 
S

Steve

weird. I found that if I changed the return type to an int in the managed
code it works fine.

static extern int MSP430_Initialize(string port, out long version);


Anyone know why?
 
G

Guest

because a int in C# is equivilent to a long in C++.
an int16 is equivlent to a short in C++.

Cheers,
Mark

Steve said:
weird. I found that if I changed the return type to an int in the managed
code it works fine.

static extern int MSP430_Initialize(string port, out long version);


Anyone know why?



Steve said:
I'm trying to call some unmanaged methods from a DLL. I did this awhile
ago, a couple years ago... I'm a little rusty. I will show you what I'm
dealing with:

<unmanaged function signature>
STATUS_T WINAPI MSP430_Initialize(CHAR* port, LONG* version);

</unmanaged function signature>

STATUS_T is a long (typedef long STATUS_T)
CHAR is a char (winnt.h)
LONG is a long(winnt.h)

Here is my c# signature:
<c# signature>
[DllImport("MSP430.dll")]

static extern long MSP430_Initialize(string port, out long version);

</c# signature>

This doesn't crash (it did before I added the 'out' keyword) but it also
doesn't return a valid value. If I make the same call from a unmanaged c
app I get a return result of: 0
In my c# app I get: 8589934592


I'm not seeing the problem. I know PInvoke can be a tricky bastard... but
this seems pretty straightforward. Anyone have any ideas for me?

Thanks for reading,
Steve
 
S

Steve

Thanks Mark, I should have known that :0)

Have a good weekend,
Steve


Mark said:
because a int in C# is equivilent to a long in C++.
an int16 is equivlent to a short in C++.

Cheers,
Mark

Steve said:
weird. I found that if I changed the return type to an int in the
managed
code it works fine.

static extern int MSP430_Initialize(string port, out long version);


Anyone know why?



Steve said:
I'm trying to call some unmanaged methods from a DLL. I did this
awhile
ago, a couple years ago... I'm a little rusty. I will show you what
I'm
dealing with:

<unmanaged function signature>
STATUS_T WINAPI MSP430_Initialize(CHAR* port, LONG* version);

</unmanaged function signature>

STATUS_T is a long (typedef long STATUS_T)
CHAR is a char (winnt.h)
LONG is a long(winnt.h)

Here is my c# signature:
<c# signature>
[DllImport("MSP430.dll")]

static extern long MSP430_Initialize(string port, out long version);

</c# signature>

This doesn't crash (it did before I added the 'out' keyword) but it
also
doesn't return a valid value. If I make the same call from a unmanaged
c
app I get a return result of: 0
In my c# app I get: 8589934592


I'm not seeing the problem. I know PInvoke can be a tricky bastard...
but
this seems pretty straightforward. Anyone have any ideas for me?

Thanks for reading,
Steve
 

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