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

MSMQ错误信息获取
本文介绍了一种通过FormatMessage API调用从MQUTIL.DLL模块中检索Microsoft消息队列错误代码说明字符串的方法,并提供了一个示例代码,展示了如何打印错误消息。
部署运行你感兴趣的模型镜像

来源:http://support.microsoft.com/kb/256348/zh-cn

 注意:这篇文章是由无人工介入的自动的机器翻译系统翻译完成。这些文章是微软为不懂英语的用户提供的, 以使他们能够理解这些文章的内容。微软不保证机器翻译的正确度,也不对由于内容的误译或者客户对它的使用所引起的任何直接的, 或间接的可能的问题负责。

概要

FormatMessage() API 调用可用于获取与 Microsoft 消息队列错误代码说明字符串。 FormatMessage 调用的 Mqutil.dll 模块句柄和用 FORMAT_MESSAGE_FROM_HMODULE 标志来检索消息文本

更多信息

以下示例代码显示简单函数将其打印描述 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(
" ERRORMESSAGE: %s "), (LPTSTR)pBuffer );
}

else
{
    _tprintf( _TEXT(
" ERRORNUMBER: %d "), dwErrorMsgId );
}



// Free the buffer.
LocalFree( pBuffer );

}

您可能感兴趣的与本文相关的镜像

Qwen-Image

Qwen-Image

图片生成
Qwen

Qwen-Image是阿里云通义千问团队于2025年8月发布的亿参数图像生成基础模型,其最大亮点是强大的复杂文本渲染和精确图像编辑能力,能够生成包含多行、段落级中英文文本的高保真图像

这个是这个页面视图控制器里的部分代码,是什么呢? extension OnlineDeviceListViewController: UICollectionViewDelegateFlowLayout { //MARK: D - UICollectionViewDelegateFlowLayout func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { if displayMode == .row { return (collectionViewLayout as! UICollectionViewFlowLayout).itemSize } let height = (collectionView.bounds.width - 32) * 9 / 16 + 16 return CGSize(width: collectionView.bounds.width, height: height) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { let device = self.device(at: section) if displayMode == .row { var size = (collectionViewLayout as! UICollectionViewFlowLayout).headerReferenceSize size.height += device.isFirstDeviceInSite ? OnlineDeviceListHeader.siteNameViewHeight : 0 return size } if !isFolded(at: section) { return CGSize(width: collectionView.bounds.width, height: 60.0 + (device.isFirstDeviceInSite ? OnlineDeviceListHeader.siteNameViewHeight : 0)) } let channelCount = device.channelsInfo.count let numberOfLines = (device.online && channelCount == 2) ? 1 : 2 var height = 0.0 if device.displayType == .solar { if device.displayOnline { height = 60.0 + 76.0 + (device.isFirstDeviceInSite ? OnlineDeviceListHeader.siteNameViewHeight : 0) + (displayMode == .card ? getMaxTitleHeightOffset(width: (collectionView.bounds.width - 32) / 2) : 0) height += displayMode == .card ? getMaxStatusHeightOffset(width: (collectionView.bounds.width - 32) / 2 - 24 - 16 - 4, status: SolarEnergyCardContentView.getTextOfSolarStatus(systemStatus: device.solarSystemStatus)) : 0 } else { height = 60.0 + (device.isFirstDeviceInSite ? OnlineDeviceListHeader.siteNameViewHeight : 0) } } else { if (device.online && channelCount <= 2 && channelCount > 0 && device.displayType == .NVR) { let numberOfLines = 1; height = (collectionView.bounds.width - 32) * 9 / 32 * CGFloat(numberOfLines) + 76 + CGFloat(numberOfLines - 1) + (device.isFirstDeviceInSite ? OnlineDeviceListHeader.siteNameViewHeight : 0) } else { height = (collectionView.bounds.width - 32) * 9 / 32 * CGFloat(numberOfLines) + 76 + CGFloat(numberOfLines - 1) + (device.isFirstDeviceInSite ? OnlineDeviceListHeader.siteNameViewHeight : 0) } } return CGSize(width: collectionView.bounds.width, height: height) } func getMaxTitleHeightOffset(width: CGFloat) -> CGFloat { let text1 = LocalizedString(key: solar_DeviceList_Remaining_Power) let text2 = LocalizedString(key: solar_DeviceList_Device_Status) let font = UIFont.projectFont(ofSize: 14) let defaultH = getLabelHeightByString(string: LocalizedString(key: commonYes), font: font, width: width) //搞个单行文案算默认高度 return max(getLabelHeightByString(string: text1, font: font, width: width), getLabelHeightByString(string: text2, font: font, width: width)) - defaultH //初始化布局有单行高度,此处仅偏移超过单行高度的部分 } func getMaxStatusHeightOffset(width: CGFloat, status: String) -> CGFloat { let font = UIFont.mediumProjectFont(ofSize: 16) let defaultH = getLabelHeightByString(string: LocalizedString(key: commonYes), font: font, width: width) //搞个单行文案算默认高度 let height = getLabelHeightByString(string: status, font: font, width: width) return height - defaultH //初始化布局有单行高度,此处仅偏移超过单行高度的部分 } func getLabelHeightByString(string: String, font: UIFont, width: CGFloat) -> CGFloat { var labelHeight:CGFloat = 0.0 let attributes = [NSAttributedString.Key.font: font] labelHeight += NSString(string: string).boundingRect(with: CGSize(width: width, height: 100), options: .usesLineFragmentOrigin, attributes: attributes as [NSAttributedString.Key : Any], context: nil).height return labelHeight } }
最新发布
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值