C# formatting external Dll function parameters

为君一笑 提交于 2019-12-04 11:45:07
Joel Coehoorn

I have yet to find a good reference on this topic

Here you go.

Oddly, the site is down at the moment, but it's a very good resource for marshalling problems. In the mean time, you can use Archive.net's archive of pinvoke.net at http://web.archive.org/web/20080202041852/http://www.pinvoke.net

That looks right. The first part of your question (relating to MarshalAs) tells the compiler to convert the managed C# variable into a PSTR. Marshaling in the .NET environment refers to transferring data between the managed and unmanaged environment. These attributes (among several others) tell the compiler how to do it specifically.

The second point indicates that it's going to pass a pointer to the struct. The IntPtr type in .NET is a managed type that's used to hold on to a particular piece of memory. In the .NET world, pointers (generally) don't exist, though there still are reference types. Pointer and handle management is done for us by the CLR. The IntPtr struct is used when marshaling pointers to data structures or primitive types

In addition to the P/Invoke site, Adam Nathan's .NET and COM: The Complete Interoperability Guide is something worthwhile adding to your bookshelf if you've more than a passing interest in .NET interop.

Kev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!