MS Windows, mingw compatibility of recent snapshots

Holger Vogt <[email protected]>
Newsgroups gmane.comp.gnu.gnucap.devel
Message-ID <[email protected]>
Dear Al,

enclosed you will find an improved patch of c_attach.cc for MS Windows 
with correct error message handling, now tested with the gnucap att and 
det commands.

Regards

Holger


/* MS Windows portability added to c_attach.cc
  * DLLs may be loaded with the following patch
  */
//testing=none
#ifdef _WIN32
#include <windows.h>
#undef min
#undef max
#else
#include <dlfcn.h>
#endif

#include "ap.h"
#include "c_comand.h"

#ifdef _WIN32
/*--------------------------------------------------------------------------*/
static std::map<const std::string, HINSTANCE> attach_list;
/*--------------------------------------------------------------------------*/
void CMD::attach(CS& cmd)
{
   int here = cmd.cursor();
   std::string s;
   cmd >> s;

   LPVOID lpMsgBuf;
   DWORD dw;
   // load dll by its filename s (including path with backslashes)
   HINSTANCE handle = LoadLibrary(s.c_str());
   if (handle) {
     attach_list[s] = handle;
   }else{
     // get the error code
     dw = GetLastError();
     // get the corresponding error message
     FormatMessage(
         FORMAT_MESSAGE_ALLOCATE_BUFFER |
         FORMAT_MESSAGE_FROM_SYSTEM |
         FORMAT_MESSAGE_IGNORE_INSERTS,
         NULL,
         dw,
         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
         (LPTSTR) &lpMsgBuf,
         0, NULL );
     // send the error message
     cmd.warn(bERROR, here, (LPCTSTR)lpMsgBuf);
     // free the error message buffer
     LocalFree(lpMsgBuf);
   }
}
/*--------------------------------------------------------------------------*/
void CMD::detach(CS& cmd)
{
   int here = cmd.cursor();
   std::string s;
   cmd >> s;

   HINSTANCE handle = attach_list[s];
   if (handle) {
     FreeLibrary(handle);
     attach_list[s] = NULL;
   }else{
     cmd.warn(bERROR, here, "plugin not attached");
   }
}
#else
/*--------------------------------------------------------------------------*/
static std::map<const std::string, void*> attach_list;
/*--------------------------------------------------------------------------*/
void CMD::attach(CS& cmd)
{
   int here = cmd.cursor();
   std::string s;
   cmd >> s;

   void* handle = dlopen(s.c_str(), RTLD_NOW);
   if (handle) {
     attach_list[s] = handle;
   }else{
     cmd.warn(bERROR, here, dlerror());
   }
}
/*--------------------------------------------------------------------------*/
void CMD::detach(CS& cmd)
{
   int here = cmd.cursor();
   std::string s;
   cmd >> s;

   void* handle = attach_list[s];
   if (handle) {
     dlclose(handle);
     attach_list[s] = NULL;
   }else{
     cmd.warn(bERROR, here, "plugin not attached");
   }
}
#endif
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.