Platform Invoke

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 8.   New Features in Visual C++ .NET


The direct use of unmanaged APIs, exposed by WIN32 DLLs, in managed applications is one of the important features of managed extensions for C++. The C-styled (unmanaged) functions in existing native DLLs can be directly used in managed applications with the help of platform invocation services (PInvoke). PInvoke locates and invokes an exported function and marshals its arguments at runtime across the managed and unmanaged boundary. For this, PInvoke relies on metadata. Data marshalling is handled automatically. Hence if customized data marshalling is not required, there is no need for you to use PInvoke. Marshalling is required for types that don't have forms such as char, string, and struct types. Customized data marshalling can be simplified with the use of PInvoke because instead of writing procedural marshalling code, you can declare marshalling information in attributes. The DLL Import attribute has to be declared for the unmanaged APIs that are being accessed by the managed application. The sequences of steps that occur when PInvoke calls an unmanaged function are

  1. The DLL containing the function is located.

  2. This DLL is loaded into the memory.

  3. The function is then located in memory and its arguments are pushed onto the stack. Data marshalling is done if required.

  4. Control is then transferred to the unmanaged function.

Note that locating and loading of the DLL and the function in the memory occurs only when the function is called for the first time. If the called API throws an unmanaged exception, PInvoke wraps it as a managed exception and throws it to the managed code.

Now that we have explored the various features of managed extensions, let's move to the evolutionary aspect of the Web application development, Web services.


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

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