PInvoke或DllImport [英] PInvoke or DllImport
问题描述
我是NET的新人.
有谁知道在NET中使用PInvoke时幕后发生的事情吗?我很好奇CLR如何找到win32函数以及如何使用它.我要问的是,它会进入WIN32文件夹并复制该功能(或在其上进行反映)然后使用它?如果我使用的是win95或LINUX,会检查操作系统类型吗?
[DllImport("user32.dll")]
私有静态外部int MessageBox(int i,字符串文本,字符串标题,uint类型);
在此先感谢
Johan
I''m a NET newby.
Does anyone understand what happens behind the scenes when using PInvoke in NET?I''m curious as to how the CLR finds the win32 function and how it consumes it.What I''m asking is, does it go to the WIN32 folder and copy the function (or reflect over it) and then use it?What if I''m using win95 or LINUX, does it check the OS type?
[DllImport("user32.dll")]
private static extern int MessageBox( int i,string text, string caption, uint type);
Thanks in advance
Johan
推荐答案
CLR知道如何专门处理外部呼叫.在这种情况下,将使用标准方法调用外部方法-使用LoadLibrary
/GetProcAddress
.当然,还有一些细节,例如需要在调用之前将托管类型编组为本机类型,然后在调用之后进行转换的管道代码.
The CLR knows how to specially handle external calls. In this case the external method is invoked the standard way - which is by usingLoadLibrary
/GetProcAddress
. Of course there are details like the plumbing code required to marshal managed types to native types before the call, and then to convert back after the call.
这篇关于PInvoke或DllImport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!