Compile messages.
authorIain Patterson <me@iain.cx>
Mon, 19 Apr 2010 22:17:55 +0000 (23:17 +0100)
committerIain Patterson <me@iain.cx>
Mon, 19 Apr 2010 22:18:51 +0000 (23:18 +0100)
Don't spam the event log with warnings about not finding application
messages.
Every message currently uses the default event ID 1.

event.cpp
event.h
nssm.cpp
nssm.h
nssm.rc
nssm.vcproj
registry.cpp
registry.h
service.cpp

index 7f2b9f4..2a9bbf2 100644 (file)
--- a/event.cpp
+++ b/event.cpp
@@ -2,13 +2,13 @@
 \r
 /* Convert error code to error string - must call LocalFree() on return value */\r
 char *error_string(unsigned long error) {\r
-  char *message;\r
-  if (! FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char *) &message, 0, 0)) return 0;\r
+  static char message[65535];\r
+  if (! FormatMessage(FORMAT_MESSAGE_FROM_HMODULE, 0, NSSM_MESSAGE_DEFAULT, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), message, sizeof(message), 0)) return 0;\r
   return message;\r
 }\r
 \r
 /* Log a message to the Event Log */\r
-void eventprintf(unsigned short type, char *format, ...) {\r
+void eventprintf(unsigned short type, unsigned long id, char *format, ...) {\r
   char message[4096];\r
   char *strings[2];\r
   int n, size;\r
@@ -32,7 +32,7 @@ void eventprintf(unsigned short type, char *format, ...) {
   if (! handle) return;\r
 \r
   /* Log it */\r
-  if (! ReportEvent(handle, type, 0, 0, 0, 1, 0, (const char **) strings, 0)) {\r
+  if (! ReportEvent(handle, type, 0, id, 0, 1, 0, (const char **) strings, 0)) {\r
     printf("ReportEvent(): %s\n", error_string(GetLastError()));\r
   }\r
 \r
diff --git a/event.h b/event.h
index 8bc7777..d6fb7ed 100644 (file)
--- a/event.h
+++ b/event.h
@@ -2,6 +2,6 @@
 #define EVENT_H\r
 \r
 char *error_string(unsigned long);\r
-void eventprintf(unsigned short, char *, ...);\r
+void eventprintf(unsigned short, unsigned long, char *, ...);\r
 \r
 #endif\r
index f412579..85add5f 100644 (file)
--- a/nssm.cpp
+++ b/nssm.cpp
@@ -39,11 +39,14 @@ int main(int argc, char **argv) {
   /* Undocumented: "run" is used to actually do service stuff */\r
   if (! str_equiv(argv[1], NSSM_RUN)) exit(usage(2));\r
 \r
+  /* Register messages */\r
+  create_messages();\r
+\r
   /* Start service magic */\r
   SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };\r
   if (! StartServiceCtrlDispatcher(table)) {\r
     char *message = error_string(GetLastError());\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "StartServiceCtrlDispatcher() failed: %s", message);\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "StartServiceCtrlDispatcher() failed: %s", message);\r
     if (message) LocalFree(message);\r
     return 100;\r
   }\r
diff --git a/nssm.h b/nssm.h
index 9b8c7cd..6f0a762 100644 (file)
--- a/nssm.h
+++ b/nssm.h
@@ -6,6 +6,7 @@
 #include <stdio.h>\r
 #include <windows.h>\r
 #include "event.h"\r
+#include "messages.h"\r
 #include "registry.h"\r
 #include "service.h"\r
 #include "gui.h"\r
diff --git a/nssm.rc b/nssm.rc
index aa1134d..ff9a524 100644 (file)
--- a/nssm.rc
+++ b/nssm.rc
@@ -1,4 +1,4 @@
-//Microsoft Developer Studio generated resource script.\r
+// Microsoft Visual C++ generated resource script.\r
 //\r
 #include "resource.h"\r
 \r
@@ -27,21 +27,19 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_UK
 // TEXTINCLUDE\r
 //\r
 \r
-1 TEXTINCLUDE DISCARDABLE \r
+1 TEXTINCLUDE \r
 BEGIN\r
     "resource.h\0"\r
 END\r
 \r
-2 TEXTINCLUDE DISCARDABLE \r
+2 TEXTINCLUDE \r
 BEGIN\r
-    "#include ""afxres.h""\r\n"\r
-    "\0"\r
+    "#include ""afxres.h""\r\0"\r
 END\r
 \r
-3 TEXTINCLUDE DISCARDABLE \r
+3 TEXTINCLUDE \r
 BEGIN\r
-    "\r\n"\r
-    "\0"\r
+    "#include ""messages.rc\0"\r
 END\r
 \r
 #endif    // APSTUDIO_INVOKED\r
@@ -54,15 +52,15 @@ END
 \r
 // Icon with lowest ID value placed first to ensure application icon\r
 // remains consistent on all systems.\r
-IDI_NSSM                ICON    DISCARDABLE     "nssm.ico"\r
+IDI_NSSM                ICON                    "nssm.ico"\r
 \r
 /////////////////////////////////////////////////////////////////////////////\r
 //\r
 // Dialog\r
 //\r
 \r
-IDD_INSTALL DIALOG DISCARDABLE  0, 0, 220, 90\r
-STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU\r
+IDD_INSTALL DIALOG  0, 0, 220, 90\r
+STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU\r
 CAPTION "NSSM service installer"\r
 FONT 8, "MS Sans Serif"\r
 BEGIN\r
@@ -77,8 +75,8 @@ BEGIN
     LTEXT           "Application:",IDC_STATIC,7,9,38,8\r
 END\r
 \r
-IDD_REMOVE DIALOG DISCARDABLE  0, 0, 223, 28\r
-STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU\r
+IDD_REMOVE DIALOG  0, 0, 223, 28\r
+STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU\r
 CAPTION "NSSM service remover"\r
 FONT 8, "MS Sans Serif"\r
 BEGIN\r
@@ -94,7 +92,7 @@ END
 //\r
 \r
 #ifdef APSTUDIO_INVOKED\r
-GUIDELINES DESIGNINFO DISCARDABLE \r
+GUIDELINES DESIGNINFO \r
 BEGIN\r
     IDD_INSTALL, DIALOG\r
     BEGIN\r
@@ -118,8 +116,7 @@ END
 //\r
 // Generated from the TEXTINCLUDE 3 resource.\r
 //\r
-\r
-\r
+#include "messages.rc"\r
 /////////////////////////////////////////////////////////////////////////////\r
 #endif    // not APSTUDIO_INVOKED\r
 \r
index 4bd08c5..f8300fb 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="Windows-1252"?>\r
 <VisualStudioProject\r
        ProjectType="Visual C++"\r
-       Version="9,00"\r
+       Version="9.00"\r
        Name="nssm"\r
        ProjectGUID="{32995E05-606F-4D83-A2E6-C2B361B34DF1}"\r
        RootNamespace="nssm"\r
@@ -33,6 +33,9 @@
                        />\r
                        <Tool\r
                                Name="VCCustomBuildTool"\r
+                               Description="Compiling messages"\r
+                               CommandLine="mc -A &quot;$(InputDir)\$(InputName).mc&quot; -r &quot;$(InputDir)&quot; -h &quot;$(InputDir)&quot;"\r
+                               Outputs="$(InputDir)\$(InputName).rc;$(InputDir)\$(InputName).h"\r
                        />\r
                        <Tool\r
                                Name="VCXMLDataGeneratorTool"\r
                        />\r
                </Configuration>\r
                <Configuration\r
-                       Name="Release|Win32"\r
-                       OutputDirectory=".\Release"\r
-                       IntermediateDirectory=".\Release"\r
+                       Name="Debug|x64"\r
+                       OutputDirectory="$(PlatformName)\$(ConfigurationName)"\r
+                       IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"\r
                        ConfigurationType="1"\r
                        InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC60.vsprops"\r
                        UseOfMFC="0"\r
                        />\r
                        <Tool\r
                                Name="VCCustomBuildTool"\r
+                               Description="Compiling messages"\r
+                               CommandLine="mc -A &quot;$(InputDir)\$(InputName).mc&quot; -r &quot;$(InputDir)&quot; -h &quot;$(InputDir)&quot;"\r
+                               Outputs="$(InputDir)\$(InputName).rc;$(InputDir)\$(InputName).h"\r
                        />\r
                        <Tool\r
                                Name="VCXMLDataGeneratorTool"\r
                        />\r
                        <Tool\r
                                Name="VCMIDLTool"\r
-                               TypeLibraryName=".\Release/nssm.tlb"\r
+                               TargetEnvironment="3"\r
+                               TypeLibraryName=".\Debug/nssm.tlb"\r
                                HeaderFileName=""\r
                        />\r
                        <Tool\r
                                Name="VCCLCompilerTool"\r
-                               Optimization="2"\r
-                               InlineFunctionExpansion="1"\r
-                               PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"\r
-                               StringPooling="true"\r
-                               RuntimeLibrary="0"\r
-                               EnableFunctionLevelLinking="true"\r
-                               PrecompiledHeaderFile=".\Release/nssm.pch"\r
-                               AssemblerListingLocation=".\Release/"\r
-                               ObjectFile=".\Release/"\r
-                               ProgramDataBaseFileName=".\Release/"\r
+                               Optimization="0"\r
+                               PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"\r
+                               MinimalRebuild="true"\r
+                               BasicRuntimeChecks="3"\r
+                               RuntimeLibrary="1"\r
+                               PrecompiledHeaderFile=".\Debug/nssm.pch"\r
+                               AssemblerListingLocation=".\Debug/"\r
+                               ObjectFile=".\Debug/"\r
+                               ProgramDataBaseFileName=".\Debug/"\r
                                WarningLevel="3"\r
                                SuppressStartupBanner="true"\r
+                               DebugInformationFormat="3"\r
                        />\r
                        <Tool\r
                                Name="VCManagedResourceCompilerTool"\r
                        />\r
                        <Tool\r
                                Name="VCResourceCompilerTool"\r
-                               PreprocessorDefinitions="NDEBUG"\r
+                               PreprocessorDefinitions="_DEBUG"\r
                                Culture="2057"\r
                        />\r
                        <Tool\r
                        <Tool\r
                                Name="VCLinkerTool"\r
                                OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"\r
-                               LinkIncremental="1"\r
+                               LinkIncremental="2"\r
                                SuppressStartupBanner="true"\r
-                               ProgramDatabaseFile=".\Release/nssm.pdb"\r
+                               GenerateDebugInformation="true"\r
+                               ProgramDatabaseFile=".\Debug/nssm_debug.pdb"\r
                                SubSystem="1"\r
                                RandomizedBaseAddress="1"\r
                                DataExecutionPrevention="0"\r
-                               TargetMachine="1"\r
+                               TargetMachine="17"\r
                        />\r
                        <Tool\r
                                Name="VCALinkTool"\r
                        <Tool\r
                                Name="VCBscMakeTool"\r
                                SuppressStartupBanner="true"\r
-                               OutputFile=".\Release/nssm.bsc"\r
+                               OutputFile=".\Debug/nssm.bsc"\r
                        />\r
                        <Tool\r
                                Name="VCFxCopTool"\r
                        />\r
                </Configuration>\r
                <Configuration\r
-                       Name="Debug|x64"\r
-                       OutputDirectory="$(PlatformName)\$(ConfigurationName)"\r
-                       IntermediateDirectory="$(PlatformName)\$(ConfigurationName)"\r
+                       Name="Release|Win32"\r
+                       OutputDirectory=".\Release"\r
+                       IntermediateDirectory=".\Release"\r
                        ConfigurationType="1"\r
                        InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC60.vsprops"\r
                        UseOfMFC="0"\r
                        />\r
                        <Tool\r
                                Name="VCCustomBuildTool"\r
+                               Description=""\r
+                               CommandLine=""\r
+                               Outputs=""\r
                        />\r
                        <Tool\r
                                Name="VCXMLDataGeneratorTool"\r
                        />\r
                        <Tool\r
                                Name="VCMIDLTool"\r
-                               TargetEnvironment="3"\r
-                               TypeLibraryName=".\Debug/nssm.tlb"\r
+                               TypeLibraryName=".\Release/nssm.tlb"\r
                                HeaderFileName=""\r
                        />\r
                        <Tool\r
                                Name="VCCLCompilerTool"\r
-                               Optimization="0"\r
-                               PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"\r
-                               MinimalRebuild="true"\r
-                               BasicRuntimeChecks="3"\r
-                               RuntimeLibrary="1"\r
-                               PrecompiledHeaderFile=".\Debug/nssm.pch"\r
-                               AssemblerListingLocation=".\Debug/"\r
-                               ObjectFile=".\Debug/"\r
-                               ProgramDataBaseFileName=".\Debug/"\r
+                               Optimization="2"\r
+                               InlineFunctionExpansion="1"\r
+                               PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"\r
+                               StringPooling="true"\r
+                               RuntimeLibrary="0"\r
+                               EnableFunctionLevelLinking="true"\r
+                               PrecompiledHeaderFile=".\Release/nssm.pch"\r
+                               AssemblerListingLocation=".\Release/"\r
+                               ObjectFile=".\Release/"\r
+                               ProgramDataBaseFileName=".\Release/"\r
                                WarningLevel="3"\r
                                SuppressStartupBanner="true"\r
-                               DebugInformationFormat="3"\r
                        />\r
                        <Tool\r
                                Name="VCManagedResourceCompilerTool"\r
                        />\r
                        <Tool\r
                                Name="VCResourceCompilerTool"\r
-                               PreprocessorDefinitions="_DEBUG"\r
+                               PreprocessorDefinitions="NDEBUG"\r
                                Culture="2057"\r
                        />\r
                        <Tool\r
                        <Tool\r
                                Name="VCLinkerTool"\r
                                OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"\r
-                               LinkIncremental="2"\r
+                               LinkIncremental="1"\r
                                SuppressStartupBanner="true"\r
-                               GenerateDebugInformation="true"\r
-                               ProgramDatabaseFile=".\Debug/nssm_debug.pdb"\r
+                               ProgramDatabaseFile=".\Release/nssm.pdb"\r
                                SubSystem="1"\r
                                RandomizedBaseAddress="1"\r
                                DataExecutionPrevention="0"\r
-                               TargetMachine="17"\r
+                               TargetMachine="1"\r
                        />\r
                        <Tool\r
                                Name="VCALinkTool"\r
                        <Tool\r
                                Name="VCBscMakeTool"\r
                                SuppressStartupBanner="true"\r
-                               OutputFile=".\Debug/nssm.bsc"\r
+                               OutputFile=".\Release/nssm.bsc"\r
                        />\r
                        <Tool\r
                                Name="VCFxCopTool"\r
                        />\r
                        <Tool\r
                                Name="VCCustomBuildTool"\r
+                               Description=""\r
+                               CommandLine=""\r
+                               Outputs=""\r
                        />\r
                        <Tool\r
                                Name="VCXMLDataGeneratorTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Release|Win32"\r
+                                       Name="Debug|x64"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Debug|x64"\r
+                                       Name="Release|Win32"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Release|Win32"\r
+                                       Name="Debug|x64"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Debug|x64"\r
+                                       Name="Release|Win32"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Release|Win32"\r
+                                       Name="Debug|x64"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Debug|x64"\r
+                                       Name="Release|Win32"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Release|Win32"\r
+                                       Name="Debug|x64"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Debug|x64"\r
+                                       Name="Release|Win32"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Release|Win32"\r
+                                       Name="Debug|x64"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Debug|x64"\r
+                                       Name="Release|Win32"\r
                                        >\r
                                        <Tool\r
                                                Name="VCCLCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Release|Win32"\r
+                                       Name="Debug|x64"\r
                                        >\r
                                        <Tool\r
                                                Name="VCResourceCompilerTool"\r
                                        />\r
                                </FileConfiguration>\r
                                <FileConfiguration\r
-                                       Name="Debug|x64"\r
+                                       Name="Release|Win32"\r
                                        >\r
                                        <Tool\r
                                                Name="VCResourceCompilerTool"\r
                                </FileConfiguration>\r
                        </File>\r
                </Filter>\r
+               <File\r
+                       RelativePath=".\messages.mc"\r
+                       >\r
+                       <FileConfiguration\r
+                               Name="Debug|Win32"\r
+                               >\r
+                               <Tool\r
+                                       Name="VCCustomBuildTool"\r
+                                       Description="Compiling messages"\r
+                                       CommandLine="mc -A $(InputPath) -r $(InputDir) -h $(InputDir)"\r
+                                       Outputs="$(InputFileName).rc;$(InputFileName).h"\r
+                               />\r
+                       </FileConfiguration>\r
+                       <FileConfiguration\r
+                               Name="Debug|x64"\r
+                               >\r
+                               <Tool\r
+                                       Name="VCCustomBuildTool"\r
+                                       Description="Compiling messages"\r
+                                       CommandLine="mc -A $(InputPath) -r $(InputDir) -h $(InputDir)"\r
+                                       Outputs="$(InputFileName).rc;$(InputFileName).h"\r
+                               />\r
+                       </FileConfiguration>\r
+                       <FileConfiguration\r
+                               Name="Release|Win32"\r
+                               >\r
+                               <Tool\r
+                                       Name="VCCustomBuildTool"\r
+                                       Description="Compiling messages"\r
+                                       CommandLine="mc -A $(InputPath) -r $(InputDir) -h $(InputDir)"\r
+                                       AdditionalDependencies=""\r
+                                       Outputs="$(InputFileName).rc;$(InputFileName).h"\r
+                               />\r
+                       </FileConfiguration>\r
+                       <FileConfiguration\r
+                               Name="Release|x64"\r
+                               >\r
+                               <Tool\r
+                                       Name="VCCustomBuildTool"\r
+                                       Description="Compiling messages"\r
+                                       CommandLine="mc -A $(InputPath) -r $(InputDir) -h $(InputDir)"\r
+                                       AdditionalDependencies=""\r
+                                       Outputs="$(InputFileName).rc;$(InputFileName).h"\r
+                               />\r
+                       </FileConfiguration>\r
+               </File>\r
        </Files>\r
        <Globals>\r
        </Globals>\r
index d1080ae..eca8e78 100644 (file)
@@ -1,35 +1,61 @@
 #include "nssm.h"\r
 \r
+int create_messages() {\r
+  HKEY key;\r
+\r
+  char registry[MAX_PATH];\r
+  if (_snprintf(registry, sizeof(registry), "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", NSSM) < 0) {\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Out of memory for eventlog registry()!");\r
+    return 1;\r
+  }\r
+\r
+  if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, 0) != ERROR_SUCCESS) {\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't open eventlog registry!", NSSM_REGISTRY);\r
+    return 2;\r
+  }\r
+\r
+  /* Get path of this program */\r
+  char path[MAX_PATH];\r
+  GetModuleFileName(0, path, MAX_PATH);\r
+\r
+  /* Try to register the module but don't worry so much on failure */\r
+  RegSetValueEx(key, "EventMessageFile", 0, REG_SZ, (const unsigned char *) path, strlen(path) + 1);\r
+  unsigned long types = EVENTLOG_INFORMATION_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_ERROR_TYPE;\r
+  RegSetValueEx(key, "TypesSupported", 0, REG_DWORD, /*XXX*/(PBYTE) &types, sizeof(types));\r
+\r
+  return 0;\r
+}\r
+\r
 int create_parameters(char *service_name, char *exe, char *flags, char *dir) {\r
   /* Get registry */\r
   char registry[MAX_PATH];\r
   if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REGISTRY in create_parameters()!");\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Out of memory for NSSM_REGISTRY in create_parameters()!");\r
     return 1;\r
   }\r
 \r
   /* Try to open the registry */\r
   HKEY key;\r
   if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, 0) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't open service registry settings!", NSSM_REGISTRY);\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't open service registry settings!", NSSM_REGISTRY);\r
     return 2;\r
   }\r
 \r
   /* Try to create the parameters */\r
   if (RegSetValueEx(key, NSSM_REG_EXE, 0, REG_SZ, (const unsigned char *) exe, strlen(exe) + 1) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't add registry value %s: %s", NSSM_REG_EXE, error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't add registry value %s: %s", NSSM_REG_EXE, error_string(GetLastError()));\r
     RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
     RegCloseKey(key);\r
     return 3;\r
   }\r
   if (RegSetValueEx(key, NSSM_REG_FLAGS, 0, REG_SZ, (const unsigned char *) flags, strlen(flags) + 1) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't add registry value %s: %s", NSSM_REG_FLAGS, error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't add registry value %s: %s", NSSM_REG_FLAGS, error_string(GetLastError()));\r
     RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
     RegCloseKey(key);\r
     return 4;\r
   }\r
   if (RegSetValueEx(key, NSSM_REG_DIR, 0, REG_SZ, (const unsigned char *) dir, strlen(dir) + 1) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't add registry value %s: %s", NSSM_REG_DIR, error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't add registry value %s: %s", NSSM_REG_DIR, error_string(GetLastError()));\r
     RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
     RegCloseKey(key);\r
     return 5;\r
@@ -45,7 +71,7 @@ int create_exit_action(char *service_name, const char *action_string) {
   /* Get registry */\r
   char registry[MAX_PATH];\r
   if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REG_EXIT in create_exit_action()!");\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Out of memory for NSSM_REG_EXIT in create_exit_action()!");\r
     return 1;\r
   }\r
 \r
