开发者

Blittable Vs. Non-Blittable in IL

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 function开发者_如何转开发s 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.


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

  • Blittable and Non-Blittable Types
    http://msdn.microsoft.com/en-us/75dwhxf7.aspx
  • Copying and Pinning
    http://msdn.microsoft.com/en-us/23acw07k.aspx
  • CLR Inside Out: Marshaling between Managed and Unmanaged Code
    http://msdn.microsoft.com/en-us/cc164193.aspx
  • .NET Column: P/Invoke Revisited
    http://msdn.microsoft.com/en-us/cc163910.aspx
  • An Overview of Managed/Unmanaged Code Interoperability
    http://msdn.microsoft.com/en-us/library/ms973872.aspx

Hope it helps.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