开发者

What is a Managed Prototype?

I just need clarification on what a managed prototype is.

I think it is a method that uses the DLLImport attribute and has a method like so:

[DllImport("开发者_Go百科user32.dll")]
        private static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

Does it always mean this i.e you must have a DLLImport attribute and then a method signiture which is a private static extern???

Cheers


A function prototype is a function that is declared solely as a way for the compiler to work out how to call some code, without necessarily having that "some code" available to look at, so without the prototype it would be flying blind related to argument types, etc.

So for P/Invoke functionality, ie. calling functions in native DLLs from a managed language like C#, yes, you need those prototypes, unless you can find an already existing class in .NET that either wraps that function, DLL, or implements similar functionality in pure managed code.

And yes, you need a [DllImport...] attribute to specify whith DLL that has the function, and it should be static and extern, but it does not necessarily have to be private, although it usually is, typically because you then wrap that function in a managed class to make it easier for the rest of your code to use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