Windows 核心编程 ErrorShow

 

/*

  由于用原始API编写程序,所以在发送ESM_POKECODEANDLOOKUP 消息时,对话框资源还没有初始化成功

  因此除去对ESM_POKECODEANDLOOKUP 消息的初始化处理部分,程序运行成功。 

*/

 

 

/************************************************************************/

/* Module : ErrorShow.cpp                                                                     */

/************************************************************************/

 

#include <Windows.h>

#include <tchar.h>

#include "resource.h"

 

#define ESM_POKECODEANDLOOKUP   (WM_USER + 100)

const TCHAR g_szAppName[] = _TEXT("Error Show");

 

 

INT_PTR CALLBACK DialogProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)

{

switch(uMsg)

{

case WM_INITDIALOG:

SendMessage(hwnd,WM_SETICON,ICON_SMALL,(LPARAM)

LoadIcon((HINSTANCE)GetWindowLongPtr(hwnd,GWLP_HINSTANCE),

MAKEINTRESOURCE(IDD_ERRORSHOW)));

// Don't accept error codes more than 5 digits long

SendMessage(GetDlgItem(hwnd,IDC_ERRORCODE),EM_LIMITTEXT,(WPARAM)5,0L);

break;

 

case WM_COMMAND:

switch( LOWORD(wParam) )

{

 

case IDCANCEL:

EndDialog(hwnd, uMsg);

break;

 

case IDC_ALWAYSONTOP:

SetWindowPos(hwnd, IsDlgButtonChecked(hwnd, IDC_ALWAYSONTOP) 

? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

break;

 

case IDC_ERRORCODE: 

EnableWindow(GetDlgItem(hwnd, IDOK), GetWindowTextLength(hwnd) > 0);

break;

 

case IDOK:

// Get the error code

DWORD dwError = GetDlgItemInt(hwnd, IDC_ERRORCODE, NULL, FALSE);

 

HLOCAL hlocal = NULL;   // Buffer that gets the error message string

 

// Get the error code's textual description

BOOL fOk = FormatMessage(

FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 

NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), 

(PTSTR) &hlocal, 0, NULL);

 

if (!fOk) 

{

// Is it a network-related error?

HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, 

DONT_RESOLVE_DLL_REFERENCES);

 

if (hDll != NULL) 

{

FormatMessage(

FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,

hDll, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),

(PTSTR) &hlocal, 0, NULL);

FreeLibrary(hDll);

}

}

 

if (hlocal != NULL) 

{

SetDlgItemText(hwnd, IDC_ERRORTEXT, (PCTSTR) LocalLock(hlocal));

LocalFree(hlocal);

}

else 

{

SetDlgItemText(hwnd, IDC_ERRORTEXT, TEXT("Error number not found."));

}

break;

}

break;

}

return false;

}

 

//////////////////////////////////////////////////////////////////////////

 

int WINAPI wWinMain(  HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPWSTR lpCmdLine,

int nShowCmd )

{

HWND hwnd = FindWindow(_TEXT("#32770"),_TEXT("Error Show"));

if (IsWindow(hwnd))

{

SendMessage(hwnd,ESM_POKECODEANDLOOKUP,_ttoi(lpCmdLine),0);

else

{

DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_ERRORSHOW),

NULL,DialogProc,_ttoi(lpCmdLine));

}

return 0;

}

 

 

你可能感兴趣的:(编程,windows,null,System,buffer,winapi)