Home Backend Development C#.Net Tutorial Detailed introduction to calling FormatMessage API in C#

Detailed introduction to calling FormatMessage API in C#

Mar 15, 2017 am 10:10 AM

FormatMessage is an API provided by WINDOWS. It is used to obtain the text information corresponding to the error code returned when calling the Windows API. It has been used under VB before, but in # I haven't used it in ##C#, mainly because I don't understand some rules of calling Windows API in C#.

Recently, I suddenly became very interested in developing mobile phone programs using VC++ WIN32. I picked up C++ that I haven’t used for a long time. Of course, I need to deal with Windows API frequently. I write too much in C#, and then use VB again. I was a little uncomfortable, so I called this method under C#. Why not use it directly in C++? Well,

debugging on mobile phones is a bit annoying, and I don’t want to write too much code. C# is still much more convenient and faster to use.

DWORD WINAPI FormatMessage(    
in          
DWORD dwFlags,    
in          
LPCVOID lpSource,   
 in         
  DWORD dwMessageId,   
   in         
    DWORD dwLanguageId,    
    out         
    LPTSTR lpBuffer,    
    in          
    DWORD nSize,   
     in          
     va_list* Arguments  
     );
Copy after login
 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]StringBuilder lpBuffer, uint nSize, IntPtr arguments);
Copy after login

This is the FormatMessage API prototype and definition in C#. The second and last parameters are not used, so they can be defined as IntPtr or int. If it is int, 0 is passed in when calling. For IntPtr, pass in IntPtr.Zero.

What’s annoying is that the lpBuffer parameter is used to receive the returned text information. In VB, this type of parameter is defined as ByVal String, and then initialized with Space (length) (put

StringInitialized to a space of specified length), just specify the length value for nSize when calling.

In a similar method in C#, the parameters are defined as String LPBUFFER. When calling, when calls,:

  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);
Copy after login

can be known by the return value. The value has not changed.

                                                                                                                                                                                                                        We try to change it to ref and out, but it doesn’t work. ref directly reports a pointer error, and out means the function call fails.

Later, the processing of string in C#was similar to C. It was treated as a

constant

to the processing. Modify the value of a string A new string. Obviously the output parameter cannot be defined as a string here.

The last change to stringbuilder, and use [OUT] Properties

to be modified. When calling, when you call,:

   StringBuilder lpBuffer=new StringBuilder(260);    //声明StringBuilder的初始大小
        int count=FormatMessage(dwFlags,IntPtr.Zero,1439,0,lpBuffer,260,IntPtr.Zero);
Copy after login
Success!

The above is the detailed content of Detailed introduction to calling FormatMessage API in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

Factorial in C# Factorial in C# Sep 03, 2024 pm 03:34 PM

Guide to Factorial in C#. Here we discuss the introduction to factorial in c# along with different examples and code implementation.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

Prime Numbers in C# Prime Numbers in C# Sep 03, 2024 pm 03:35 PM

Guide to Prime Numbers in C#. Here we discuss the introduction and examples of prime numbers in c# along with code implementation.

See all articles