[resend 1/8] reg: Add system error printing function

Jonathan Vollebregt jnvsor at gmail.com
Wed Jan 14 11:12:21 CST 2015


---
 programs/reg/reg.c  | 32 ++++++++++++++++++++++++++++----
 programs/reg/reg.h  |  1 +
 programs/reg/reg.rc |  1 +
 3 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/programs/reg/reg.c b/programs/reg/reg.c
index e9c7d16..a88512d 100644
--- a/programs/reg/reg.c
+++ b/programs/reg/reg.c
@@ -67,6 +67,30 @@ static int reg_message(int msg)
     return reg_printfW(formatW, msg_buffer);
 }
 
+static void reg_print_error(LSTATUS error_code)
+{
+    switch (error_code)
+    {
+        case ERROR_SUCCESS:
+            return;
+        case ERROR_BAD_COMMAND:
+            reg_message(STRING_INVALID_CMDLINE);
+            return;
+        default:
+        {
+            static const WCHAR error_string[] = {'%','0','5','d',':',' ','%','s',0};
+            WCHAR *message = NULL;
+            FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL,
+                error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (WCHAR *)&message, 0, NULL);
+
+            reg_message(STRING_ERROR);
+            reg_printfW(error_string, error_code, message);
+            LocalFree(message);
+            return;
+        }
+    }
+}
+
 static int reg_StrCmpNIW(LPCWSTR str, LPCWSTR comp, int len)
 {
     int i;
@@ -413,7 +437,7 @@ int wmain(int argc, WCHAR *argvW[])
 
         if (argc < 3)
         {
-            reg_message(STRING_INVALID_CMDLINE);
+            reg_print_error(ERROR_BAD_COMMAND);
             return 1;
         }
         else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
@@ -449,7 +473,7 @@ int wmain(int argc, WCHAR *argvW[])
 
         if (argc < 3)
         {
-            reg_message(STRING_INVALID_CMDLINE);
+            reg_print_error(ERROR_BAD_COMMAND);
             return 1;
         }
         else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
@@ -480,7 +504,7 @@ int wmain(int argc, WCHAR *argvW[])
 
         if (argc < 3)
         {
-            reg_message(STRING_INVALID_CMDLINE);
+            reg_print_error(ERROR_BAD_COMMAND);
             return 1;
         }
         else if (argc == 3 && (!lstrcmpW(argvW[2], slashHelpW) ||
@@ -504,7 +528,7 @@ int wmain(int argc, WCHAR *argvW[])
     }
     else
     {
-        reg_message(STRING_INVALID_CMDLINE);
+        reg_print_error(ERROR_BAD_COMMAND);
         return 1;
     }
 }
diff --git a/programs/reg/reg.h b/programs/reg/reg.h
index 2221647..1c2ae83 100644
--- a/programs/reg/reg.h
+++ b/programs/reg/reg.h
@@ -30,3 +30,4 @@
 #define STRING_INVALID_CMDLINE  107
 #define STRING_NO_REMOTE        108
 #define STRING_CANNOT_FIND      109
+#define STRING_ERROR            110
diff --git a/programs/reg/reg.rc b/programs/reg/reg.rc
index 479912b..6183aaa 100644
--- a/programs/reg/reg.rc
+++ b/programs/reg/reg.rc
@@ -35,4 +35,5 @@ STRINGTABLE
     STRING_INVALID_CMDLINE, "Error: Invalid command line parameters\n"
     STRING_NO_REMOTE, "Error: Unable to add keys to remote machine\n"
     STRING_CANNOT_FIND, "Error: The system was unable to find the specified registry key or value\n"
+    STRING_ERROR, "Unexpected error: "
 }
-- 
2.1.4




More information about the wine-patches mailing list