Some Comments about Calling Win32 APIs in C# Managed Code

王朝c#·作者佚名  2006-01-09
宽屏版  字体: |||超大  

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有