Browse Source

Fixed loading localised resources.

We weren't loading the correct resources for non-English languages when
compiled as a Unicode application.

We now explicitly call FindResourceEx(), LoadResource() and
CreateDialogIndirectParam() to ensure the appropriate windows are shown.

We now explicitly pass GetUserDefaultLangID() to FormatMessage(),
falling back to language neutral if it fails.

Resources and messages are now explicitly UTF-16LE.  That means that git
will treat them as binary and refuse to diff them.  To work around that,
add the following to .git/info/attributes:

    *.mc diff=utf16
    *.rc diff=utf16

and the following to .git/config:

    [diff "utf16"]
    	textconv = "iconv -f utf-16le -t utf-8"
Iain Patterson 9 years ago
parent
commit
7aa403b0a0
5 changed files with 42 additions and 19 deletions
  1. 9 5
      event.cpp
  2. 29 10
      gui.cpp
  3. BIN
      messages.mc
  4. BIN
      nssm.rc
  5. 4 4
      nssm.vcproj

+ 9 - 5
event.cpp

@@ -14,8 +14,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 +25,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;
 }

+ 29 - 10
gui.cpp

@@ -4,9 +4,28 @@ static enum { NSSM_TAB_APPLICATION, NSSM_TAB_DETAILS, NSSM_TAB_LOGON, NSSM_TAB_S
 static HWND tablist[NSSM_NUM_TABS];
 static int selected_tab;
 
+static HWND dialog(const TCHAR *templ, HWND parent, DLGPROC function, LPARAM l) {
+  /* The caller will deal with GetLastError()... */
+  HRSRC resource = FindResourceEx(0, RT_DIALOG, templ, GetUserDefaultLangID());
+  if (! resource) {
+    if (GetLastError() != ERROR_RESOURCE_LANG_NOT_FOUND) return 0;
+    resource = FindResourceEx(0, RT_DIALOG, templ, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));
+    if (! resource) return 0;
+  }
+
+  HGLOBAL ret = LoadResource(0, resource);
+  if (! ret) return 0;
+
+  return CreateDialogIndirectParam(0, (DLGTEMPLATE *) ret, parent, function, l);
+}
+
+static HWND dialog(const TCHAR *templ, HWND parent, DLGPROC function) {
+  return dialog(templ, parent, function, 0);
+}
+
 int nssm_gui(int resource, nssm_service_t *service) {
   /* Create window */
-  HWND dlg = CreateDialogParam(0, MAKEINTRESOURCE(resource), 0, nssm_dlg, (LPARAM) service);
+  HWND dlg = dialog(MAKEINTRESOURCE(resource), 0, nssm_dlg, (LPARAM) service);
   if (! dlg) {
     popup_message(MB_OK, NSSM_GUI_CREATEDIALOG_FAILED, error_string(GetLastError()));
     return 1;
@@ -837,18 +856,18 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       tab.cchTextMax = (int) _tcslen(tab.pszText);
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_APPLICATION, (LPARAM) &tab);
       if (service->native) {
-        tablist[NSSM_TAB_APPLICATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_NATIVE), window, tab_dlg);
+        tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_NATIVE), window, tab_dlg);
         EnableWindow(tablist[NSSM_TAB_APPLICATION], 0);
         EnableWindow(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_PATH), 0);
       }
-      else tablist[NSSM_TAB_APPLICATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);
+      else tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);
       ShowWindow(tablist[NSSM_TAB_APPLICATION], SW_SHOW);
 
       /* Details tab. */
       tab.pszText = message_string(NSSM_GUI_TAB_DETAILS);
       tab.cchTextMax = (int) _tcslen(tab.pszText);
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_DETAILS, (LPARAM) &tab);
-      tablist[NSSM_TAB_DETAILS] = CreateDialog(0, MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);
+      tablist[NSSM_TAB_DETAILS] = dialog(MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);
       ShowWindow(tablist[NSSM_TAB_DETAILS], SW_HIDE);
 
       /* Set defaults. */
@@ -863,7 +882,7 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       tab.pszText = message_string(NSSM_GUI_TAB_LOGON);
       tab.cchTextMax = (int) _tcslen(tab.pszText);
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_LOGON, (LPARAM) &tab);
-      tablist[NSSM_TAB_LOGON] = CreateDialog(0, MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg);
+      tablist[NSSM_TAB_LOGON] = dialog(MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg);
       ShowWindow(tablist[NSSM_TAB_LOGON], SW_HIDE);
 
       /* Set defaults. */