@@ -53,7 +79,7 @@ int create_exit_action(char *service_name, const char *action_string) {
   HKEY key;\r
   unsigned long disposition;\r
   if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, registry, 0, 0, REG_OPTION_NON_VOLATILE, KEY_WRITE, 0, &key, &disposition) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't open service exit action registry settings!");\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't open service exit action registry settings!");\r
     return 2;\r
   }\r
 \r
@@ -65,7 +91,7 @@ int create_exit_action(char *service_name, const char *action_string) {
 \r
   /* Create the default value */\r
   if (RegSetValueEx(key, 0, 0, REG_SZ, (const unsigned char *) action_string, strlen(action_string) + 1) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't add default registry value %s: %s", NSSM_REG_EXIT, error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't add default registry value %s: %s", NSSM_REG_EXIT, error_string(GetLastError()));\r
     RegCloseKey(key);\r
     return 3;\r
   }\r
@@ -80,14 +106,14 @@ int get_parameters(char *service_name, char *exe, int exelen, char *flags, int f
   /* Get registry */\r
   char registry[MAX_PATH];\r
   if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY, service_name) < 0) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REGISTRY in get_parameters()!");\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Out of memory for NSSM_REGISTRY in get_parameters()!");\r
     return 1;\r
   }\r
 \r
   /* Try to open the registry */\r
   HKEY key;\r
   if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, KEY_READ, &key) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't open service registry settings!", NSSM_REGISTRY);\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't open service registry settings!", NSSM_REGISTRY);\r
     return 2;\r
   }\r
 \r
