- .NET Core 2.0 By Example
- Rishabh Verma Neha Shrivastava
- 270字
- 2021-06-24 18:31:02
Platform Invoke (P/Invoke)
P/Invoke allows us to access structures and functions of unmanaged libraries from our managed code ASP.NET Core 2.0. To use P/Invoke API, we use the System.Runtime.InteropServices namespace. This namespace allows us to access attributes the way we want, to use them with native components. The DllImport attribute is used for the declaration.
static extern bool AllowSetForegroundWindow(int dwProcessId);
The following diagram demonstrates where Interop stands between native and managed code:

For example, if we want to use the AnyPopup() method of user32.dll, we use it as follows:
using System.Runtime.InteropServices;
public class Demo
{
// Import user32.dll (contains the method we need) and define
// the method corresponding to the native method
[DllImport("user32.dll")]
static extern bool AnyPopup();
public static void Main(string[] args)
{
// Invoke the method as a regular managed method.
AnyPopup();
}
}
In this program, we used the DllImport attribute which calls the dynamic-link library (DLL), and we can use all these methods inside this DLL which is decorated with the _declspec(dllexport) keyword or extern "c". We will discuss more about the import and export of methods from the library in the following section.
- MATLAB與C/C++混合編程
- 敏捷開(kāi)發(fā)的藝術(shù)(原書(shū)第2版)
- 企業(yè)性能測(cè)試:體系構(gòu)建、落地指導(dǎo)與案例解讀
- OpenNI體感應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)
- 一線(xiàn)架構(gòu)師實(shí)踐指南
- 實(shí)用軟件工程
- iOS移動(dòng)開(kāi)發(fā)從入門(mén)到精通
- 軟件質(zhì)量管理指南
- 鳳凰項(xiàng)目:一個(gè)IT運(yùn)維的傳奇故事
- 嵌入式軟件測(cè)試:方法、案例與模板詳解
- 軟件項(xiàng)目管理案例教程(第5版)
- Spring Boot趣味實(shí)戰(zhàn)課
- 分布式高可用算法
- Vue.js 3企業(yè)級(jí)項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)(微課視頻版)
- 云原生應(yīng)用構(gòu)建:基于OpenShift