Using Platform Invoke

The platform invoke (often abbreviated as PInvoke) feature of .NET enables .NET code to call functions from unmanaged libraries such as the Win32 API. The following example lets you see this in detail:

  1. Add a new Visual C# ASP.NET Web Application project to the existing solution, and name it Example11_3 .

  2. Place a Label control named lblComputerName on the form.

  3. Switch to Code view and add the following using directives to the top of your code:

     using System.Text; using System.Runtime.InteropServices; 
  4. 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 buffer, ref uint size); 
  5. Add this code to the Page_Load() event handler:

     private void Page_Load(object sender, System.EventArgs e) {     StringBuilder sbBuf = new StringBuilder(128);     UInt32 intLen = (uint) sbBuf.Capacity;     Int32 intRet=0;     // Call the Win API method     intRet = GetComputerName(sbBuf, ref intLen);     lblComputerName.Text = "This computer is named " +         sbBuf.ToString(); } 
  6. Set the project as the startup project for the solution and run it. The form displays the name of the computer on which the code is run. Remember, if you're using a client on one computer and a server on another, the ASP.NET code executes on the server. In that case, the browser displays the name of the server, not the name of the client.

Note the use of the CharSet.Auto parameter in the DllImport attribute of the GetComputerName() method declaration. You might know that many Windows API calls come in two versions, depending on the character set you're using. For example, GetComputerName really exists as GetComputerNameA (for ANSI characters ) and GetComputerNameW (for Unicode characters). The Auto modifier instructs the .NET Framework to use the appropriate version of the API call for the platform on which the code is running.

graphics/alert_icon.gif

You should use the StringBuilder object for a Windows API call that expects a string buffer.


PInvoke can also handle API calls that require structures as parameters. For example, many API calls require a Rect structure, which consists of four members filled in with the coordinates of a rectangle. In Visual C# .NET, you can declare a structure with explicit byte offsets for each member, which lets you define any structure the Windows API requires, like so:

 [StructLayout(LayoutKind.Explicit)] public struct Rect {     [FieldOffset(0)] public Int32 left;     [FieldOffset(4)] public Int32 top;     [FieldOffset(8)] public Int32 right;     [FieldOffset(12)] public Int32 bottom; } 

The StructLayout attribute tells the Visual C# .NET compiler that you'll explicitly specify the location of the individual fields in the structure. The FieldOffset attribute specifies the starting byte of each field in the structure. By using these attributes, you can ensure that the .NET Framework constructs the same structure the API function is expecting to receive.



MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
ISBN: 789729016
EAN: N/A
Year: 2005
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net