C#中了解EXTERN概念的问题 [英] problem in understanding the EXTERN concept in c#

查看:80
本文介绍了C#中了解EXTERN概念的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#中的extern概念不清楚,谁能向我解释以下代码:

I am not clear with the extern concept in c# can anyone explain me the code below:

IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION)

//to disable X close button
const int  MF_BYPOSITION = 0x400;
[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

[DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);

推荐答案

extern告诉编译器该功能不是从当前程序集或其引用的程序集实现的.
在您的代码中,您告诉编译器将从User32.dll中调用这些函数.调用此函数时,程序将加载User32.dll,然后调用该函数.

引用程序集和导入dll之间的一个重要区别是,在您显式调用该dll的函数之前,您的程序将不需要导入的dll.
在您的示例中,仅当您调用导入的函数之一时,且仅在调用时才加载User32.dll.
extern tells the compiler that the function is not implemented from the current assembly or from its referenced assemblies.

In your code, you are telling the compiler that these functions are to be called from User32.dll. When you will make a call to this function, your program will load User32.dll and then call the function.

One important difference between referencing an assembly and importing a dll is your program will not need the imported dll untill you explicitly call a function of that dll.
In your example User32.dll will be loaded only if you make a call to one of the imported functions, and only at the moment of the call.


这篇关于C#中了解EXTERN概念的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