《windows核心编程》笔记(一)

1,windows对程序错误的处理,

None.gif int APIENTRY_tWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPTSTRlpCmdLine, int nCmdShow)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifLPVOIDlpMsgBuf;
InBlock.gifHANDLEhFile
=::CreateFile(_T("C://12.txt"),0,0,NULL,OPEN_EXISTING,0,NULL);//打开文件
InBlock.gif
if(INVALID_HANDLE_VALUE==hFile)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//文件不存在
InBlock.gif
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0,(LPTSTR)&lpMsgBuf,0,NULL);
InBlock.gifMessageBox(NULL,(LPCWSTR)lpMsgBuf,_T(
"错误信息"),MB_OK|MB_ICONINFORMATION);
InBlock.gifLocalFree(lpMsgBuf);
ExpandedSubBlockEnd.gif}

InBlock.gif
return0;
ExpandedBlockEnd.gif}

None.gif

调试时,在Watch窗口输入“@err,hr”,就可以显示线程的最后错误代码的号码和该错误的描述信息。
2008060701.jpg

VC6.0还带了一个实用小程序,可以用来将错误代码转换为相应的文本描述信息。
2008060702.jpg<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

书中给出了一个模仿VC6自带的那个Error Lookup实用程序的示例:

None.gif #define ESM_POKECODEANDLOOKUP(WM_USER+100) // 用户自定义消息
None.gif
const TCHARg_szAppName[] = TEXT( " ErrorShow " );
None.gif
None.gifBOOLDlg_OnInitDialog(HWNDhwnd,HWNDhwndFocus,LPARAMlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gifchSETDLGICONS(hwnd,IDI_ERRORSHOW);
//设置图标
InBlock.gif
InBlock.gif
//Don'taccepterrorcodesmorethan5digitslong
InBlock.gif
Edit_LimitText(GetDlgItem(hwnd,IDC_ERRORCODE),5);//限制输入字符长度最大为
InBlock.gif
InBlock.gif
//Lookupthecommand-linepassederrornumber
InBlock.gif
SendMessage(hwnd,ESM_POKECODEANDLOOKUP,lParam,0);//发送初始错误代码消息
InBlock.gif
return(TRUE);
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
void Dlg_OnCommand(HWNDhwnd, int id,HWNDhwndCtl,UINTcodeNotify) dot.gif {
InBlock.gif
InBlock.gif
switch(id)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
caseIDCANCEL:
InBlock.gifEndDialog(hwnd,id);
//关闭对话框窗口
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseIDC_ALWAYSONTOP://始终在最前
InBlock.gif
SetWindowPos(hwnd,IsDlgButtonChecked(hwnd,IDC_ALWAYSONTOP)?HWND_TOPMOST:HWND_NOTOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseIDC_ERRORCODE:
InBlock.gifEnableWindow(GetDlgItem(hwnd,IDOK),Edit_GetTextLength(hwndCtl)
>0);//设置“lookup"按钮是否enable
InBlock.gif
break;
InBlock.gif
InBlock.gif
caseIDOK:
InBlock.gif
//Gettheerrorcode
InBlock.gif
DWORDdwError=GetDlgItemInt(hwnd,IDC_ERRORCODE,NULL,FALSE);//获取错误代码号码
InBlock.gif

InBlock.gifHLOCALhlocal
=NULL;//Bufferthatgetstheerrormessagestring
InBlock.gif
InBlock.gif
//UsethedefaultsystemlocalesincewelookforWindowsmessages.
InBlock.gif
//Note:thisMAKELANGIDcombinationhas0asvalue
InBlock.gif
DWORDsystemLocale=MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL);//本地语言
InBlock.gif
InBlock.gif
//Gettheerrorcode'stextualdescription
InBlock.gif
BOOLfOk=FormatMessage(
InBlock.gifFORMAT_MESSAGE_FROM_SYSTEM
|FORMAT_MESSAGE_IGNORE_INSERTS|
InBlock.gifFORMAT_MESSAGE_ALLOCATE_BUFFER,
InBlock.gifNULL,dwError,systemLocale,
InBlock.gif(PTSTR)
&hlocal,0,NULL);
InBlock.gif
InBlock.gif
if(!fOk)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//Isitanetwork-relatederror?
InBlock.gif
HMODULEhDll=LoadLibraryEx(TEXT("netmsg.dll"),NULL,
InBlock.gifDONT_RESOLVE_DLL_REFERENCES);
InBlock.gif
InBlock.gif
if(hDll!=NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffOk
=FormatMessage(
InBlock.gifFORMAT_MESSAGE_FROM_HMODULE
|FORMAT_MESSAGE_IGNORE_INSERTS|
InBlock.gifFORMAT_MESSAGE_ALLOCATE_BUFFER,
InBlock.gifhDll,dwError,systemLocale,
InBlock.gif(PTSTR)
&hlocal,0,NULL);
InBlock.gifFreeLibrary(hDll);
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(fOk&&(hlocal!=NULL))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSetDlgItemText(hwnd,IDC_ERRORTEXT,(PCTSTR)LocalLock(hlocal));
InBlock.gifLocalFree(hlocal);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSetDlgItemText(hwnd,IDC_ERRORTEXT,TEXT(
"Notextfoundforthiserrornumber."));
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gifINT_PTRWINAPIDlg_Proc(HWNDhwnd,UINTuMsg,WPARAMwParam,LPARAMlParam)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gif
switch(uMsg)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifchHANDLE_DLGMSG(hwnd,WM_INITDIALOG,Dlg_OnInitDialog);
//初始化处理
InBlock.gif
chHANDLE_DLGMSG(hwnd,WM_COMMAND,Dlg_OnCommand);//命令处理
InBlock.gif

InBlock.gif
caseESM_POKECODEANDLOOKUP:
InBlock.gifSetDlgItemInt(hwnd,IDC_ERRORCODE,(UINT)wParam,FALSE);
//控件上设置错误代码
InBlock.gif
FORWARD_WM_COMMAND(hwnd,IDOK,GetDlgItem(hwnd,IDOK),BN_CLICKED,PostMessage);//模拟按钮点击事件
InBlock.gif
SetForegroundWindow(hwnd);//设置为最前的窗口
InBlock.gif
break;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return(FALSE);
ExpandedBlockEnd.gif}

None.gif
None.gif
int WINAPI_tWinMain(HINSTANCEhinstExe,HINSTANCE,PTSTRpszCmdLine, int )
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
InBlock.gifHWNDhwnd
=FindWindow(TEXT("#32770"),TEXT("ErrorShow"));//寻址窗口
InBlock.gif
if(IsWindow(hwnd))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//已经存在
InBlock.gif
//Aninstanceisalreadyrunning,activateitandsenditthenew#
InBlock.gif
SendMessage(hwnd,ESM_POKECODEANDLOOKUP,_ttoi(pszCmdLine),0);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//新窗口
InBlock.gif
DialogBoxParam(hinstExe,MAKEINTRESOURCE(IDD_ERRORSHOW),NULL,Dlg_Proc,_ttoi(pszCmdLine));
ExpandedSubBlockEnd.gif}

InBlock.gif
return(0);
ExpandedBlockEnd.gif}

