P/invoke. NET Call Win32API

Source: Internet
Author: User

Project: Wireless passive temperature measurement software system

Project, to use P/invoke in. NET call Win32API, to implement the INI configuration file read and write Function! Because there is some configuration information that needs to be saved so that the next startup program finishes initializing, this is actually a kind of class persistence. Will be

Some information is written to the INI file (initialization files) for simple persistence support.

Windows provides an API interface for manipulating INI files, and its supported INI file formats are generally as follows:

===============================

[Section1]

Key11=value11

Key12=value12

[Section2]

Key21=value21

Key22=value22

...

[Sectionn]

Keyn1=valuen1

Keyn2=valuen2

===============================

Generally, an INI file can have n sections, each of which can have n key names and values corresponding to each key name and its value in the form of an equation of one row.

The name of the general key can be taken, but it is recommended to make meaningful characters and words. values can generally be integers and strings , and other types are converted.

Common system Configuration files:

C:/boot.ini

C:/windows/win.ini

C:/windows/system.ini

c:/windows/Desktop.ini

C:/windows/resources/themes/windows Classic.theme

Note that the string is stored in the INI file without the quotation marks; the equal sign between the key and value is not allowed, and the comment is semicolon ";" Beginning.

However, it is regrettable that C # is used. NET Framework of the public class library does not provide a direct operation of the INI file class, so the only better way is to call the API function.

And then. NET Framework is based on managed code, and API functions are based on unmanaged code (code executed under the control of the runtime is called managed code. In contrast, code that runs outside the runtime is called unmanaged code. How do you implement the operation between managed code and unmanaged code? NET Framework provides a wide variety of members that support COM interop and platform invoke services under the System.Runtime.InteropServices namespace.

A transaction (transition) occurs when the controlled code interacts with the unmanaged code, which typically happens when using the Platform invoke service (Platform invocation services), which is P/invoke

Platform invoke is a service that enables managed code to invoke unmanaged functions implemented in a DLL, such as calling the system's APIs or dealing with COM objects, by System.Runtime.InteropServices the namespace

In order to invoke a function from managed code in an unmanaged DLL, you create a P/invoke wrapper (Wrapper). A P/invoke wrapper is a. NET compliant method declaration, and the syntax used to create the P/invoke wrapper is essentially the same as the declarative syntax for creating a managed method. The only difference is that the P/invoke wrapper does not contain the function body, but only the method name, return value type, and parameter information. Also, the P/invoke wrapper uses the DllImport attribute. This property is used to locate an unmanaged DLL that contains a target function.

One of the most important attributes of DllImportAttribute can be used to define the platform invocation method used to access the unmanaged API, which provides the information necessary to invoke functions exported from an unmanaged DLL. Here's how to implement the interoperability of C # with API functions.

Read operation:

[DllImport ("kernel32")]
private static extern int getprivateprofilestring (string section, string key, String defval, StringBuilder retVal, int siz E, string filePath);
Section: The name of the paragraph to read
Key: Keys to read
Defval: Default value in case of Read exception
The value corresponding to the Retval:key, or null if the key does not exist
Size: The allowable sizes of the values
Full path and file name of the Filepath:ini file

Write operation:

[DllImport ("kernel32")]
private static extern long WritePrivateProfileString (string section, string key, String val, string filePath);
Section: The name of the paragraph to write
Key: To write the keys, overwrite writes if the key exists
The value that corresponds to the Val:key
Full path and file name of the Filepath:ini file

For example:
MessageBox declaration in Win32 's header file:
int WINAPI messageboxa (hwnd,lpcwstr lptext,lpcwstr lpcaption,uint utype);
So we want to call this MessageBox in C # when we declare this:
Using System.Runtime.InteropServices;
public class Win32 {
[DllImport ("User32.dll")]
public static extern int MessageBox (int hWnd, String text,
String caption, uint type);
}
You can then use the regular. Net method to invoke the MessageBox:
public class HelloWorld {
public static void Main () {
Win32.messagebox (0, "Hello World", "Platform Invoke Sample", 0);
}
}

Using P/invoke: Includes three main steps: declaration, invocation, and error handling

P/invoke. NET Call Win32API

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.