• 如何获取错误消息说明使用 FormatMessage API


    FormatMessage() API 调用可能用于获取与 Microsoft 消息队列的错误代码相关联的描述字符串。 FORMAT_MESSAGE_FROM_HMODULE 标志与 FormatMessage 呼叫和 $ 的 Mqutil.dll 模块句柄用于检索消息文本。

    更多信息

    下面的代码示例显示了一个简单的函数,将打印到标准输出 Microsoft 消息队列错误消息的说明:
    #include <windows.h>
    #include <lmerr.h>
    #include <tchar.h>
    
    #define ERRMSGBUFFERSIZE 256
    
    void fnDisplayError( DWORD dwErrorMsgId )
    {
    DWORD ret;        // Temp space to hold a return value.
    HINSTANCE hInst;  // Instance handle for DLL.
    HLOCAL pBuffer;   // Buffer to hold the textual error description.
    
    if ( HRESULT_FACILITY(dwErrorMsgId) == FACILITY_MSMQ )
    { // MSMQ errors only (see winerror.h for facility info).
    // Load the MSMQ library containing the error message strings.
    	hInst = LoadLibrary( TEXT("MQUTIL.DLL") );
    	if(hInst != 0)
    	{ // hInst not NULL if the library was successfully loaded.
    // Get the text string for a message definition
    		ret = FormatMessage( 
    				FORMAT_MESSAGE_ALLOCATE_BUFFER | // Function will handle memory allocation.
    				FORMAT_MESSAGE_FROM_HMODULE | // Using a module's message table.
    				FORMAT_MESSAGE_IGNORE_INSERTS, 
    				hInst, // Handle to the DLL.
    				dwErrorMsgId, // Message identifier.
    				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
    				(LPTSTR)&pBuffer, // Buffer that will hold the text string.
     				ERRMSGBUFFERSIZE, // Allocate at least this many chars for pBuffer.
    				NULL // No insert values.
    			        );
    	} // hInst not NULL if the library was successfully loaded.
    
    } // MSMQ errors only.
    
    else if ( dwErrorMsgId >= NERR_BASE && dwErrorMsgId <= MAX_NERR )
    { // Could be a network error.
    // Load the library containing network messages.
    	hInst = LoadLibrary( TEXT("NETMSG.DLL") );
    	if(hInst != 0)
    	{ // Not NULL if successfully loaded.
    // Get a text string for the message definition.
    		ret = FormatMessage(  
    				FORMAT_MESSAGE_ALLOCATE_BUFFER | // The function will allocate memory for the message.
    				FORMAT_MESSAGE_FROM_HMODULE | // Message definition is in a module.
    				FORMAT_MESSAGE_IGNORE_INSERTS,  // No inserts used.
    				hInst, // Handle to the module containing the definition.
    				dwErrorMsgId, // Message identifier.
    				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
    				(LPTSTR)&pBuffer, // Buffer to hold the text string.
    				ERRMSGBUFFERSIZE, // Smallest size that will be allocated for pBuffer.
    				NULL // No inserts.
    			);
    	} // Not NULL if successfully loaded.
    
    } // Could be a network error.
    else
    { // Unknown message source.
    // Get the message string from the system.
    	ret = FormatMessage(  
    			FORMAT_MESSAGE_ALLOCATE_BUFFER | // The function will allocate space for pBuffer.
    			FORMAT_MESSAGE_FROM_SYSTEM | // System wide message.
    			FORMAT_MESSAGE_IGNORE_INSERTS, // No inserts.
    			NULL, // Message is not in a module.
    			dwErrorMsgId, // Message identifier.
    			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language.
    			(LPTSTR)&pBuffer, // Buffer to hold the text string.
    			ERRMSGBUFFERSIZE, // The function will allocate at least this much for pBuffer.
    			NULL // No inserts.
    		);
    }
    
    
    // Display the string.
    
    if( ret )
    {
    	_tprintf( _TEXT("\tERRORMESSAGE: %s\n"), (LPTSTR)pBuffer );
    }
    else
    {
    	_tprintf( _TEXT("\tERRORNUMBER: %d\n"), dwErrorMsgId );
    }
    
    
    // Free the buffer.
    LocalFree( pBuffer );
    
    }
    			
  • 相关阅读:
    正则学习笔记 主要是C#或Javascript
    禁止页面复制、保存等常用js命令
    Webkit几个CSS效果和JS控制
    marquee用法的详细解释让文字动起来
    js比较两个时间的大小
    关于System.MissingMethodException
    Windows mobile XmlTextWriter FileStream 路径问题
    jQuery UI插件sortable中文帮助文档
    asp.net中用Eval绑定时间的格式
    jQuery图表插件Flot
  • 原文地址:https://www.cnblogs.com/chenhs/p/1779260.html
Copyright © 2020-2023  润新知