views:

161

answers:

3

I'm trying to make sure that my Managed to Unmanaged calls are optimized. Is there a quick way to see by looking at the IL if any non-blittable types have accidentally gotten into my pinvoke calls?

I tried just writing two unmanaged functions in a .dll, one that uses bool (which is non-blittable) and one that uses ints. But I didn't see anything different when looking at the IL to let me know that it was doing something extra to marshal the bool.

+1  A: 

There is no marshaling-related stuff in IL. Instead, you should analyze methods' parameter types (with modifiers) and return types following the rules described in MSDN. These rules are fairly simple, it ought to be possible to write an automated checker and plug it into e.g. FxCop.

Anton Tykhyy
A: 

There is no marshaling-related stuff in IL.

Thats not true.

Even for 'bool' there may be a need for some marshaling to be done depending on how 'bool' is defined in the unmanaged language. For example, in MS C++ alone, there are two types 'bool' which is 1 byte and 'BOOL' which is 4 bytes. On the other hand, I think, .Net bool is 1 byte, so you may have to specify how to marshal it depending on the size of the unmanaged bool.

logicnp
It seems like there must be something there. Bool is listed specifically as non-blittable. But I don't see anything unusual in the IL to indicate that any marshalling is going ok when I look at it.
Michael Covelli
So how is it in IL? Where does the assembly say `bool` is not blittable? Yes, there might be `MarshalAs` attributes, but 1) that's strictly speaking not IL and 2) neither absence nor presence of `MarshalAs` says anything about blittability — type analysis is what's required.
Anton Tykhyy
+1  A: 

Anton Tykhyy is right. Type analysis is what you need; searching through IL is not going to help you. I recommend the reading of the following articles to decide if you're doing it right.

Hope it helps.

Ricardo Nolde