A Closer Look at Platform Invoke
Platform invoke relies on metadata to locate exported functions and marshal their arguments at run time. The following illustration shows this process.
When platform invoke calls an unmanaged function, it performs the following sequence of actions:
Locates the DLL containing the function.
Loads the DLL into memory.
Locates the address of the function in memory and pushes its arguments onto the stack, marshaling data as required.
Locating and loading the DLL, and locating the address of the function in memory occur only on the first call to the function.
Transfers control to the unmanaged function.
Platform invoke throws exceptions generated by the unmanaged function to the managed caller.