API call-P/invoke automatically generates API Declaration

Source: Internet
Author: User

On the Internet, I see a lot of online users. when the Win32 API is called in a program, or the function provided in the vc dll is called, it is always plagued by the correct declaration of the correct C function in C, in C ++, there is nothing more disgusting than generating the declaration-Day of the struct in C. Because:

1. If your struct contains tchar string members, consider ANSI and Unicode DLL.

2. If your struct contains array members, you need to consider long arrays instead of corresponding C # data types.

3. If your struct contains a consortium (union), you need to use the explain option if the consortium contains a consortium.

4. You should also consider that your struct can run on both 32-bit and 64-bit machines.

5. You also need to consider the padding optimization made by the c compiler on the struct.

6. You also need to consider optimizing the struct in. net. For example, CLR will change the order of some. Net struct members to make more effective use of memory.

7. If your structure contains other structs.

8. If your struct contains function pointers ......

9. If your struct contains an array of function pointers.

10. If your struct contains pointers ......

11. If some members in your struct are set by the called C function.

12. CLR provides layout options for several struct types, such as auto, explicit, and sequential.

13. Some struct structures are the synthesis of the above situation. Think about our variant structure.

 

If the struct definition is incorrect, the CLR simply throws an accessviolationexception when using it. It is really called a day, and it is called a ground.

 

Another scenario is defined in C. net struct, so that C or C ++ programs can use this structure to call. net Class Library provides some functions, this conversion is also a painful process.

 

It seems that Microsoft is also suffering from the poor. Net Support of P/invoke, so it is helpless to release the P/invoke InterOP assistant tool. You can download this tool through the following link:

Http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe

 

In fact, this tool is already open-source.

Http://www.codeplex.com/clrinterop/

Download to its source code.

 

A brief introduction to its usage
Automatically generate native function or structure declaration in. Net ProgramSwitch to the "sigimp translate snippet" label, copy the native function or structure declaration to the "native code snippet" text box, and select the "auto generate" dialog box, click "generate" to obtain the corresponding. net Declaration

 

 

Find the Declaration in. Net in Win32 APISelect "sigimp search" and enter the name of the function or structure you want to search for in the "name" text box.

 

 

Verify or generate a. net function (or structure) Declaration in CSwitch to "sigexp" and open a call that contains the P/invoke function. net assembly, this program will display the corresponding c Declaration, and tell you where C # statements are written incorrectly.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.