Add error code to your module _ use the VC's own tool MC to create a resource and add it to a DLL or EXE

Source: Internet
Author: User
Tags control characters custom name

This article describes how to use Visual Studio 's own MC.exe tool to create a message resource and add it to your own DLL ( or . exe) To Add the error code for your own module.

OneMCTools Introduction

Message Compiler (MC) is a tool used to create a message resource that is referenced by a DLL ( or EXE) module. the input to the MC is a text file in a specific format, the *.mc file, which makes it easy to define multiple language support in one file.

When writing to complete a*.MCfile, you need some action to use this file in your program. First, useMCtool to compile this file into a*.rcfile (also generates*.hand the*.binfile), and then the*.rcand*.hadd into the correspondingDLL(EXE) module, and finally through the corresponding event log function orFomatmessagefunction to use the message text in it. (See the following example for specific procedures)

Second, the message text file definition (*.MCfiles)

1. Overview

Messages defined in a *.mc file,the MC tool automatically assigns a number to each message, and then generates a *.h file. The message ID in which the program refers .

The most common syntax for defining messages in a *.MC file is keyword=value, and the spaces on both sides of the equals sign are ignored. The next similar definition is delimited by a space or line break. where VALUE can be an integer constant, or a symbol similar to a macro identifier, or a 8 - character (or less) name.

2. Comments

Single-line comment: Start with a semicolon, followed by the comment text, in order to appear in the generated *.h file as a comment state, do not hit the semicolon after a //. as follows:

;// This is a single-line comment


Multiline Comment: Each line starts with a semicolon, but for the same purpose, do not write multiple lines of comments as follows:

;/* This is a multiline comment

; This is a multi-line comment

; This is a multi-line comment */

3. Header definition Block

The *.MC file begins with the header definition, and the header defines some name identifiers and language identifiers for use by subsequent message definitions. The head usually contains the following 0 or more declarations (which can be multiple identical declarations, such as the Language section).

Messageidtypedef=type----The type used for the message identity definition, such as #define name ((type) 0xnnnnnnnn) Note: *.h Such definitions can be seen in the file. This type must be able to accommodate the range of message codes (in the first place), such as DWORD. This type can also be customized in the program section. The default state is untyped, and there is no corresponding conversion. You can only start defining it when you need to use this declaration. such as:messageidtypedef=dword.

Severitynames= (Name=number[:name])---- Set the message code in the number of, The number of bits, that is, Severity section.

The default settings are as follows:

Severitynames= (

success=0x0

informational=0x1

warning=0x2

error=0x3

)

Facilitynames= (Name=number[:name])----Set the 27--16 bit in the message code , that is, the Facility part, The default settings are as follows:

Facilityname= (

System=0x0ff

Application=0xfff

)

languagenames= (name=number:filename)---- Set the language identifier used for the message, and can be set in multiple language versions. The default settings are as follows:

Languagenames= (english=1:msg0001), where 1 can be generated by macro Makelangid, such as the generation of Simplified Chinese, makelangid (lan_ Chines, sublang_chinese_simplified), the generated value is 0x804, then we can define languagenames= (chinese= 0x804:msg0804), where MSG0804 is our custom name, the generated *.bin file will be given its command, as Msg0804.bin .

Outputbase=number---- generate a constant numeric format for messages, such as specifying the number of digits in the , generating A number of ten decimal numbers, and so on.

4. Message body Definition

The message text file contains the following 0 or more declarations, where MessageId Identifies the beginning of a message definition and must exist,Severity , Facility the declaration is optional.

Messageid=[number|+number]---- message ordinal ID, this must be, but the value is optional. If no value is specified, this value equals the previous Facility plus 1, if a + sign is preceded by the specified value, the previous Facility This value is added to generate MessageId .

Severity=name----A name specified in the head declaration Facilitynames , this declaration is optional. If no value is specified, the value that was last specified in the message definition block is used. The default first message definition is

Facility=application.

Symbolicname=name---- An identifier that can be seen in the *.h file, such as #define name ((type) 0xnnnnnnnn).

Outpubbase={number}---- generate the message constant numeric format, for example , Specify the number of , Generate Ten number of binary.

Language=name----A name specified in the Head declaration Languagenames , this item is optional, and if no value is specified, the last specified value in the message definition block is used. The default first message definition is

Language=english.

message text----messages.

. ( point number )---- message definition Terminator, note: This terminator is entered in English input mode, otherwise use MC The tool will prompt for no terminator when compiling. Examples of message definitions are as follows:

messageid=0x1

Severity=error

Facility=runtime

Symbolicname=msg_bad_command

Language=english

You have the chosen an incorrect command.

.

Language=chinese

You have chosen an abnormal command.

.

You can also use some control characters in the message text definition to view MSDN.

Three*.MCExamples of file definitions

1, file contents, file name MCFILE.MC