@@ -877,7 +896,7 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       tab.pszText = message_string(NSSM_GUI_TAB_SHUTDOWN);
       tab.cchTextMax = (int) _tcslen(tab.pszText);
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_SHUTDOWN, (LPARAM) &tab);
-      tablist[NSSM_TAB_SHUTDOWN] = CreateDialog(0, MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);
+      tablist[NSSM_TAB_SHUTDOWN] = dialog(MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);
       ShowWindow(tablist[NSSM_TAB_SHUTDOWN], SW_HIDE);
 
       /* Set defaults. */
@@ -893,7 +912,7 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       tab.pszText = message_string(NSSM_GUI_TAB_EXIT);
       tab.cchTextMax = (int) _tcslen(tab.pszText);
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_EXIT, (LPARAM) &tab);
-      tablist[NSSM_TAB_EXIT] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);
+      tablist[NSSM_TAB_EXIT] = dialog(MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);
       ShowWindow(tablist[NSSM_TAB_EXIT], SW_HIDE);
 
       /* Set defaults. */
@@ -909,14 +928,14 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       tab.pszText = message_string(NSSM_GUI_TAB_IO);
       tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_IO, (LPARAM) &tab);
-      tablist[NSSM_TAB_IO] = CreateDialog(0, MAKEINTRESOURCE(IDD_IO), window, tab_dlg);
+      tablist[NSSM_TAB_IO] = dialog(MAKEINTRESOURCE(IDD_IO), window, tab_dlg);
       ShowWindow(tablist[NSSM_TAB_IO], SW_HIDE);
 
       /* Rotation tab. */
       tab.pszText = message_string(NSSM_GUI_TAB_ROTATION);
       tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ROTATION, (LPARAM) &tab);
-      tablist[NSSM_TAB_ROTATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);
+      tablist[NSSM_TAB_ROTATION] = dialog(MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);
       ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);
 
       /* Set defaults. */
@@ -928,7 +947,7 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       tab.pszText = message_string(NSSM_GUI_TAB_ENVIRONMENT);
       tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ENVIRONMENT, (LPARAM) &tab);
-      tablist[NSSM_TAB_ENVIRONMENT] = CreateDialog(0, MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);
+      tablist[NSSM_TAB_ENVIRONMENT] = dialog(MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);
       ShowWindow(tablist[NSSM_TAB_ENVIRONMENT], SW_HIDE);
 
       return 1;

BIN
messages.mc


BIN
nssm.rc


+ 4 - 4
nssm.vcproj

@@ -718,7 +718,7 @@
 				<Tool
 					Name="VCCustomBuildTool"
 					Description="Compiling messages"
-					CommandLine="mc -A $(InputName).mc -r . -h ."
+					CommandLine="mc -u -U $(InputName).mc -r . -h ."
 					Outputs="$(InputName).rc;$(InputName).h"
 				/>
 			</FileConfiguration>
@@ -728,7 +728,7 @@
 				<Tool
 					Name="VCCustomBuildTool"
 					Description="Compiling messages"
-					CommandLine="mc -A $(InputName).mc -r . -h ."
+					CommandLine="mc -u -U $(InputName).mc -r . -h ."
 					Outputs="$(InputName).rc;$(InputName).h"
 				/>
 			</FileConfiguration>
@@ -738,7 +738,7 @@
 				<Tool
 					Name="VCCustomBuildTool"
 					Description="Compiling messages"
-					CommandLine="mc -A $(InputName).mc -r . -h ."
+					CommandLine="mc -u -U $(InputName).mc -r . -h ."
 					AdditionalDependencies=""
 					Outputs="$(InputName).rc;$(InputName).h"
 				/>
@@ -749,7 +749,7 @@
 				<Tool
 					Name="VCCustomBuildTool"
 					Description="Compiling messages"
-					CommandLine="mc -A $(InputName).mc -r . -h ."
+					CommandLine="mc -u -U $(InputName).mc -r . -h ."
 					AdditionalDependencies=""
 					Outputs="$(InputName).rc;$(InputName).h"
 				/>