@@ -95,21 +121,21 @@ int get_parameters(char *service_name, char *exe, int exelen, char *flags, int f
 \r
   /* Try to get executable file - MUST succeed */\r
   if (RegQueryValueEx(key, NSSM_REG_EXE, 0, &type, (unsigned char *) exe, (unsigned long *) &exelen) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't get application path (registry value %s): %s", NSSM_REG_EXE, error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't get application path (registry value %s): %s", NSSM_REG_EXE, error_string(GetLastError()));\r
     RegCloseKey(key);\r
     return 3;\r
   }\r
 \r
   /* Try to get flags - may fail */\r
   if (RegQueryValueEx(key, NSSM_REG_FLAGS, 0, &type, (unsigned char *) flags, (unsigned long *) &flagslen) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_WARNING_TYPE, "Can't get application flags (registry value %s): %s", NSSM_REG_FLAGS, error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_WARNING_TYPE, NSSM_MESSAGE_DEFAULT, "Can't get application flags (registry value %s): %s", NSSM_REG_FLAGS, error_string(GetLastError()));\r
     RegCloseKey(key);\r
     return 4;\r
   }\r
 \r
   /* Try to get startup directory - may fail */\r
   if (RegQueryValueEx(key, NSSM_REG_DIR, 0, &type, (unsigned char *) dir, (unsigned long *) &dirlen) != ERROR_SUCCESS) {\r
-    eventprintf(EVENTLOG_WARNING_TYPE, "Can't get application startup directory (registry value %s): %s", NSSM_REG_DIR, error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_WARNING_TYPE, NSSM_MESSAGE_DEFAULT, "Can't get application startup directory (registry value %s): %s", NSSM_REG_DIR, error_string(GetLastError()));\r
     RegCloseKey(key);\r
     return 5;\r
   }\r
