繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> 评论及其它 >> Some Comments about Calling Win32 APIs in C# Managed Code

Some Comments about Calling Win32 APIs in C# Managed Code

2005-01-09 20:59:56  作者:bitcof  来源:互联网  浏览次数:2  文字大小:【】【】【
简介:When we need to invoke a Win32 API in a certain .DLL file of the Win32 system:1. Search the API in MSDN or Platform SDK for its accurate prototype, as well as informations of its  parameters'...

When we need to invoke a Win32 API in a certain .DLL file of the Win32 system:

1. Search the API in MSDN or Platform SDK for its accurate prototype, as well as informations of its parameters' types.

2. In the C# source file, declare that function prototype using DllImportAttribute, e.g.:

[DllImport(User32.dll), CharSet=CharSet.Unicode, EntryPoint="MessageBoxW"), ......]

According to the API we want to use, we must specify appropriate properties to the DllImportAttribute object. For example, if we need to call a API which uses a Unicode string as its parameter, we must set the CharSet property to CharSet.Unicode, etc.

3. If the API need pointers or handles as its parameters, in the C# file, we should use System.UIntPtr or System.IntPtr instead.

4. If the API need a pointer to a certain variable as its parameter, in the C# file, we should use ref or out keywords.

5. If the API need a parameter reletive to a structure, we should define the structure in our own C# file.

6. If the API need function pointer as its parameter, we should define a corresponding delegate object.

责任编辑:admin
相关文章