简体   繁体   English

为什么P / Invoke函数在C#中必须是静态的?

[英]Why must P/Invoke functions be static in C#?

see the code: 看代码:

class VirtualMemoryManager
{
    [DllImport("kernel32.dll",EntryPoint="GetCurrentProcess")]
    internal static extern IntPtr GetCurrentProcessHandle();
}

Why the "GetCurrentProcessHandle" must "static" 为什么“ GetCurrentProcessHandle”必须“静态”

This is a requirement for P/Invoke methods because there is no consistent notion of an instance in the Windows API. 这是P / Invoke方法的要求,因为Windows API中没有实例的一致概念。

Source: http://msdn.microsoft.com/en-us/magazine/cc164123.aspx 来源: http//msdn.microsoft.com/en-us/magazine/cc164123.aspx

Externally imported functions are static by nature. 外部导入的函数本质上是静态的。 By not making them static they will not gain anything. 通过使它们不静态,他们将不会获得任何收益。 An instance method can use local variables, which is impossible with dll import routines anyway. 实例方法可以使用局部变量,这对于dll导入例程而言是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM