Some Comments about Calling Win32 APIs in C# Managed Code

类别:.NET开发 点击:0 评论:0 推荐:

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.

本文地址:http://com.8s8s.com/it/it42293.htm