gnucap-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnucap-devel] MS Windows, mingw compatibility of recent snapshots


From: Holger Vogt
Subject: [Gnucap-devel] MS Windows, mingw compatibility of recent snapshots
Date: Mon, 01 Jan 2007 11:34:50 +0100
User-agent: Thunderbird 1.5.0.9 (Windows/20061207)

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
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/






reply via email to

[Prev in Thread] Current Thread [Next in Thread]