90a216e51a54663ed9b59828c44c11ac5f030de1
[scilab.git] / scilab / libs / dynamiclibrary / src / dynamiclibrary_windows.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Allan CORNET
4  * 
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at    
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 #include <string.h>
13 #include "dynamiclibrary_windows.h"
14 #include "charEncoding.h"
15 #include "MALLOC.h"
16 /*---------------------------------------------------------------------------*/
17 IMPORT_EXPORT_DYNAMICLIBRARY_DLL DynLibHandle LoadDynLibraryW(wchar_t *libname)
18 {
19         return (DynLibHandle) LoadLibraryW(libname);
20 }
21 /*---------------------------------------------------------------------------*/
22 IMPORT_EXPORT_DYNAMICLIBRARY_DLL DynLibHandle LoadDynLibrary(char *libname)
23 {
24         return (DynLibHandle) LoadLibrary(libname);
25 }
26 /*---------------------------------------------------------------------------*/
27 IMPORT_EXPORT_DYNAMICLIBRARY_DLL BOOL FreeDynLibrary(DynLibHandle hInstance)
28 {
29         return (BOOL) FreeLibrary((HMODULE) hInstance);
30 }
31 /*---------------------------------------------------------------------------*/
32 IMPORT_EXPORT_DYNAMICLIBRARY_DLL DynLibFuncPtr GetDynLibFuncPtr(DynLibHandle _hLib, char* _pstEntryPointName)
33 {
34         DynLibFuncPtr retFuncPtr = NULL;
35         if (_hLib)
36         {
37                 retFuncPtr = GetProcAddress(_hLib, _pstEntryPointName);
38         }
39         
40         return retFuncPtr;
41 }
42 /*---------------------------------------------------------------------------*/
43 IMPORT_EXPORT_DYNAMICLIBRARY_DLL DynLibFuncPtr GetDynLibFuncPtrW(DynLibHandle _hLib, wchar_t* _pwstEntryPointName)
44 {
45     DynLibFuncPtr retFuncPtr = NULL;
46     if (_hLib)
47     {
48         char* pstEntryPoint = wide_string_to_UTF8(_pwstEntryPointName);
49         retFuncPtr = GetProcAddress(_hLib, pstEntryPoint);
50         FREE(pstEntryPoint);
51     }
52     return retFuncPtr;
53 }
54 /*---------------------------------------------------------------------------*/
55 IMPORT_EXPORT_DYNAMICLIBRARY_DLL wchar_t* GetLastDynLibError(void)
56 {
57         static wchar_t buffer[512];
58         DWORD dw = GetLastError(); 
59         DWORD source = 0;
60
61         if (dw == 0)
62         {
63                 wcscpy(buffer, L"Unknown Error");
64         }
65     else if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
66                         FORMAT_MESSAGE_IGNORE_INSERTS, &source, dw, 0,
67                         buffer, 512, NULL) == 0) 
68         {
69                         wcscpy(buffer, L"Unknown Error");
70         }
71
72         return buffer;
73 }
74 /*---------------------------------------------------------------------------*/