1. System Message 처리 [Bottom] [Top]
1 void GetSystemMessage( DWORD dwCode ) 2 { 3 // 임시 버퍼 포인터 4 LPTSTR pTemp; 5 6 // 시스템 에러 메세지로 변환 7 DWORD dwResult = FormatMessage( 8 ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM ), 9 NULL, 10 dwCode, 11 MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ), 12 reinterpret_cast< LPTSTR >( &pTemp ), 13 0, 14 NULL ); 15 if( IS_NOT_ZERO( dwResult ) ) 16 { 17 // TODO: 메세지 처리 18 // (pTemp : 시스템 메세지 문자열) 19 // 20 } 21 22 // 임시 메세지 버퍼 제거 23 LocalFree( pTemp ); 24 }
2. System Event Log 처리 [Bottom] [Top]
1 HRESULT SystemLog( LPCTSTR szProgram, LPCTSTR szMesg ) 2 { 3 // 이벤트 로그 열기 4 HANDLE hSysEventSrc = RegisterEventSource( NULL, szProgram ); 5 if( IS_NULL( hSysEventSrc ) ) 6 { 7 return E_FAIL; 8 } 9 10 // 이벤트 로그 기록 11 BOOL bResult = ReportEvent( 12 hSysEventSrc, 13 EVENTLOG_ERROR_TYPE, 14 0, 0, NULL, 1, 0, 15 &szMesg, 16 NULL ); 17 18 // 이벤트 로그 닫기 19 DeregisterEventSource( hSysEventSrc ); 20 21 return ( IS_ZERO( bResult ) ? E_FAIL : S_OK ); 22 }
3. Event Source [Bottom] [Top]
Event Logging http://msdn.microsoft.com/en-us/library/aa363652%28VS.85%29.aspx
Event Source http://msdn.microsoft.com/en-us/library/aa363661%28VS.85%29.aspx
Adding an Event Source to the Registry http://msdn.microsoft.com/en-us/library/aa363634%28VS.85%29.aspx
Message Files http://msdn.microsoft.com/en-us/library/aa363669%28VS.85%29.aspx
3.1. Event Source 등록 처리 [Bottom] [Top]
1 // File: Win32/AddEventSource.cpp 2 3 // AddEventSource.cpp : Adding an Event Source to the Registry. 4 // 5 // Reference 6 // - URL : http://msdn.microsoft.com/en-us/library/aa363634(VS.85).aspx 7 // 8 9 #include <stdafx.h> 10 #include <windows.h> 11 #include <iostream> 12 #include <strsafe.h> 13 14 15 //------------------------------------------------------------------------------ 16 // 함수 원형 선언 17 18 BOOL AddEventSource( 19 LPCTSTR pszLogName, // Application log or a custom log 20 LPCTSTR pszSrcName, // event source name 21 LPCTSTR pszMsgDLL, // path for message DLL 22 DWORD dwNum ); // number of categories 23 24 25 //------------------------------------------------------------------------------ 26 // Main() 함수 구현 27 28 int _tmain( int argc, _TCHAR* argv[] ) 29 { 30 if( FALSE == AddEventSource( 31 _T( "Application" ), 32 _T( "SampleEventSourceName" ), 33 _T( "C:\\WINDOWS\\SYSTEM32\\eventSource.dll" ), 34 1 ) ) 35 { 36 _tprintf( _T( "An Error has Occurred.\n" ) ); 37 return 1; 38 } 39 40 return 0; 41 } 42 43 44 //------------------------------------------------------------------------------ 45 // 함수 구현 46 47 BOOL AddEventSource( 48 LPCTSTR pszLogName, // Application log or a custom log 49 LPCTSTR pszSrcName, // event source name 50 LPCTSTR pszMsgDLL, // path for message DLL 51 DWORD dwNum ) // number of categories 52 { 53 HKEY hk; 54 DWORD dwData, dwDisp; 55 TCHAR szBuf[ MAX_PATH ]; 56 size_t cchSize = MAX_PATH; 57 58 //-------------------------------------------------------------------------- 59 // Create the event source as a subkey of the log. 60 61 HRESULT hr = StringCchPrintf( szBuf, cchSize, 62 _T( "SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s" ), 63 logName, sourceName ); 64 65 if( RegCreateKeyEx( HKEY_LOCAL_MACHINE, szBuf, 66 0, NULL, REG_OPTION_NON_VOLATILE, 67 KEY_WRITE, NULL, &hk, &dwDisp ) ) 68 { 69 _tprintf( _T( "Could not create the registry key." ) ); 70 return FALSE; 71 } 72 73 //-------------------------------------------------------------------------- 74 // Set the name of the message file. 75 76 if( RegSetValueEx( hk, // subkey handle 77 _T( "EventMessageFile" ), // value name 78 0, // must be zero 79 REG_EXPAND_SZ, // value type 80 (LPBYTE)dllName, // pointer to value data 81 (DWORD)( lstrlen( dllName ) + 1 ) * sizeof( TCHAR ) ) ) // data size 82 { 83 _tprintf( _T( "Could not set the event message file." ) ); 84 RegCloseKey( hk ); 85 return FALSE; 86 } 87 88 //-------------------------------------------------------------------------- 89 // Set the supported event types. 90 91 dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | 92 EVENTLOG_INFORMATION_TYPE; 93 94 if( RegSetValueEx( hk, // subkey handle 95 _T( "TypesSupported" ), // value name 96 0, // must be zero 97 REG_DWORD, // value type 98 (LPBYTE)&dwData, // pointer to value data 99 sizeof( DWORD ) ) ) // length of value data 100 { 101 _tprintf( _T( "Could not set the supported types." ) ); 102 RegCloseKey( hk ); 103 return FALSE; 104 } 105 106 //-------------------------------------------------------------------------- 107 // Set the category message file and number of categories. 108 109 if( RegSetValueEx( hk, // subkey handle 110 _T( "CategoryMessageFile" ), // value name 111 0, // must be zero 112 REG_EXPAND_SZ, // value type 113 (LPBYTE)dllName, // pointer to value data 114 (DWORD)( lstrlen( dllName ) + 1 ) * sizeof( TCHAR ) ) ) // data size 115 { 116 _tprintf( _T( "Could not set the category message file." ) ); 117 RegCloseKey( hk ); 118 return FALSE; 119 } 120 121 if( RegSetValueEx( hk, // subkey handle 122 _T( "CategoryCount" ), // value name 123 0, // must be zero 124 REG_DWORD, // value type 125 (LPBYTE)&dwCategoryNum, // pointer to value data 126 sizeof( DWORD ) ) ) // length of value data 127 { 128 _tprintf( _T( "Could not set the category count." ) ); 129 RegCloseKey( hk ); 130 return FALSE; 131 } 132 133 //-------------------------------------------------------------------------- 134 135 RegCloseKey( hk ); 136 return TRUE; 137 }
