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 / November 2007

Tip: Looking for answers? Try searching our database.

Marshaling a bool from unmanaged to managed code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
J.R. Heisey - 26 Nov 2007 22:04 GMT
I have a DLL that takes a pointer to a callback function of type:

typedef void ( __stdcall * StdMyCallback_T)(void* context, const char *
sName, bool bState,
                    EError error, const char * pErrorMsg);

I have defined a delegate as such:

public delegate void MyCallbackDelegate(IntPtr iPtr, IntPtr sName,
             [In,MarshalAs( UnmanagedType.I1 )] bool bState, Error error,
IntPtr sErrorMsg);

The callback function in managed code is defined as:

private void CDCI_My_Callback_Handler(IntPtr iPtr, IntPtr sName, bool
bState,Error error,
                                                                   IntPtr
sErrorMsg)
{
 MyEventArgs evt = new MyEventArgs(sName, bState,error,
                                                            sErrorMsg);
 ... // other stuff
}

However the bState parameter is not properly marshaled. In the MarshalAs
directive I've also tried UnmanagedType.Bool. When I look at the assembly
code in the C++ based unmanaged DLL for the parameter bState, I see the al
register getting set then eax register is pushed onto the stack. Three extra
bytes with undefined values are pushed. I expect for memory alignment
reasons. Now how do I get the Managed code to marshal the bool parameter
properly?

Microsoft you listening?

Thanks,
J.R. Heisey
Michael Phillips, Jr. - 27 Nov 2007 00:44 GMT
Did you try using UnmanagedType.U1 to represent bool?

See:
http://msdn2.microsoft.com/en-us/library/t2t3725f.aspx

>I have a DLL that takes a pointer to a callback function of type:
>
[quoted text clipped - 32 lines]
> Thanks,
> J.R. Heisey
J.R. Heisey - 27 Nov 2007 03:03 GMT
According to the on line help it said I1. However your assertion is
validated in

http://msdn2.microsoft.com/en-us/library/ms182206(VS.80).aspx.

However I was able to get it working after I found other instances of
bool parameters in functions defined in the same unmanaged DLL. When I
explicitly specified the marshaling of these bools my callback started
working. Perhaps there was some stack corruption due to the unmarshaled
parameter.

Hmm ... looking over my code I use U1 in most cases and in the callback
I'm using I1. Seems to work but I will revert the I1 to U1 for consistancy.

Thanks,

> Did you try using UnmanagedType.U1 to represent bool?
>
[quoted text clipped - 37 lines]
>>Thanks,
>>J.R. Heisey
William DePalo [MVP VC++] - 28 Nov 2007 02:16 GMT
> However the bState parameter is not properly marshaled. In the MarshalAs
> directive I've also tried UnmanagedType.Bool. When I look at the assembly
[quoted text clipped - 5 lines]
>
> Microsoft you listening?

I hope there's a way.

FWIW: I can tell you that returning C++ bools ( as a return value not as an
writable argument ) from native to managed code was not possible for me with
the 1.1 version of framework (
http://www.codeproject.com/buglist/virtualboolbug.asp ) and managed
extensions for C++. My code is liberally sprinkled with lines like

   _asm xor eax, eax ; clear all thirty two bits

to get things to work. It was either that or change the type from bool to
BOOL.

Regards,
Will

Rate this thread:







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.