1111* 1) Currently, we're looking for DLLs named either sample.dll or sample32.dll
1212*
1313*****************************************************************************/
14+
1415#include <Windows.h>
1516#include <stdio.h>
1617#include <assert.h>
1718#include "sq.h"
1819
19- HANDLE tryLoading (TCHAR * prefix , TCHAR * baseName , TCHAR * postfix )
20+ static HANDLE
21+ tryLoading (TCHAR * prefix , TCHAR * baseName , TCHAR * postfix )
2022{
2123 TCHAR libName [300 ];
2224 HANDLE h ;
@@ -34,33 +36,42 @@ HANDLE tryLoading(TCHAR *prefix, TCHAR *baseName, TCHAR *postfix)
3436 return h ;
3537}
3638
37- /* Return the module entry for the given module name */
38- void * ioLoadModule (char * pluginName )
39+ /* Return the module entry for the given module name, or null if not found */
40+ void *
41+ ioLoadModule (char * pluginName )
3942{
4043 HANDLE handle ;
4144 TCHAR * name ;
42-
45+ int nameLen = pluginName ? (int )strlen (pluginName ) : 0 ;
46+ int endsInDLL = nameLen > 4 && !strcmp (pluginName + nameLen - 4 , ".dll" );
47+
4348#ifdef UNICODE
4449 int len = MultiByteToWideChar (CP_UTF8 , 0 , pluginName , -1 , NULL , 0 );
45- if (len <= 0 ) return 0 ; /* invalid UTF8 ? */
50+ if (len <= 0 )
51+ return 0 ; /* invalid UTF8 ? */
4652 name = alloca (len * sizeof (WCHAR ));
47- if (MultiByteToWideChar (CP_UTF8 , 0 , pluginName , -1 , name , len ) == 0 ) return 0 ;
53+ if (MultiByteToWideChar (CP_UTF8 , 0 , pluginName , -1 , name , len ) == 0 )
54+ return 0 ;
4855#else
4956 name = pluginName ;
5057#endif
5158
52- handle = tryLoading (TEXT ("" ),name ,TEXT ("" ));
53- if (handle ) return handle ;
54- handle = tryLoading (TEXT ("" ),name ,TEXT (".dll" ));
55- if (handle ) return handle ;
56- handle = tryLoading (TEXT ("" ),name ,TEXT ("32.dll" ));
57- if (handle ) return handle ;
58- handle = tryLoading (imagePath ,name ,TEXT ("" ));
59- if (handle ) return handle ;
60- handle = tryLoading (imagePath ,name ,TEXT (".dll" ));
61- if (handle ) return handle ;
62- handle = tryLoading (imagePath ,name ,TEXT ("32.dll" ));
63- if (handle ) return handle ;
59+ if ((handle = tryLoading (TEXT ("" ),name ,TEXT ("" ))))
60+ return handle ;
61+ if (!endsInDLL ) {
62+ if ((handle = tryLoading (TEXT ("" ),name ,TEXT (".dll" ))))
63+ return handle ;
64+ if ((handle = tryLoading (TEXT ("" ),name ,TEXT ("32.dll" ))))
65+ return handle ;
66+ }
67+ if ((handle = tryLoading (imagePath ,name ,TEXT ("" ))))
68+ return handle ;
69+ if (!endsInDLL ) {
70+ if ((handle = tryLoading (imagePath ,name ,TEXT (".dll" ))))
71+ return handle ;
72+ if ((handle = tryLoading (imagePath ,name ,TEXT ("32.dll" ))))
73+ return handle ;
74+ }
6475 return 0 ;
6576}
6677
@@ -94,7 +105,6 @@ ioFindExternalFunctionIn(char *lookupName, void *moduleHandle)
94105}
95106#endif /* SPURVM */
96107
97- sqInt ioFreeModule (void * moduleHandle )
98- {
99- return FreeLibrary ((HANDLE ) moduleHandle );
100- }
108+ sqInt
109+ ioFreeModule (void * moduleHandle )
110+ { return FreeLibrary ((HANDLE ) moduleHandle ); }
0 commit comments