Windows DLL Module Class I am doing a project where I use DLL's as run-time modules or plugins. To do this, I use the Windows LoadLibrary() and FreeLibrary() API calls to call two functions exported by the DLL. I decided to make a class so dealing with the DLL's would be easier. Is there anything I can add to my class to make it better, is there anything I'm doing wrong? Feel free to criticize anything you want. 

SaModule.h

#ifndef SAMODULE_H__
#define SAMODULE_H__

#include &lt;Windows.h&gt;
#include &lt;string&gt;
#include &lt;exception&gt;
#include "WinException.h"

class SaModule
{
private:

    typedef const char* (*LPFNGETNAME)();
    typedef bool (*LPFNDOSCAN)(std::string&amp;, std::string&amp;);
    HINSTANCE hInstance;
    std::string path;

public:

    SaModule();
    SaModule(const std::string&amp; path);
    ~SaModule();

    void Load(const std::string&amp; path);
    void Unload();

    LPFNDOSCAN DoScan;
    LPFNGETNAME GetName;

    HINSTANCE GetHandle() const;
    operator HINSTANCE() const;

};

#endif//SAMODULE_H__



SaModule.cpp

#include "SaModule.h"
#include "WinException.h"
#include &lt;sstream&gt;
#include &lt;iomanip&gt;

SaModule::SaModule(): path(), hInstance(NULL)
{
    //Nothing...
}

SaModule::SaModule(const std::string&amp; path): path(path), hInstance(NULL)
{
    Load(path);
}

SaModule::~SaModule()
{
    Unload();
}

void SaModule::Load(const std::string&amp; path)
{

    this-&gt;path = path;

    if( NULL == (hInstance=LoadLibrary(path.c_str())) )
        throw WinException();

    if( NULL == (GetName=reinterpret_cast&lt;LPFNGETNAME&gt;(GetProcAddress(hInstance,"GetName"))) )
        throw WinException();

    if( NULL == (DoScan=reinterpret_cast&lt;LPFNDOSCAN&gt;(GetProcAddress(hInstance, "DoScan"))) )
        throw WinException();

}

void SaModule::Unload()
{
    FreeLibrary(hInstance);
}

HINSTANCE SaModule::GetHandle() const
{
    return hInstance;
}

SaModule::operator HINSTANCE() const
{
    return hInstance;
}



WinException.h

#ifndef WINEXCEPTION_H__
#define WINEXCEPTION_H__

#include &lt;Windows.h&gt;
#include &lt;exception&gt;
#include &lt;string&gt;

class WinException: public std::exception
{
private:

    DWORD dwError;
    std::string errMsg;

public:

    WinException();
    const char* what() const;


};

#endif//WINEXCEPTION_H__



WinException.cpp

#include "WinException.h"
#include &lt;sstream&gt;
#include &lt;iomanip&gt;

WinException::WinException()
{

    std::ostringstream ss;
    dwError = GetLastError();   
    LPVOID lpMsgBuf = NULL;

    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL, dwError, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), reinterpret_cast&lt;LPTSTR&gt;(&amp;lpMsgBuf), 0, NULL);

    ss &lt;&lt; "Error: 0x" &lt;&lt; std::hex &lt;&lt; std::setfill('0') &lt;&lt; std::setw(8) &lt;&lt; dwError &lt;&lt; std::endl
        &lt;&lt; reinterpret_cast&lt;LPTSTR&gt;(lpMsgBuf);

    errMsg = ss.str();

    LocalFree(lpMsgBuf);

}

const char* WinException::what() const
{
    return errMsg.c_str();
}



Example Usage

#include &lt;iostream&gt;
#include "SaModule.h"

int main(int argc, char* argv[])
{

    try
    {
        SaModule mod("some.sa.dll");
        std::cout &lt;&lt; "Loaded Module: " &lt;&lt; mod.GetName() &lt;&lt; std::endl;
    }
    catch( WinException&amp; ex )
    {
        std::cout &lt;&lt; ex.what() &lt;&lt; std::endl;
    }

    return 0;

}
Just a couple of quick thoughts.


DoScan and GetName probably should be initialised to NULL as well.
Load doesn't call Unload for any previous loaded dlls. If Load should be called once only per SaModule, that should be detected.
A more informative exception could be thrown. "...during attempted insertion of plugin xyz", for example.
Beware of function name decoration, if that could be an issue.