错误码

// 01 错误处理.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <windows.h>
#include <Strsafe.h>

void ShowErrorInfo(LPCTSTR lpErrInfo,
    UINT unErrCode,
    UINT unLine = __LINE__) // 示意,不能这么用
{
    LPTSTR lpMsgBuf = nullptr;
    WCHAR  szMessage[128] = { 0 };
    WCHAR  szCaption[32] = { 0 };

    //能够将错误码转换为错误信息

    FormatMessage(0x1300, NULL, unErrCode,
        0x400, (LPTSTR)&lpMsgBuf, 64, NULL);

    StringCchPrintfW(szMessage, 128,
        L"Error_0x%08X:%s", unErrCode, lpMsgBuf);

    StringCchPrintfW(szCaption, 32,
        L"%s (Error Line:%05d)", lpErrInfo, unLine);

    MessageBox(NULL, szMessage, szCaption, MB_OK);
}
int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hFile = CreateFile(NULL, 0, 0, 0, 0, 0, 0);
    if (hFile==INVALID_HANDLE_VALUE)
    {
        ShowErrorInfo(NULL, GetLastError());
    }
    return 0;
}

 

posted @ 2016-03-22 08:06  天还是那么蓝  阅读(169)  评论(0编辑  收藏  举报