1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#ifndef _LOADPLUGIN_H_
#define _LOADPLUGIN_H_
 
 
#include<map>
//#include<helpers_s.h>
 
#ifdef _UNICODE
#define _tstring wstring
#else
#define _tstring string
#endif
 
typedef HRESULT (STDAPICALLTYPE *PDLLGETCLASSOBJECT)(const CLSID& clsid, const IID& iid, void** ppv);
 
class cLoadPlugin
{
public:
    cLoadPlugin()
    {
        m_plugins.clear();
        InitializeCriticalSection( &m_cs );
    }
    ~cLoadPlugin();
 
    bool LoadPlugin( const TCHAR* szDllPath , IID IInterface , void** pInterface );
 
 
 
 
private:
    HMODULE  GetModule( const TCHAR* szDllPath );
    _tstring& toLower( _tstring& sin );
    HMODULE isInList( _tstring& path );
    map<_tstring , HMODULE> m_plugins;
    CRITICAL_SECTION m_cs;
     
    void ecs()
    {
        EnterCriticalSection( &m_cs );
    }
    void lcs()
    {
        LeaveCriticalSection( &m_cs );
    }
};
 
 
 
cLoadPlugin::~cLoadPlugin()
{  
    map<_tstring , HMODULE>::iterator it;
 
    DeleteCriticalSection( &m_cs );
     
    for ( it=m_plugins.begin() ; it != m_plugins.end(); it++ )
    {
        FreeLibrary( (HMODULE)it->second );
    }
 
 
}
 
 
_tstring& cLoadPlugin::toLower( _tstring& sin )
{
    TCHAR cStart = L'A', cEnd=L'Z';
    for( unsigned int i=0; i < sin.length(); i++ )
    {
        if( sin[i] >= cStart && sin[i] <= cEnd )
        {
            sin[i] = sin[i]+32;
        }
 
    }
 
    return( sin );
}
 
HMODULE cLoadPlugin::isInList( _tstring& path )
{
    map<_tstring , HMODULE>::iterator it;
     
    it = m_plugins.find( path );
    if( it == m_plugins.end() )
        return( NULL );
    return( it->second );
}
 
bool cLoadPlugin::LoadPlugin( const TCHAR* szDllPath , IID IInterface , void** pInterface )
{
    ecs();
 
    HRESULT hr;
    HMODULE hMod = NULL;
    PDLLGETCLASSOBJECT proc = NULL;
    IClassFactory* pFactory = NULL;
 
    //Get the module from list or load it
    hMod = GetModule( szDllPath );
    if( !hMod )
    {
        lcs();
        return( false );
    }
 
    //Get Proc address
    proc = (PDLLGETCLASSOBJECT) GetProcAddress( hMod , "DllGetClassObject" );
    if( !proc )
    {
        lcs();
        return( false );
    }
 
    hr = proc( IInterface , IID_IClassFactory , (void**)&pFactory );
    if( hr || !pFactory )
    {
        lcs();
        return( false );
    }
 
    hr = pFactory->CreateInstance( NULL, IInterface , pInterface );
    if( hr || !pInterface )
    {
        lcs();
        return( false );
    }
 
    pFactory->Release();
     
    lcs();
    return( true );
}
 
 
HMODULE  cLoadPlugin::GetModule( const TCHAR* szDllPath )
{
    _tstring spath;
    _tstring mname;
    const TCHAR *pName = NULL, c = L'\\';
    HMODULE hMod = NULL;
    PDLLGETCLASSOBJECT proc = NULL;
 
     
    if( !szDllPath )
        return( NULL );
 
    //convet path to lowercase and check if file exist
    //If not a file
    spath = szDllPath;
    toLower( spath );
 
    //if( !IsFile( toLower( spath ).c_str() ) )
    //  return( false );
 
    //Is alread on list
    pName = _tcsrchr( spath.c_str() , c );
    if( !pName )
        pName = spath.c_str();
    else
        pName++;
    mname = pName;
 
    //Check if module already loaded
    hMod = isInList( mname );
 
    //if not on list load module and store it to list
    if( !hMod )
    {
        if( ( hMod = LoadLibrary( spath.c_str() ) ) == NULL )
            return( NULL );
 
         proc = (PDLLGETCLASSOBJECT) GetProcAddress( hMod , "DllGetClassObject" );
         if( !proc )
         {
             FreeLibrary( hMod );
             return( NULL );
         }
 
         //insert plugin
         m_plugins[mname] = hMod;
    }
 
    return( hMod );
}
 
#endif //_LOADPLUGIN_H_