[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

Marshaling a bool from unmanaged to managed code

J.R. Heisey

11/26/2007 10:04:00 PM

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


6 Answers

Michael Phillips, Jr.

11/27/2007 12:44:00 AM

0

Did you try using UnmanagedType.U1 to represent bool?

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

"J.R. Heisey" <jrheisey@synaptics.com> wrote in message
news:uKfXHhHMIHA.4272@TK2MSFTNGP05.phx.gbl...
>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
>
>


J.R. Heisey

11/27/2007 3:03:00 AM

0

According to the on line help it said I1. However your assertion is
validated in

http://msdn2.microsoft.com/en-us/librar...(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,

Michael Phillips, Jr. wrote:
> Did you try using UnmanagedType.U1 to represent bool?
>
> See:
> http://msdn2.microsoft.com/en-us/library/t2t...
>
> "J.R. Heisey" <jrheisey@synaptics.com> wrote in message
> news:uKfXHhHMIHA.4272@TK2MSFTNGP05.phx.gbl...
>
>>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
>>
>>
>
>
>

William DePalo [MVP VC++]

11/28/2007 2:17:00 AM

0

"J.R. Heisey" <jrheisey@synaptics.com> wrote in message
news:uKfXHhHMIHA.4272@TK2MSFTNGP05.phx.gbl...
> 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?

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/virtualb... ) 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


sugarless anko

10/26/2010 2:31:00 PM

0

On 10?26?, ??2:08, "AleXX" <al...@ka.bishop-dalai-baru.net> wrote:
> "sugarless anko" <serbet...@gmail.com> wrote in message
>
> news:0f4e9b06-842a-45c5-82ff-623fd45b82af@p26g2000yqb.googlegroups.com...
>
> > Kill japs! kill japs kill more japs! Another "USA", extreme
> > ethnocentric China emerged in Asia.
>
> >http://sankei.jp.msn.com/photos/world/china/101024/chn10102......
> >http://sankei.jp.msn.com/photos/world/china/101024/chn10102......
>
> Because of unpardonable war crimes committed by the imperial army of japan
> during world war 2. Atomic Bombing of Hiroshima and Nagasaki were still not
> an "equalizing" punishment for japan. The world need to see more punishment
> meted out to this japanese nation with a cruel past.

Chinese, Koreans and you white Anglo-Saxons always accuse the japs for
that however I have a question.
Chinese and whites are claiming that the japs are an inferior version
of Chinese,
a kind of Chinese species, and the land of the japs is a part of
China.
You never admit the independence of the jap race and nation so you
claim that the japs are Chinese.
Then, why do you claim the "Chinese of japs" for that Nanking thing?
Eh???
Don't you think it is contradicting?

There is no race called 'japs" on this planet and those addressed as
the japs today are actually Chinese, according to Chinese authority
and USA. It refers to the fact that you whites actually nuked the
Chinese and you are the brutal species butchering the Chinese.

Anti-DabianchenVirus

10/26/2010 2:47:00 PM

0

Proof that abianchen/Meichi/report2009 is not "a guy from Taiwan",
like she claims, but an ugly Philippino dyke.

On Jul 13, 12:40 pm, "abianc...@my-deja.com" <abianc...@my-deja.com>
wrote:
> Hey Psycho Xangdi (aka Chairman Mao Says), not sure you want to prove
> abianchen is your Chinese daddy or Meichi is your Filipino mom or
> report2009 is your Chinese grandpa? I am sure abianchen has no problem
> being your Chinese dad since he is your mom's best client. Who knows,
> maybe abianchen is your bio daddy, want to test DNA?

Unable to refute the overwhelming evidence proving that shit face ??
Meichi/Dabianchen/report2009 is an ugly Philippino lesbian
pathological liar, she reverts to her moron mode of childish taunts
against Xangdi's parents, going as far as to present herself as
"Xangdi's father"! This sort of writing can only come from a demented
person with no self-respect and certainly no morals.

Proof that abianchen/Meichi/report2009 is not "a guy from Taiwan",
but
an ugly Philippino dyke:
http://groups.google.com/group/soc.culture.china/msg/31e3f3......
http://groups.google.com/group/soc.culture.china/msg/fc81fe......

Proof that abianchen/Meichi/report2009 can't read/write Chinese other
than cutting-and-pasting from the internet:
http://groups.google.com/group/soc.culture.china/msg/757601......
http://groups.google.com/group/soc.culture.china/msg/282011......
http://groups.google.com/group/soc.culture.china/msg/83029b......
http://groups.google.com/group/soc.culture.china/msg/dd8518......

Proof that abianchen/Meichi/report2009 is a pathological liar:
http://groups.google.com/group/soc.culture.china/msg/ee41fc......
http://groups.google.com/group/soc.culture.china/msg/995a71......
http://groups.google.com/group/soc.culture.china/msg/fdeab5......
http://groups.google.com/group/soc.culture.china/msg/4bdc74......
mode=source
http://groups.google.com/group/soc.culture.china/msg/90a8fd......
http://groups.google.com/group/soc.culture.china/msg/240eb7......

Proof that abianchen/Meichi/report2009 makes persitent childish and
girlish claims:
http://groups.google.com/group/soc.culture.china/msg/7d3d15......

Proof that abianchen/Meichi/report2009 is a loser:
http://groups.google.com/group/soc.culture.china/msg/8af8fd......

Proof that abianchen/Meichi/report2009 is childish and repetitive:
http://groups.google.com/group/soc.culture.china/msg/5646b3......

sugarless anko

10/26/2010 3:15:00 PM

0

On 10?26?, ??2:08, "AleXX" <al...@ka.bishop-dalai-baru.net> wrote:
> "sugarless anko" <serbet...@gmail.com> wrote in message
>
> news:0f4e9b06-842a-45c5-82ff-623fd45b82af@p26g2000yqb.googlegroups.com...
>
> > Kill japs! kill japs kill more japs! Another "USA", extreme
> > ethnocentric China emerged in Asia.
>
> >http://sankei.jp.msn.com/photos/world/china/101024/chn10102......
> >http://sankei.jp.msn.com/photos/world/china/101024/chn10102......
>
> Because of unpardonable war crimes committed by the imperial army of japan
> during world war 2. Atomic Bombing of Hiroshima and Nagasaki were still not
> an "equalizing" punishment for japan. The world need to see more punishment
> meted out to this japanese nation with a cruel past.

Incidentally, Chinese foreign minister officially declared that by the
year 2050, China will regain its own territory of Japanese islands.
http://isukeya.iza.ne.jp/images/user/20100424/...

If you ask someone in London "where is Tokyo?", he/she will answer
"It's in China".
95% of whites in USA would answer so too.
They simply have never realized the presence of a sovereign state
Japan and they are subconsciously thinking it's China. So in your
world, the japs are Chinese.
It means that you nuked the Chinese, you indiscriminately butchered
the Chinese from behind, raided, burned the Chinese from behind.