@@ -124,7 +150,7 @@ int get_exit_action(char *service_name, unsigned long *ret, unsigned char *actio
   /* Get registry */\r
   char registry[MAX_PATH];\r
   if (_snprintf(registry, sizeof(registry), NSSM_REGISTRY "\\%s", service_name, NSSM_REG_EXIT) < 0) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Out of memory for NSSM_REG_EXIT in get_exit_action()!");\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Out of memory for NSSM_REG_EXIT in get_exit_action()!");\r
     return 1;\r
   }\r
 \r
@@ -132,7 +158,7 @@ int get_exit_action(char *service_name, unsigned long *ret, unsigned char *actio
   HKEY key;\r
   long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, KEY_READ, &key);\r
   if (error != ERROR_SUCCESS && error != ERROR_FILE_NOT_FOUND) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't open registry %s!", registry);\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't open registry %s!", registry);\r
     return 2;\r
   }\r
 \r
index 3ad673f..2f45f99 100644 (file)
@@ -7,6 +7,7 @@
 #define NSSM_REG_DIR "AppDirectory"\r
 #define NSSM_REG_EXIT "AppExit"\r
 \r
+int create_messages();\r
 int create_parameters(char *, char *, char *, char *);\r
 int create_exit_action(char *, const char *);\r
 int get_parameters(char *, char *, int, char *, int, char *, int);\r
