changeset 38:a43d7a2867d2

Tidy up error handling, especially on Windows
author Chris Cannam
date Tue, 28 Aug 2018 14:33:41 +0100
parents 3ccc384c0161
children a28b19b136e8 40c6936c2fc9
files src/helper.cpp
diffstat 1 files changed, 21 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/helper.cpp	Mon Jun 11 17:46:50 2018 +0100
+++ b/src/helper.cpp	Tue Aug 28 14:33:41 2018 +0100
@@ -69,8 +69,15 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <process.h>
+#endif
+
 #include <string>
-#ifdef UNICODE
+#include <iostream>
+
+#ifdef _WIN32
+#ifndef UNICODE
+#error "This must be compiled with UNICODE defined"
+#endif
 static std::string lastLibraryName = "";
 static HMODULE LoadLibraryUTF8(std::string name) {
     lastLibraryName = name;
@@ -84,16 +91,16 @@
     return h;
 }
 static std::string GetErrorText() {
+    DWORD err = GetLastError();
     wchar_t *buffer;
-    DWORD err = GetLastError();
-    FormatMessage(
+    FormatMessageW(
         FORMAT_MESSAGE_ALLOCATE_BUFFER |
         FORMAT_MESSAGE_FROM_SYSTEM |
         FORMAT_MESSAGE_IGNORE_INSERTS,
         NULL,
         err,
         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-        (LPTSTR) &buffer,
+        (LPWSTR) &buffer,
         0, NULL );
     int wn = wcslen(buffer);
     int n = WideCharToMultiByte(CP_UTF8, 0, buffer, wn, 0, 0, 0, 0);
@@ -120,10 +127,6 @@
     return s;
 }
 #define DLOPEN(a,b)  LoadLibraryUTF8(a)
-#else
-#define DLOPEN(a,b)  LoadLibrary((a).c_str())
-#define GetErrorText() ""
-#endif
 #define DLSYM(a,b)   (void *)GetProcAddress((HINSTANCE)(a),(b).c_str())
 #define DLCLOSE(a)   (!FreeLibrary((HINSTANCE)(a)))
 #define DLERROR()    (GetErrorText())
@@ -135,9 +138,6 @@
 #define DLERROR()    dlerror()
 #endif
 
-#include <string>
-#include <iostream>
-
 //#include <unistd.h>
 
 using namespace std;
@@ -156,7 +156,6 @@
     unsigned long index = 0;
     while (fn(index)) ++index;
     if (index == 0) return "Library contains no plugins";
-//    else cerr << "Library contains " << index << " plugin(s)" << endl;
     return "";
 }
 
@@ -167,7 +166,6 @@
     unsigned int index = 0;
     while (fn(2, index)) ++index;
     if (index == 0) return "Library contains no plugins";
-//    else cerr << "Library contains " << index << " plugin(s)" << endl;
     return "";
 }
 
@@ -178,24 +176,26 @@
         return "Unable to open plugin library: " + error();
     }
 
+    string msg = "";
+    
     void *fn = DLSYM(handle, descriptor);
     if (!fn) {
-        return "Failed to find plugin descriptor " + descriptor +
+        msg = "Failed to find plugin descriptor " + descriptor +
             " in library: " + error();
-    }
-
-    if (descriptor == "ladspa_descriptor") {
-        return checkLADSPAStyleDescriptorFn(fn);
+    } else if (descriptor == "ladspa_descriptor") {
+        msg = checkLADSPAStyleDescriptorFn(fn);
     } else if (descriptor == "dssi_descriptor") {
-        return checkLADSPAStyleDescriptorFn(fn);
+        msg = checkLADSPAStyleDescriptorFn(fn);
     } else if (descriptor == "vampGetPluginDescriptor") {
-        return checkVampDescriptorFn(fn);
+        msg = checkVampDescriptorFn(fn);
     } else {
         cerr << "Note: no descriptor logic known for descriptor function \""
              << descriptor << "\"; not actually calling it" << endl;
     }
+
+    DLCLOSE(handle);
     
-    return "";
+    return msg;
 }
 
 int main(int argc, char **argv)