Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 292
Michael Covelli Avatar asked Feb 28 '23 08:02

Michael Covelli


2 Answers

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.

like image 130
Anton Tykhyy Avatar answered Mar 06 '23 21:03

Anton Tykhyy


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.

like image 31
Ricardo Nolde Avatar answered Mar 06 '23 21:03

Ricardo Nolde