Managed/Native Code Interoperability
4/19/2010
Integrating pieces of managed code with other pieces of managed code is simple and straightforward, as is integrating unmanaged (native) code with unmanaged code. While you can write applications entirely in either managed or native code, it is possible to mix managed and native projects in the same solution. Although keeping your applications uniformly managed or native makes your job as a developer simpler, at some point you will find that you just cannot solve an aspect of your solution without making calls to native or unmanaged code.
Fortunately, you can accomplish these programming tasks by using Platform Invocation Services, or PInvoke, provided by the .NET Framework Common Language Runtime. The pInvoke function provides a way for you to access the Windows API from managed code. For more information on PInvoke, see An Introduction to P/Invoke and Marshaling on the Microsoft .NET Compact Framework on this external Web site.
When using native code from your managed project, you will receive a warning about using unsafe code. In order for your project to build, you must set the Use unsafe code checkbox, from the Build tab of the project's Properties dialog.
Reference
Microsoft is not responsible for content on third-party websites. Links are provided for convenience only, and do not constitute an endorsement.
- PInvoke.NET
Third-party site specializing in mixing native and managed code.
- Native and .NET Interoperability
MSDN section focused on combining managed and unmanaged code.
- Interoperating with Unmanaged Code
MSDN section detailing using COM with managed code.
- Using Native COM Servers from .NET
MSDN topic covering the difference approaches to using existing COM objects with managed code.
- Pinvoke_and_COM_
MSDN section covering Platform Invoke with APIs and COM objects.
- Platform Invoke Sample
A sample that calls native code from a managed application.