None.gif

这里用到了几个宏定义,下面这个是用来指定消息处理函数的

None.gif // ThenormalHANDLE_MSGmacroinWindowsX.hdoesnotworkproperlyfordialog
None.gif
// boxesbecauseDlgProcreturnsaBOOLinsteadofanLRESULT(like
None.gif
// WndProcs).ThischHANDLE_DLGMSGmacrocorrectstheproblem:
None.gif
#define chHANDLE_DLGMSG(hWnd,message,fn)/
None.gif
case (message): return (SetDlgMsgResult(hWnd,uMsg,/
None.gifHANDLE_##message((hWnd),(wParam),(lParam),(fn))))
None.gif

下面这个是用来为窗口设置大/小图标

None.gif // Setsthedialogboxicons
None.gif
inline void chSETDLGICONS(HWNDhWnd, int idi)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifSendMessage(hWnd,WM_SETICON,ICON_BIG,(LPARAM)
InBlock.gifLoadIcon((HINSTANCE)GetWindowLongPtr(hWnd,GWLP_HINSTANCE),
InBlock.gifMAKEINTRESOURCE(idi)));
InBlock.gifSendMessage(hWnd,WM_SETICON,ICON_SMALL,(LPARAM)
InBlock.gifLoadIcon((HINSTANCE)GetWindowLongPtr(hWnd,GWLP_HINSTANCE),
InBlock.gifMAKEINTRESOURCE(idi)));
ExpandedBlockEnd.gif}

None.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值