Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 3.07 KB

using-explicit-pinvoke-in-cpp-dllimport-attribute.md

File metadata and controls

42 lines (31 loc) · 3.07 KB
title ms.custom ms.date ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Using Explicit PInvoke in C++ (DllImport Attribute) | Microsoft Docs
11/04/2016
cpp-windows
article
C++
marshaling [C++], platform invoke
C++ Interop, platform invoke
interop [C++], platform invoke
platform invoke [C++], marshaling in C++
data marshaling [C++], platform invoke
18e5218c-6916-48a1-a127-f66e22ef15fc
11
mikeblome
mblome
ghogen

Using Explicit PInvoke in C++ (DllImport Attribute)

The .NET Framework provides explicit Platform Invoke (or PInvoke) features with the Dllimport attribute to allow managed applications to call unmanaged functions packaged inside DLLs. Explicit PInvoke is required for situations where unmanaged APIs are packaged as DLLs and the source code is not available. Calling Win32 functions, for example, requires PInvoke. Otherwise, use implicit P{Invoke; see Using C++ Interop (Implicit PInvoke) for more information.

PInvoke works by using xref:System.Runtime.InteropServices.DllImportAttribute. This attribute, which takes the name of the DLL as its first argument, is placed before a function declaration for each DLL entry point that will be used. The signature of the function must match the name of a function exported by the DLL (but some type conversion can be performed implicitly by defining the DllImport declarations in terms of managed types.)

The result is a managed entry point for each native DLL function that contains the necessary transition code (or thunk) and simple data conversions. Managed functions can then call into the DLL through these entry points. The code inserted into a module as the result of PInvoke is entirely managed and explicit PInvoke is supported for /clr, /clr:pure, and /clr:safe compilations. The /clr:pure and /clr:safe compiler options are deprecated in Visual Studio 2015. For more information, see Pure and Verifiable Code (C++/CLI).

In This Section

See Also

Calling Native Functions from Managed Code