Using Platform Invoke

In addition to COM and COM+ components, the .NET Framework can access other types of unmanaged code libraries. .NET can call functions from unmanaged Windows API libraries through the use of Platform Invoke (PInvoke), described in the following steps:

1.
After creating a new Visual C# .NET application, place a Label control named lblComputerName on the form. Add the following code at the top of the form's class definition:
using System.Text;
using System.Runtime.InteropServices;
2.
Add the following lines of code in the class definition, which indicates that the GetComputerName method is implemented in kernel32.dll:
 [DllImport("kernel32.dll", CharSet=CharSet.Auto)] public static extern int GetComputerName( StringBuilder ...

Get Developing and Implementing Windows®-Based Applications with Visual C#™ .NET and Visual Studio® .NET Exam Cram™ 2 (Exam 70-316) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.