index 4dc6fa2..ad5f2c2 100644 (file)
@@ -16,7 +16,7 @@ static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", 0 };
 SC_HANDLE open_service_manager() {\r
   SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);\r
   if (! ret) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Unable to connect to service manager!\nPerhaps you need to be an administrator...");\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Unable to connect to service manager!\nPerhaps you need to be an administrator...");\r
     return 0;\r
   }\r
 \r
@@ -139,7 +139,7 @@ int remove_service(char *name) {
 /* Service initialisation */\r
 void WINAPI service_main(unsigned long argc, char **argv) {\r
   if (_snprintf(service_name, sizeof(service_name), "%s", argv[0]) < 0) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): Out of memory for service_name!");\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "service_main(): Out of memory for service_name!");\r
     return;\r
   }\r
 \r
@@ -159,14 +159,14 @@ void WINAPI service_main(unsigned long argc, char **argv) {
   /* Get startup parameters */\r
   int ret = get_parameters(argv[0], exe, sizeof(exe), flags, sizeof(flags), dir, sizeof(dir));\r
   if (ret) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): Can't get startup parameters: error %d", ret);\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "service_main(): Can't get startup parameters: error %d", ret);\r
     return;\r
   }\r
 \r
   /* Register control handler */\r
   service_handle = RegisterServiceCtrlHandlerEx(NSSM, service_control_handler, 0);\r
   if (! service_handle) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "service_main(): RegisterServiceCtrlHandlerEx() failed: %s", error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "service_main(): RegisterServiceCtrlHandlerEx() failed: %s", error_string(GetLastError()));\r
     return;\r
   }\r
 \r
