Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 1 | //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the DynamicLibrary |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Win32.h" |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame^] | 15 | #include <windef.h> |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 16 | |
| 17 | namespace llvm { |
| 18 | using namespace sys; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | //=== WARNING: Implementation here must contain only Win32 specific code |
| 22 | //=== and must not be UNIX code |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame^] | 26 | handle = new HMODULE; |
| 27 | *((HMODULE*)handle) = LoadLibrary(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 28 | |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame^] | 29 | if (*((HMODULE*)handle) == 0) { |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 30 | char Buffer[100]; |
| 31 | // FIXME: This should use FormatMessage |
| 32 | sprintf(Buffer, "Windows error code %d\n", GetLastError()); |
| 33 | throw std::string(Buffer); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | DynamicLibrary::~DynamicLibrary() { |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame^] | 38 | assert(handle !=0 && "Invalid DynamicLibrary handle"); |
| 39 | if (*((HMODULE*)handle)) |
| 40 | FreeLibrary(*((HMODULE*)handle)); |
| 41 | delete (HMODULE*)handle; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
| 45 | assert(handle !=0 && "Invalid DynamicLibrary handle"); |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame^] | 46 | return (void*) GetProcAddress(*((HMODULE*)handle), symbolName); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } |
| 50 | |
| 51 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |