Detailed introduction to calling the FormatMessage API in C #

Source: Internet
Author: User
FormatMessage is an API provided by Windows that gets the text information for the error code returned when calling the Windows API, previously used in VB, but not in C #, and is largely not familiar with some of the rules that Windows API calls in C #.

Recently, VC + + WIN32 development of mobile phone program suddenly have a great interest, pick up N-long use of C + +, of course, often need to deal with Windows API, write C # write more, and then use VB and a bit unaccustomed, so in C # cut this method. Why not use it directly in C + +? Well, the phone debugging more annoying, do not want to write too much code, C # to use up or to be convenient and much faster.

DWORD WINAPI FormatMessage (    in          DWORD DwFlags,    in          lpcvoid lpsource,    in           DWORD Dwmessageid,      inch             DWORD Dwlanguageid,        out             LPTSTR lpbuffer,        in              DWORD nSize,        in               va_list* Arguments       );
const int format_message_from_system = 0x1000;        const int format_message_ignore_inserts = 0x200;        [DllImport ("Kernel32.dll")]        private static extern int FormatMessage (UINT dwFlags, IntPtr lpsource, uint Dwmessageid, uint Dwlanguageid,         [Out]stri Ngbuilder lpbuffer, uint nSize, IntPtr arguments);

This is the FormatMessage API prototype and defined in C #, where the 2nd and last parameters are defined as either IntPtr or int, and if it is an int, the IntPtr.Zero is passed in when the 0,intptr is called.

The more annoying is lpbuffer this parameter, used to receive the returned text information, in VB, this type of parameters are defined as ByVal string, and then use space (length) to initialize (the string is initialized to a specified length of space), the call is nsize to specify the length value.

In C # I use a similar method, the parameter is defined as String lpbuffer, when called:

  UINT dwflags= Format_message_from_system  |  Format_message_ignore_inserts;              String Lpbuffer=new string (' ', 260);        int Count=formatmessage (Dwflags,intptr.zero,1439,0,lpbuffer,260,intptr.zero);

By returning the value count, you know that the function call succeeded, but the value of lpbuffer has not changed.

Attempts to change to ref and out do not work, ref direct pointer error, out the function call failed.

Later I think of C # in the processing of string similar to C, is to treat it as a constant, modify the value of a string is actually to discard the string and declare a new string, it is obvious that the output parameter can not be defined as a string.

Finally, it is changed to StringBuilder and modified with the [out] property, when called:

   StringBuilder lpbuffer=new StringBuilder (260);    Declares the initial size of StringBuilder        int count=formatmessage (Dwflags,intptr.zero,1439,0,lpbuffer,260,intptr.zero);

Success!

Related Article

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.