CreateFile returns INVALID_HANDLE_VALUE on failure https://msdn.microsoft.com/en...
authorDavid Bremner <dbremner@gmail.com>
Mon, 4 Jul 2016 21:47:25 +0000 (14:47 -0700)
committerIain Patterson <me@iain.cx>
Tue, 5 Jul 2016 09:03:45 +0000 (10:03 +0100)
io.cpp

diff --git a/io.cpp b/io.cpp
index 45d1e76..06d85d4 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -156,7 +156,7 @@ int delete_createfile_parameter(HKEY key, TCHAR *prefix, TCHAR *suffix) {
 \r
 HANDLE write_to_file(TCHAR *path, unsigned long sharing, SECURITY_ATTRIBUTES *attributes, unsigned long disposition, unsigned long flags) {\r
   HANDLE ret = CreateFile(path, FILE_WRITE_DATA, sharing, attributes, disposition, flags, 0);\r
-  if (ret) {\r
+  if (ret!= INVALID_HANDLE_VALUE) {\r
     if (SetFilePointer(ret, 0, 0, FILE_END) != INVALID_SET_FILE_POINTER) SetEndOfFile(ret);\r
     return ret;\r
   }\r
@@ -192,7 +192,7 @@ void rotate_file(TCHAR *service_name, TCHAR *path, unsigned long seconds, unsign
 \r
   /* Try to open the file to check if it exists and to get attributes. */\r
   HANDLE file = CreateFile(path, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);\r
-  if (file) {\r
+  if (file != INVALID_HANDLE_VALUE) {\r
     /* Get file attributes. */\r
     if (! GetFileInformationByHandle(file, &info)) {\r
       /* Reuse current time for rotation timestamp. */\r
@@ -275,7 +275,7 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {
   /* stdin */\r
   if (service->stdin_path[0]) {\r
     si->hStdInput = CreateFile(service->stdin_path, FILE_READ_DATA, service->stdin_sharing, 0, service->stdin_disposition, service->stdin_flags, 0);\r
-    if (! si->hStdInput) {\r
+    if (si->hStdInput == INVALID_HANDLE_VALUE) {\r
       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, service->stdin_path, error_string(GetLastError()), 0);\r
       return 2;\r
     }\r
@@ -285,7 +285,7 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {
   if (service->stdout_path[0]) {\r
     if (service->rotate_files) rotate_file(service->name, service->stdout_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high, service->rotate_delay, service->stdout_copy_and_truncate);\r
     HANDLE stdout_handle = write_to_file(service->stdout_path, service->stdout_sharing, 0, service->stdout_disposition, service->stdout_flags);\r
-    if (! stdout_handle) return 4;\r
+    if (stdout_handle == INVALID_HANDLE_VALUE) return 4;\r
 \r
     if (service->rotate_files && service->rotate_stdout_online) {\r
       service->stdout_pipe = si->hStdOutput = 0;\r
@@ -324,7 +324,7 @@ int get_output_handles(nssm_service_t *service, STARTUPINFO *si) {
     else {\r
       if (service->rotate_files) rotate_file(service->name, service->stderr_path, service->rotate_seconds, service->rotate_bytes_low, service->rotate_bytes_high, service->rotate_delay, service->stderr_copy_and_truncate);\r
       HANDLE stderr_handle = write_to_file(service->stderr_path, service->stderr_sharing, 0, service->stderr_disposition, service->stderr_flags);\r
-      if (! stderr_handle) return 7;\r
+      if (stderr_handle == INVALID_HANDLE_VALUE) return 7;\r
 \r
       if (service->rotate_files && service->rotate_stderr_online) {\r
         service->stderr_pipe = si->hStdError = 0;\r
@@ -570,7 +570,7 @@ unsigned long WINAPI log_and_rotate(void *arg) {
 \r
           /* Reopen. */\r
           logger->write_handle = write_to_file(logger->path, logger->sharing, 0, logger->disposition, logger->flags);\r
-          if (! logger->write_handle) {\r
+          if (logger->write_handle == INVALID_HANDLE_VALUE) {\r
             error = GetLastError();\r
             log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, logger->path, error_string(error), 0);\r
             /* Oh dear.  Now we can't log anything further. */\r