Sunday, July 22, 2007

Unamanaged Code: Use of pInvoke, steps

Platform Invoke or pinvoke:
The process of executing native code from within a .net assembly is called platform invoke
Pinvoke is used to call the Win32 API directly
Also used to access procedures compiled to native code for performance reasons

Steps to use pinvoke:
Import the System.Runtime.InteropServices namespace
Declare the unmanaged procedure using the DLLImport attribute or the declare statement
Map the data types of the procedures parameters to the equivalent .net types
Call the unmanaged procedure and test its return value for success
If the procedure did not succeed, handle the exception using the Marshal object’s GetLastWin32Error method

No comments:

Post a Comment