
自定Event並寫入Event Log中的步驟:
一.建立Message Text File (.mc)。
Message file 中定義了event的嚴重性(severity)、產生這個信息的組件名字(facility)及語言等。主要分成兩個部分:Header section 與 Message Definitions。
1.Header Section
用來定義Message Definitions會用到的息訊名稱和語言等關鍵詞。
| Statement syntax | Description | ||||||||
| MessageIdTypedef = type |
| ||||||||
| SeverityNames=(name=number[:name]) |
| ||||||||
| Facility=(name=number[:name]) |
| ||||||||
| LanguageNames=(name=number:filename) |
|
2.Message Definitions
每個Message Text File 可以包含一個或多個 Message definitions。
| Statement syntax | Description | ||||
| MessageID=[number|+number] | 分配給我個信息的16-bit值 | ||||
| Serverity=name |
| ||||
| Facility=name |
| ||||
| SymbolicName=name |
| ||||
| Language=name |
| ||||
| message text | 此訊息的文字 | ||||
| . | 結束字符 |
在訊息編譯器中,任何空格和換行都會被自動忽略。
%n[!format_specifier!]
| 轉字符 | 定義 |
| %b | 空格 |
| %t | 內縮 |
| %r%n | 換行 |
| %1~%99 | 插入字串符 |
;// header section
MessageIdTypedef=DWORD
SeverityNames=(
Informational=0x1:STATUS_SEVERITY_INFORMATIONAL
Warning=0x2:STATUS_SEVERITY_WARNING
Error=0x3:STATUS_SEVERITY_ERROR
)
FacilityNames=(
System=0x0:FACILITY_SYSTEM
Runtime=0x2:FACILITY_RUNTIME
Stubs=0x3:FACILITY_STUBS
Io=0x4:FACILITY_IO_ERROR_CODE
)
LanguageNames=(English=0x409:MSG00409)
MessageId=0x2
Severity=Error
Facility=Runtime
SymbolicName=MSG_ERROR
Language=English
Error:%b%1
.
MessageId=0x3
Severity=Warning
Facility=Runtime
SymbolicName=MSG_WARNING
Language=English
Warning:%b%1
.
MessageId=0x4
Severity=Informational
Facility=Runtime
SymbolicName=MSG_INFORMATION
Language=English
Information:%b%1
.
如果要輸入註解可以用:
;// This is a single-line comment.或
;/* This is a block comment.
; It spans multiple lines.
;*/二.用VS或Platform SDK的編譯器 Compile 此 message file。
mc -U messageFileName.mc編譯完後會產生一個 .h , 一個 .rc 和 .bin 檔案。
| 參數 | 定義 |
| -c | 在所有的訊息代碼中設罝Customer位 |
| -d | 在.h文件中使用Facility和嚴重級別代碼的十進位定義 |
| -o | 產生OLE2 .h文件 |
| -s | 插入符號作為每個訊息的第一行 |
| -v | 產生詳細的輸出 |
| -h pathname | 產生.h的路徑 |
| -e extension | 生成.h文件的1~3擴展名 |
| -r pathname | 生成RC和Bin文件路徑 |
| -x pathname | 生成的調試文件路徑 |
| -u | 定義輸入文件為Unicode文件 |
| -U | 定義訊息內容為二進位,而輸出的二進位文件為Unicode |
| filename | 編譯的Message Text File名稱 |
產生的.h Sample片段
//
// Define the facility codes
//
#define FACILITY_SYSTEM 0x0
#define FACILITY_STUBS 0x3
#define FACILITY_RUNTIME 0x2
#define FACILITY_IO_ERROR_CODE 0x4
//
// Define the severity codes
//
#define STATUS_SEVERITY_WARNING 0x2
#define STATUS_SEVERITY_INFORMATIONAL 0x1
#define STATUS_SEVERITY_ERROR 0x3
//
// MessageId: MSG_ERROR
//
// MessageText:
//
// Error:%b%1
//
#define MSG_ERROR ((DWORD)0xC0050002L)
//
// MessageId: MSG_WARNING
//
// MessageText:
//
// Warning:%b%1
//
#define MSG_WARNING ((DWORD)0x80050003L)
//
// MessageId: MSG_INFORMATION
//
// MessageText:
//
// Information:%b%1
//
#define MSG_INFORMATION ((DWORD)0x40050004L)
沒有留言:
張貼留言