Torque3D Documentation / _generateds / winDlibrary.cpp

winDlibrary.cpp

Engine/source/platformWin32/winDlibrary.cpp

More...

Classes:

Public Defines

define

Public Functions

OsLoadLibrary(const char * file)

Load a library Returns 0 if the library fails to load.

Detailed Description

Public Defines

NOMINMAX() 
WIN32_LEAN_AND_MEAN() 

Public Functions

OsLoadLibrary(const char * file)

Load a library Returns 0 if the library fails to load.

Symbols are resolved through the DLibrary interface.

 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2012 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#define NOMINMAX
25#define WIN32_LEAN_AND_MEAN
26#include <windows.h>
27
28#include "platform/types.h"
29#include "platform/platformDlibrary.h"
30
31class Win32DLibrary: public DLibrary
32{
33   HMODULE _handle;
34public:
35   Win32DLibrary();
36   ~Win32DLibrary();
37   bool open(const char* file);
38   void close();
39   void *bind(const char *name);
40};
41
42Win32DLibrary::Win32DLibrary()
43{
44   _handle = 0;
45}
46
47Win32DLibrary::~Win32DLibrary()
48{
49   close();
50}
51
52bool Win32DLibrary::open(const char* file)
53{
54   // dlopen should also include the RTLD_LOCAL flag, but it seems to be
55   // missing from cygwin
56   _handle = LoadLibraryA(file);
57   if (!_handle)
58      return false;
59   bool (*open)() = (bool(*)())bind("dllopen");
60   if (open && !(*open)()) {
61      FreeLibrary(_handle);
62      _handle = 0;
63      return false;
64   }
65   return true;
66}
67
68void Win32DLibrary::close()
69{
70   if (_handle) {
71      void (*close)() = (void(*)())bind("dllclose");
72      if (close)
73         (*close)();
74      FreeLibrary(_handle);
75      _handle = 0;
76   }
77}
78
79void* Win32DLibrary::bind(const char *name)
80{
81   return _handle? (void*)GetProcAddress(_handle,name): 0;
82}
83
84DLibraryRef OsLoadLibrary(const char* file)
85{
86   Win32DLibrary* library = new Win32DLibrary();
87   if (!library->open(file)) {
88      delete library;
89      return 0;
90   }
91   return library;
92}
93
94