@@ -180,14 +180,14 @@ int monitor_service() {
   /* Set service status to started */\r
   int ret = start_service();\r
   if (ret) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't start service %s: error code %d", service_name, ret);\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't start service %s: error code %d", service_name, ret);\r
     return ret;\r
   }\r
-  eventprintf(EVENTLOG_INFORMATION_TYPE, "Started process %s %s in %s for service %s", exe, flags, dir, service_name);\r
+  eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Started process %s %s in %s for service %s", exe, flags, dir, service_name);\r
 \r
   /* Monitor service service */\r
   if (! RegisterWaitForSingleObject(&wait_handle, pid, end_service, 0, INFINITE, WT_EXECUTEONLYONCE | WT_EXECUTELONGFUNCTION)) {\r
-    eventprintf(EVENTLOG_WARNING_TYPE, "RegisterWaitForSingleObject() returned %s - service may claim to be still running when %s exits ", error_string(GetLastError()), exe);\r
+    eventprintf(EVENTLOG_WARNING_TYPE, NSSM_MESSAGE_DEFAULT, "RegisterWaitForSingleObject() returned %s - service may claim to be still running when %s exits ", error_string(GetLastError()), exe);\r
   }\r
 \r
   return 0;\r
@@ -222,11 +222,11 @@ int start_service() {
   /* Launch executable with arguments */\r
   char cmd[MAX_PATH];\r
   if (_snprintf(cmd, sizeof(cmd), "%s %s", exe, flags) < 0) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Error constructing command line");\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Error constructing command line");\r
     return stop_service(2);\r
   }\r
   if (! CreateProcess(0, cmd, 0, 0, 0, 0, 0, dir, &si, &pi)) {\r
-    eventprintf(EVENTLOG_ERROR_TYPE, "Can't launch %s.  CreateProcess() returned %s", exe, error_string(GetLastError()));\r
+    eventprintf(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_DEFAULT, "Can't launch %s.  CreateProcess() returned %s", exe, error_string(GetLastError()));\r
     return stop_service(3);\r
   }\r
   pid = pi.hProcess;\r
