发现我挺笨的,一直以来碰到错误都是GetLastError获取错误代码,然后再去找资料区查询错在哪里... 速度慢,效率低... 呵呵,这不,看了下windows核心编程,就简单的实现了一个dll, 直接一个函数void ShowErrorInformation()函数显示错误, 把GetLastEror()都省了,多好, 呵呵
下面是ShowErrorInformation.h文件
#ifdef SHOW_ERROR_INFO
#else
#define SHOW_ERROR_INFO extern "C" __declspec(dllimport)
#endif
SHOW_ERROR_INFO void ShowErrorInformation();
然后是:ShowErrorInformation.cpp文件,关键就是FormatMessage函数, 具体参考msdn吧. 代码就不解释了.
#include <windows.h>
#define SHOW_ERROR_INFO extern "C" __declspec(dllexport)
#include "ShowErrorInformation.h"
#include "resource.h"
LRESULT CALLBACK ShowErrorInformationDlg(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
void ShowErrorInformation()
{
HINSTANCE hInst;
DWORD lastError;
lastError = GetLastError();
hInst = GetModuleHandle("ShowErrorInformation.dll");
DialogBoxParam(hInst, (LPCTSTR)IDD_ERRINFO, GetActiveWindow(), (DLGPROC)ShowErrorInformationDlg, lastError);
}
LRESULT CALLBACK ShowErrorInformationDlg(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DWORD lastError = 0;
HLOCAL hlocal = 0;
char ErrValue[8] = {0};
char *localoffset;
char *ErrMsgOffset = NULL;
int mod = 0;
int div = 0;
char * ErrMsgBuffer = NULL;
DWORD ErrMsgSize = 0;
BOOL fOk;
switch(uMsg)
{
case WM_INITDIALOG:
lastError = (DWORD) lParam;
wsprintf(ErrValue, TEXT("%08X"), lastError);
SetDlgItemText(hDlg, IDC_ERRVALUE, ErrValue);
fOk = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,\
NULL,lastError, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR)&hlocal, 0, NULL);
if (hlocal != NULL)
{
ErrMsgSize = strlen((char *)hlocal);
div = ErrMsgSize / 36;
mod = ErrMsgSize % 36;
if (mod != 0)
{
div++;
}
ErrMsgSize += div * 2;
ErrMsgBuffer = (char *)VirtualAlloc(NULL, ErrMsgSize, MEM_COMMIT, PAGE_READWRITE);
memset(ErrMsgBuffer, 0, ErrMsgSize);
if (ErrMsgBuffer == NULL)
{
MessageBox(hDlg, TEXT("Alloc Memory Error"), TEXT("Alloc Memory"), MB_OK);
EndDialog(hDlg, 0);
return FALSE;
}
//将数据换个格式存储
localoffset = (char *) hlocal;
ErrMsgOffset = (char *) ErrMsgBuffer;
if (div == 1)
{
memcpy(ErrMsgOffset, hlocal, strlen((char *)hlocal));
}
else
{
for (int i = 0; i < div; i++)
{
//这里是显示回车键, 显示的好看些嘛,呵呵
memcpy(ErrMsgOffset, localoffset, 36);
localoffset += 35;
ErrMsgOffset += 35;
*(++ErrMsgOffset) = 0x0d;
*(++ErrMsgOffset) = 0x0a;
}
}
SetDlgItemText(hDlg, IDC_ERRINFORMATION, ErrMsgBuffer);
}
else
{
SetDlgItemText(hDlg, IDC_ERRINFORMATION, ErrMsgBuffer);
}
break;
case WM_COMMAND:
switch(wParam)
{
case IDCANCEL:
if (ErrMsgBuffer != NULL)
{
VirtualFree((PVOID)ErrMsgBuffer, ErrMsgSize, MEM_DECOMMIT);
}
EndDialog(hDlg, wParam);
break;
}
break;
default:
break;
}
return FALSE;
}
然后写了一个简单的exe文件测试了下,感觉挺好,呵呵
以下是text.cpp文件
#include "stdafx.h"
#include <windows.h>
#include "ShowErrorInformation.h"
#pragma comment (lib, "ShowErrorInformation.lib")
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MessageBox(NULL, TEXT("Hello World"), NULL, MB_OK);
ShowErrorInformation();
CreateFile(TEXT("aa.txt"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
ShowErrorInformation();
return 0;
}
ShowErrorInformation.h ShowErrorInformation.lib ShowErrorInformation.dll dllcheck.exe见附件
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!