Marshalling `bool' .NET type to a unsigned char C type

Marshalling `bool' .NET type to a unsigned char C type

Post by Emmanuel S » Sun, 07 Dec 2003 09:56:21


Hi,

I have the following C code generated in `test.dll':

__declspec(dllexport) unsigned char __stdcall test (int i, int j)
{
return (unsigned char) ((i & j) == j);
}


In the .NET side I map it into:

.method public hidebysig static pinvokeimpl("test.dll" ansi)
bool test ([in] int32 i,
[in] int32 j) cil managed preservesig
{
}


However I've noticed that if my C code is compiled with all optimizations (-Ox)
then on the .NET side for the value i=0x6cf0000 and j=0x1000000, I get a true
value where I should get a false. If I don't have the -Ox flag then it works
properly.

I think that is possibly due to the marshalling of `bool' but I'm not sure. If
it is what should I do so that this above code works properly?

Thanks,
Manu
 
 
 

Marshalling `bool' .NET type to a unsigned char C type

Post by Mattias Sj » Sun, 07 Dec 2003 10:28:13

Emmanuel,


Make it

... bool marshal(int8) test( ...

Without the marshal modifier, the runtime expects a 32-bit Win32 BOOL
return value.



Mattias

--
Mattias Sjren [MVP] mattias @ mvps.org
http://www.yqcomputer.com/ | http://www.yqcomputer.com/
Please reply only to the newsgroup.

 
 
 

Marshalling `bool' .NET type to a unsigned char C type

Post by Emmanuel S » Sun, 07 Dec 2003 12:03:31

> Make it

Does it make the operation faster?

Manu
 
 
 

Marshalling `bool' .NET type to a unsigned char C type

Post by Emmanuel S » Thu, 11 Dec 2003 03:58:17

I've added a marshaller modifier for bool on my parameters too. I've noticed
something strange, if I pass a False value to my pinvoke, the C calls receives a
value of 0x01 and not 0x00 as I expected? I'm I doing something wrong?

My pinvoke is defined as:

.method public hidebysig static pinvokeimpl("libcursor.dll" ansi)
void Fdui0ly([in] native int hwnd,
[in] int32 bar_flag,
[in] bool marshal( int8) show_flag) cil managed
preservesig
{
)

And on the call site in .NET I have something like:

.method public hidebysig newslot virtual
instance void my_call([in] native int hwnd,
[in] int32 bar_flag) cil managed
{
// Code size 9 (0x9)
.maxstack 8
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: ldc.i4 0
IL_0003: call void MyClass::Fdui0ly(native int, int32, bool)
IL_0008: ret
} // end of method WelWindow::CwinShowScrollBar

Manu
 
 
 

Marshalling `bool' .NET type to a unsigned char C type

Post by Mattias Sj » Thu, 11 Dec 2003 09:46:07

Emmanuel,


I don't know, I haven't done any perf testing.



I think this is a runtime bug, but I can't remember the exact details.
It seems to work correctly in Whidbey though.

But for parameters, since the unsigned char still occupies 32 bits on
the stack, I believe you might as well use the default marshaling and
leave out marshal(int8).



Mattias

--
Mattias Sjren [MVP] mattias @ mvps.org
http://www.yqcomputer.com/ | http://www.yqcomputer.com/
Please reply only to the newsgroup.