;//***** SAMPLE.MC *****;//this is the header section. messageidtypedef=dwordseveritynames= (success=0x0:status_severity_success informational=0x1:status_severity_ Informational warning=0x2:status_severity_warning Error=0x3:status_severity_error) FacilityNames= (System=0x0:FACI Lity_system runtime=0x2:facility_runtime stubs=0x3:facility_stubs Io=0x4:facility_io_error_code) LanguageNames= (En glish=0x409:msg00409) languagenames= (chinese=0x804:msg00804); The following is a message definitions. Messageid=0x1severity=errorfacility=runtimesymbolicname=msg_bad_commandlanguage=englishyou has chosen an Incorrect command. Language=chinese you have chosen an abnormal command. Messageid=0x2severity=warningfacility=iosymbolicname=msg_bad_parm1language=englishcannot reconnect to the server. . Language=chinese cannot connect to the server. Messageid=0x3severity=successfacility=systemsymbolicname=msg_strike_any_keylanguage=englishpress any key to Continue.%0. Language=chinese Press any key to continue .... Messageid=0x4severity=errorfacility=systemSymbolicname=msg_cmd_deletelanguage=englishfile%1 contains%2 which is in error. Language=chinese file%1 contains%2 r corruption: Messageid=0x5severity=informationalfacility=systemsymbolicname=msg_retryslanguage=englishthere have been%1!d! Attempts with%2!d!%% success%! Disconnect from the server and try again later. . Language=chinese Unknown Error! Unable to connect to server, please try again later!.

2. Compile with MC tool

Open the command line interface of Visual Studio , go to the mcfile.mc file directory, compile with command mc-a-a mcfile.mc , as follows:

After the compilation is successful, McFile.h,mcfile.rc,msg00409.bin are generated in the current directory, Msg00804.bin Four files, because the text file supports both English and Chinese, so two *.bin files are generated.

The McFile.h file section is previewed as follows:

SAMPLE.MC *****//this is the header section.//the following be message definitions.////Values is + bit value s layed out as follows:////3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1//1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0//+---+-+-+-----------------------+-------------------------------+//| sev| c|     r|               Facility | Code |//+---+-+-+-----------------------+-------------------------------+////where////sev-is the SE      Verity code////00-success//01-informational//10-warning//11-error//// C-is the Customer code flag////r-is a reserved bit////facility-is the Facility code////code-is The facility ' s status code//////Define the facility codes//#define Facility_system 0x0#define facility_s TUBS 0x3#define facility_runtime 0x2#define facility_io_error_code 0x4////Def INE thE severity codes//#define status_severity_warning 0x2#define status_severity_success 0x0#define status_s Everity_informational 0x1#define status_severity_error 0x3////messageid:msg_bad_command////MessageText:// You are chosen an incorrect command.//#define Msg_bad_command ((DWORD) 0xc0020001l)


3.compile the generated resource file into the DLL ( or exe) module.

Create a win32dll project, add McFile.h and mcfile.rc files, compile, generate the corresponding DLL can be provided to our program for use.

Iv. using your own defined error code, which is the third step to generate the message resource module

#include <iostream> #include <windows.h>using namespace std;//#ifdef _unicode#define COUT wcout#else# Define COUT Cout#endifint Main () {DWORD dwerror = 0;dword Dwlanguageid = Makelangid (lang_neutral, sublang_neutral);//Solve the problem : Wcout output is not displayed in Chinese Cout.imbue (Std::locale ("CHS"); Hlocal lpmsgtextbuf = NULL; COUT << TEXT ("Enter a custom error code (such as: 3221356545 (0xc0020001)):"); while (Cin >> dwerror) {lpmsgtextbuf = NULL; Hmodule Ghresdll = LoadLibrary (TEXT ("WinMsgDll.dll")), if (NULL = = Ghresdll) {COUT << TEXT ("Load message module failed! ") << endl;return-1;} BOOL bOk = FormatMessage (Format_message_from_hmodule | Format_message_ignore_inserts | Format_message_allocate_buffer,ghresdll, Dwerror, Dwlanguageid, (LPTSTR) &lpmsgtextbuf, 0, NULL); if (!bOk) {COUT & lt;< TEXT ("error") << GetLastError () << Endl;} if (NULL! = lpmsgtextbuf) {COUT << (LPTSTR) lpmsgtextbuf << Endl; LocalFree (LPMSGTEXTBUF);} COUT << TEXT ("Enter a custom error code (such as: 3221356545 (0xc0020001)):");} ReTurn 0;} 


V. Corresponding examples Visual Studio 2005 engineering file code download

Project file Download: Create a custom error code DLL using the MC tool

Hill son
Reprint Please indicate the source, thank you. Original address:http://blog.csdn.net/s634772208/article/details/46402677

Add error code to your module _ use the VC's own tool MC to create a resource and add it to a DLL or EXE

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.