Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2003

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Emmanuel Stapf - 06 Dec 2003 00:56 GMT
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
Mattias Sj?gren - 06 Dec 2003 01:28 GMT
Emmanuel,

>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?

Make it

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

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

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Emmanuel Stapf - 06 Dec 2003 03:03 GMT
> Make it
>
> ... bool marshal(int8) test( ...
>
> Without the marshal modifier, the runtime expects a 32-bit Win32 BOOL
> return value.

Does it make the operation faster?

Manu
Emmanuel Stapf - 09 Dec 2003 18:58 GMT
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
Mattias Sj?gren - 10 Dec 2003 00:46 GMT
Emmanuel,

>Does it make the operation faster?

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

>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?

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

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.