. Net (C #) platform call: dllimportattribute. charset and string sending Encoding

Source: Internet
Author: User

First of all, if you look at the following code is somewhat confusing, it is recommended to read the following msdn on the platform call of the specified character set article: http://msdn.microsoft.com/zh-cn/library/7b93s42f.aspx

 

Learning. net (C #) platform call and object mail, you will find that the platform call Definition of the same system API function can have multiple forms, this article takes the winapi MessageBox to open the handler.

First, you can use dllimportattribute. charset to automatically specify name discovery (based on dllimportattribute. exactspelling) and string sending. For example, the Platform calls of the three functions are the most commonly used definition (from the msdn page above)

 

The first is the MessageBox ANSI function (messageboxa), and the default value of the charset Field Obtained by dllimportattribute is the charset. ANSI enumerated value.

The second is the Unicode function (messageboxw) of MessageBox ).

The last MessageBox uses the automatic detection character set encoding. the NT system calls messageboxw (UNICODE), while 98 calls messageboxa (ANSI ).

[Dllimport ("user32.dll")]

Public static extern int messageboxa (INT hwnd, string text, string caption, uint type );

[Dllimport ("user32.dll", charset = charset. Unicode)]

Public static extern int messageboxw (INT hwnd, string text, string caption, uint type );

[Dllimport ("user32.dll", charset = charset. Auto)]

Public static extern int MessageBox (INT hwnd, string text, string caption, uint type );

// 98 call messageboxa, NT call messageboxw

 

Next, remove all charsets (the default value is charset. ANSI ).

Then, in order to make the Unicode MessageBox work, manually add the marshalas feature to block the string into unmanagedtype. lpwstr (UNICODE string ).

The final MessageBox actually only calls messageboxa (ANSI MessageBox function), because dllimportattribute. exactspelling searches based on charset, and charset is ANSI (Unicode is similar later ).

// Charset. ANSI is used by default.

 

[Dllimport ("user32.dll")]

Public static extern int messageboxa (INT hwnd, string text, string caption, uint type );

 

// Manually use the marshalas feature to block strings in unicode format

[Dllimport ("user32.dll")]

Public static extern int messageboxw (INT hwnd,

[Financialas (unmanagedtype. lpwstr)] string text,

[Financialas (unmanagedtype. lpwstr)] string caption,

Uint type );

 

[Dllimport ("user32.dll")]

Public static extern int MessageBox (INT hwnd, string text, string caption, uint type );

// Call messageboxa

 

If you change all charset to Unicode, the result is the opposite of the above. You need to take special care of messageboxa with the plualas feature, and the last MessageBox will only call the messageboxw function.

// Manually send the string in ANSI format through the financialas feature

[Dllimport ("user32.dll", charset = charset. Unicode)]

Public static extern int messageboxa (INT hwnd,

[Financialas (unmanagedtype. lpstr)] string text,

[Financialas (unmanagedtype. lpstr)] string caption,

Uint type );

 

[Dllimport ("user32.dll", charset = charset. Unicode)]

Public static extern int messageboxw (INT hwnd, string text, string caption, uint type );

 

[Dllimport ("user32.dll", charset = charset. Unicode)]

Public static extern int MessageBox (INT hwnd, string text, string caption, uint type );

// Call messageboxw

 

Finally, if charset is all auto, to ensure that all functions work properly, you must add the same Alas to both the ANSI and Unicode encoding of MessageBox, because the character string sending format is not fixed at this time, of course, if you are sure that the program will run in a specific encoding environment, the corresponding definition can be omitted. Finally, the tchar definition of MessageBox is the same as the previous one, that is, 98 calls messageboxa and NT calls messageboxw.

// Financialas can be omitted if it is a 98 System

[Dllimport ("user32.dll", charset = charset. Auto)]

Public static extern int messageboxa (INT hwnd,

[Financialas (unmanagedtype. lpstr)] string text,

[Financialas (unmanagedtype. lpstr)] string caption,

Uint type );

 

// Financialas can be omitted if it is an NT System

[Dllimport ("user32.dll", charset = charset. Auto)]

Public static extern int messageboxw (INT hwnd,

[Financialas (unmanagedtype. lpwstr)] string text,

[Financialas (unmanagedtype. lpwstr)] string caption, uint type );

 

[Dllimport ("user32.dll", charset = charset. Auto)]

Public static extern int MessageBox (INT hwnd, string text, string caption, uint type );

// 98 call messageboxa, NT call messageboxw

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.