description | title | ms.custom | ms.date | helpviewer_keywords | ms.assetid | ||||
---|---|---|---|---|---|---|---|---|---|
Learn more about: How to: Call Native DLLs from Managed Code Using PInvoke | How to: Call Native DLLs from Managed Code Using PInvoke | get-started-article | 11/04/2016 |
| 3273eb4b-38d1-4619-92a6-71bda542be72 |
Functions that are implemented in unmanaged DLLs can be called from managed code using Platform Invoke (P/Invoke) functionality. If the source code for the DLL is not available, P/Invoke is the only option for interoperating. However, unlike other .NET languages, Visual C++ provides an alternative to P/Invoke. For more information, see Using C++ Interop (Implicit PInvoke).
The following code example uses the Win32 GetSystemMetrics function to retrieve the current resolution of the screen in pixels.
For functions that use only intrinsic types as arguments and return values, no extra work is required. Other data types, such as function pointers, arrays, and structures, require additional attributes to ensure proper data marshaling.
Although it is not required, it is good practice to make P/Invoke declarations static members of a value class so that they do not exist in the global namespace, as demonstrated in this example.
// pinvoke_basic.cpp// compile with: /clrusingnamespaceSystem;usingnamespaceSystem::Runtime::InteropServices; value classWin32 { public: [DllImport("User32.dll")] staticintGetSystemMetrics(int); enumclassSystemMetricIndex { // Same values as those defined in winuser.h. SM_CXSCREEN = 0, SM_CYSCREEN = 1 }; }; intmain() { int hRes = Win32::GetSystemMetrics( safe_cast<int>(Win32::SystemMetricIndex::SM_CXSCREEN) ); int vRes = Win32::GetSystemMetrics( safe_cast<int>(Win32::SystemMetricIndex::SM_CYSCREEN) ); Console::WriteLine("screen resolution: {0},{1}", hRes, vRes); }