Fixed loading localised resources.
authorIain Patterson <me@iain.cx>
Sun, 29 Dec 2013 17:25:54 +0000 (17:25 +0000)
committerIain Patterson <me@iain.cx>
Sun, 29 Dec 2013 18:00:58 +0000 (18:00 +0000)
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"

event.cpp
gui.cpp
messages.mc
nssm.rc
nssm.vcproj

index 6744da2..1313351 100644 (file)
--- a/event.cpp
+++ b/event.cpp
@@ -14,8 +14,10 @@ TCHAR *error_string(unsigned long error) {
     TlsSetValue(tls_index, (void *) error_message);\r
   }\r
 \r
-  if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) {\r
-    if (_sntprintf_s(error_message, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0;\r
+  if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, GetUserDefaultLangID(), (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) {\r
+    if (! FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, (TCHAR *) error_message, NSSM_ERROR_BUFSIZE, 0)) {\r
+      if (_sntprintf_s(error_message, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0;\r
+    }\r
   }\r
   return error_message;\r
 }\r
@@ -23,9 +25,11 @@ TCHAR *error_string(unsigned long error) {
 /* Convert message code to format string */\r
 TCHAR *message_string(unsigned long error) {\r
   TCHAR *ret;\r
-  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)) {\r
-    ret = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, 32 * sizeof(TCHAR));\r
-    if (_sntprintf_s(ret, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0;\r
+  if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, GetUserDefaultLangID(), (LPTSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) {\r
+    if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, 0, (LPTSTR) &ret, NSSM_ERROR_BUFSIZE, 0)) {\r
+      ret = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, 32 * sizeof(TCHAR));\r
+      if (_sntprintf_s(ret, NSSM_ERROR_BUFSIZE, _TRUNCATE, _T("system error %lu"), error) < 0) return 0;\r
+    }\r
   }\r
   return ret;\r
 }\r
diff --git a/gui.cpp b/gui.cpp
index 6832129..d9b7011 100644 (file)
--- a/gui.cpp
+++ b/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];\r
 static int selected_tab;\r
 \r
+static HWND dialog(const TCHAR *templ, HWND parent, DLGPROC function, LPARAM l) {\r
+  /* The caller will deal with GetLastError()... */\r
+  HRSRC resource = FindResourceEx(0, RT_DIALOG, templ, GetUserDefaultLangID());\r
+  if (! resource) {\r
+    if (GetLastError() != ERROR_RESOURCE_LANG_NOT_FOUND) return 0;\r
+    resource = FindResourceEx(0, RT_DIALOG, templ, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL));\r
+    if (! resource) return 0;\r
+  }\r
+\r
+  HGLOBAL ret = LoadResource(0, resource);\r
+  if (! ret) return 0;\r
+\r
+  return CreateDialogIndirectParam(0, (DLGTEMPLATE *) ret, parent, function, l);\r
+}\r
+\r
+static HWND dialog(const TCHAR *templ, HWND parent, DLGPROC function) {\r
+  return dialog(templ, parent, function, 0);\r
+}\r
+\r
 int nssm_gui(int resource, nssm_service_t *service) {\r
   /* Create window */\r
-  HWND dlg = CreateDialogParam(0, MAKEINTRESOURCE(resource), 0, nssm_dlg, (LPARAM) service);\r
+  HWND dlg = dialog(MAKEINTRESOURCE(resource), 0, nssm_dlg, (LPARAM) service);\r
   if (! dlg) {\r
     popup_message(MB_OK, NSSM_GUI_CREATEDIALOG_FAILED, error_string(GetLastError()));\r
     return 1;\r
@@ -837,18 +856,18 @@ INT_PTR CALLBACK nssm_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {
       tab.cchTextMax = (int) _tcslen(tab.pszText);\r
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_APPLICATION, (LPARAM) &tab);\r
       if (service->native) {\r
-        tablist[NSSM_TAB_APPLICATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_NATIVE), window, tab_dlg);\r
+        tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_NATIVE), window, tab_dlg);\r
         EnableWindow(tablist[NSSM_TAB_APPLICATION], 0);\r
         EnableWindow(GetDlgItem(tablist[NSSM_TAB_APPLICATION], IDC_PATH), 0);\r
       }\r
-      else tablist[NSSM_TAB_APPLICATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);\r
+      else tablist[NSSM_TAB_APPLICATION] = dialog(MAKEINTRESOURCE(IDD_APPLICATION), window, tab_dlg);\r
       ShowWindow(tablist[NSSM_TAB_APPLICATION], SW_SHOW);\r
 \r
       /* Details tab. */\r
       tab.pszText = message_string(NSSM_GUI_TAB_DETAILS);\r
       tab.cchTextMax = (int) _tcslen(tab.pszText);\r
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_DETAILS, (LPARAM) &tab);\r
-      tablist[NSSM_TAB_DETAILS] = CreateDialog(0, MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);\r
+      tablist[NSSM_TAB_DETAILS] = dialog(MAKEINTRESOURCE(IDD_DETAILS), window, tab_dlg);\r
       ShowWindow(tablist[NSSM_TAB_DETAILS], SW_HIDE);\r
 \r
       /* Set defaults. */\r
@@ -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);\r
       tab.cchTextMax = (int) _tcslen(tab.pszText);\r
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_LOGON, (LPARAM) &tab);\r
-      tablist[NSSM_TAB_LOGON] = CreateDialog(0, MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg);\r
+      tablist[NSSM_TAB_LOGON] = dialog(MAKEINTRESOURCE(IDD_LOGON), window, tab_dlg);\r
       ShowWindow(tablist[NSSM_TAB_LOGON], SW_HIDE);\r
 \r
       /* Set defaults. */\r
@@ -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);\r
       tab.cchTextMax = (int) _tcslen(tab.pszText);\r
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_SHUTDOWN, (LPARAM) &tab);\r
-      tablist[NSSM_TAB_SHUTDOWN] = CreateDialog(0, MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);\r
+      tablist[NSSM_TAB_SHUTDOWN] = dialog(MAKEINTRESOURCE(IDD_SHUTDOWN), window, tab_dlg);\r
       ShowWindow(tablist[NSSM_TAB_SHUTDOWN], SW_HIDE);\r
 \r
       /* Set defaults. */\r
@@ -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);\r
       tab.cchTextMax = (int) _tcslen(tab.pszText);\r
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_EXIT, (LPARAM) &tab);\r
-      tablist[NSSM_TAB_EXIT] = CreateDialog(0, MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);\r
+      tablist[NSSM_TAB_EXIT] = dialog(MAKEINTRESOURCE(IDD_APPEXIT), window, tab_dlg);\r
       ShowWindow(tablist[NSSM_TAB_EXIT], SW_HIDE);\r
 \r
       /* Set defaults. */\r
@@ -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);\r
       tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;\r
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_IO, (LPARAM) &tab);\r
-      tablist[NSSM_TAB_IO] = CreateDialog(0, MAKEINTRESOURCE(IDD_IO), window, tab_dlg);\r
+      tablist[NSSM_TAB_IO] = dialog(MAKEINTRESOURCE(IDD_IO), window, tab_dlg);\r
       ShowWindow(tablist[NSSM_TAB_IO], SW_HIDE);\r
 \r
       /* Rotation tab. */\r
       tab.pszText = message_string(NSSM_GUI_TAB_ROTATION);\r
       tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;\r
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ROTATION, (LPARAM) &tab);\r
-      tablist[NSSM_TAB_ROTATION] = CreateDialog(0, MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);\r
+      tablist[NSSM_TAB_ROTATION] = dialog(MAKEINTRESOURCE(IDD_ROTATION), window, tab_dlg);\r
       ShowWindow(tablist[NSSM_TAB_ROTATION], SW_HIDE);\r
 \r
       /* Set defaults. */\r
@@ -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);\r
       tab.cchTextMax = (int) _tcslen(tab.pszText) + 1;\r
       SendMessage(tabs, TCM_INSERTITEM, NSSM_TAB_ENVIRONMENT, (LPARAM) &tab);\r
-      tablist[NSSM_TAB_ENVIRONMENT] = CreateDialog(0, MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);\r
+      tablist[NSSM_TAB_ENVIRONMENT] = dialog(MAKEINTRESOURCE(IDD_ENVIRONMENT), window, tab_dlg);\r
       ShowWindow(tablist[NSSM_TAB_ENVIRONMENT], SW_HIDE);\r
 \r
       return 1;\r
index 3c4ae05..40a7f1c 100644 (file)
Binary files a/messages.mc and b/messages.mc differ
diff --git a/nssm.rc b/nssm.rc
index 3c03bb4..b620e5e 100644 (file)
Binary files a/nssm.rc and b/nssm.rc differ
index 3b52894..dfac3e5 100755 (executable)
                                <Tool\r
                                        Name="VCCustomBuildTool"\r
                                        Description="Compiling messages"\r
-                                       CommandLine="mc -A $(InputName).mc -r . -h ."\r
+                                       CommandLine="mc -u -U $(InputName).mc -r . -h ."\r
                                        Outputs="$(InputName).rc;$(InputName).h"\r
                                />\r
                        </FileConfiguration>\r
                                <Tool\r
                                        Name="VCCustomBuildTool"\r
                                        Description="Compiling messages"\r
-                                       CommandLine="mc -A $(InputName).mc -r . -h ."\r
+                                       CommandLine="mc -u -U $(InputName).mc -r . -h ."\r
                                        Outputs="$(InputName).rc;$(InputName).h"\r
                                />\r
                        </FileConfiguration>\r
                                <Tool\r
                                        Name="VCCustomBuildTool"\r
                                        Description="Compiling messages"\r
-                                       CommandLine="mc -A $(InputName).mc -r . -h ."\r
+                                       CommandLine="mc -u -U $(InputName).mc -r . -h ."\r
                                        AdditionalDependencies=""\r
                                        Outputs="$(InputName).rc;$(InputName).h"\r
                                />\r
                                <Tool\r
                                        Name="VCCustomBuildTool"\r
                                        Description="Compiling messages"\r
-                                       CommandLine="mc -A $(InputName).mc -r . -h ."\r
+                                       CommandLine="mc -u -U $(InputName).mc -r . -h ."\r
                                        AdditionalDependencies=""\r
                                        Outputs="$(InputName).rc;$(InputName).h"\r
                                />\r