X-Git-Url: http://git.iain.cx/?p=nssm.git;a=blobdiff_plain;f=event.cpp;h=b44482c2ab2fc2fdcccfcf05f7a0efe426fed394;hp=6744da2fc895e019e6f5616f83df7bc4279b1767;hb=HEAD;hpb=f600a48847de75cbbe5dac8478b47e91b977f931 diff --git a/event.cpp b/event.cpp index 6744da2..b44482c 100644 --- a/event.cpp +++ b/event.cpp @@ -1,5 +1,6 @@ #include "nssm.h" +#define NSSM_SOURCE _T("nssm") #define NSSM_ERROR_BUFSIZE 65535 #define NSSM_NUM_EVENT_STRINGS 16 unsigned long tls_index; @@ -14,8 +15,10 @@ TCHAR *error_string(unsigned long error) { TlsSetValue(tls_index, (void *) error_message); } - if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) { - if (_sntprintf_s(error_message, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0; + if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, GetUserDefaultLangID(), (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) { + if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) { + if (_sntprintf_s(error_message, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0; + } } return error_message; } @@ -23,9 +26,11 @@ TCHAR *error_string(unsigned long error) { /* Convert message code to format string */ TCHAR *message_string(unsigned long error) { TCHAR *ret; - if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) { - ret = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, 32 * sizeof(TCHAR)); - if (_sntprintf_s(ret, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0; + if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, GetUserDefaultLangID(), (LPTSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) { + if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, (LPTSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) { + ret = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, 32 * sizeof(TCHAR)); + if (_sntprintf_s(ret, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0; + } } return ret; } @@ -38,7 +43,7 @@ void log_event(unsigned short type, unsigned long id, ...) { TCHAR *strings[NSSM_NUM_EVENT_STRINGS]; /* Open event log */ - HANDLE handle = RegisterEventSource(0, NSSM); + HANDLE handle = RegisterEventSource(0, NSSM_SOURCE); if (! handle) return; /* Log it */ @@ -68,7 +73,7 @@ void print_message(FILE *file, unsigned long id, ...) { } /* Show a GUI dialogue */ -int popup_message(unsigned int type, unsigned long id, ...) { +int popup_message(HWND owner, unsigned int type, unsigned long id, ...) { va_list arg; TCHAR *format = message_string(id); @@ -76,16 +81,29 @@ int popup_message(unsigned int type, unsigned long id, ...) { return MessageBox(0, _T("The message which was supposed to go here is missing!"), NSSM, MB_OK | MB_ICONEXCLAMATION); } - TCHAR blurb[512]; + TCHAR blurb[NSSM_ERROR_BUFSIZE]; va_start(arg, id); if (_vsntprintf_s(blurb, _countof(blurb), _TRUNCATE, format, arg) < 0) { va_end(arg); LocalFree(format); - return MessageBox(0, _T("the message which was supposed to go here is too big!"), NSSM, MB_OK | MB_ICONEXCLAMATION); + return MessageBox(0, _T("The message which was supposed to go here is too big!"), NSSM, MB_OK | MB_ICONEXCLAMATION); } va_end(arg); - int ret = MessageBox(0, blurb, NSSM, type); + MSGBOXPARAMS params; + ZeroMemory(¶ms, sizeof(params)); + params.cbSize = sizeof(params); + params.hInstance = GetModuleHandle(0); + params.hwndOwner = owner; + params.lpszText = blurb; + params.lpszCaption = NSSM; + params.dwStyle = type; + if (type == MB_OK) { + params.dwStyle |= MB_USERICON; + params.lpszIcon = MAKEINTRESOURCE(IDI_NSSM); + } + + int ret = MessageBoxIndirect(¶ms); LocalFree(format);