@@ -272,7 +272,7 @@ void CALLBACK end_service(void *arg, unsigned char why) {
   unsigned long ret = 0;\r
   GetExitCodeProcess(pid, &ret);\r
 \r
-  eventprintf(EVENTLOG_INFORMATION_TYPE, "Process %s for service %s exited with return code %u", exe, service_name, ret);\r
+  eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Process %s for service %s exited with return code %u", exe, service_name, ret);\r
 \r
   /* What action should we take? */\r
   int action = NSSM_EXIT_RESTART;\r
@@ -290,22 +290,22 @@ void CALLBACK end_service(void *arg, unsigned char why) {
   switch (action) {\r
     /* Try to restart the service or return failure code to service manager */\r
     case NSSM_EXIT_RESTART:\r
-      eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name);\r
+      eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Action for exit code %lu is %s: Attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name);\r
       while (monitor_service()) {\r
-        eventprintf(EVENTLOG_INFORMATION_TYPE, "Failed to restart %s - sleeping ", exe, ret);\r
+        eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Failed to restart %s - sleeping ", exe, ret);\r
         Sleep(30000);\r
       }\r
     break;\r
 \r
     /* Do nothing, just like srvany would */\r
     case NSSM_EXIT_IGNORE:\r
-      eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Not attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name);\r
+      eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Action for exit code %lu is %s: Not attempting to restart %s for service %s", ret, exit_action_strings[action], exe, service_name);\r
       Sleep(INFINITE);\r
     break;\r
 \r
     /* Tell the service manager we are finished */\r
     case NSSM_EXIT_REALLY:\r
-      eventprintf(EVENTLOG_INFORMATION_TYPE, "Action for exit code %lu is %s: Stopping service %s", ret, exit_action_strings[action], service_name);\r
+      eventprintf(EVENTLOG_INFORMATION_TYPE, NSSM_MESSAGE_DEFAULT, "Action for exit code %lu is %s: Stopping service %s", ret, exit_action_strings[action], service_name);\r
       stop_service(ret);\r
     break;\r
   }\r