Platform Invocation Services (PInvoke) allows managed code to call unmanaged methods that are compiled in a DLL. Platform is based on the metadata to find the exported functions and direct their arguments at run time.
By using the P/Invoke methods you provide the .NET compiler (C# or VB) with a declaration of the unmanaged function, and you may also need to provide the compiler with a description of how to marshal the parameters and return value from the unmanaged code (DLL).
When P/Invoke calls an unmanaged function, it performs the following steps:
• Finds the unmanaged DLL containing the function.
• Loads the whole DLL into memory.
• Identifies the function address in memory and sends its arguments to the stack marshaling data as required.
• Transfers control to the unmanaged function.
Platform invoke throws exceptions generated by the unmanaged function to the managed caller.
You can specify custom marshaling attributes for fields of classes and structures pushed to or from unmanaged methods. You do this by adding MarshalAs attributes to the fields of the class or structure. You must also use the StructLayout attribute to set the layout of the structure, optionally to control the default marshaling of string members, and to set the default packing size.
In MSIL script Skater sees the 'pinvokeimpl' constructions like the following:
|