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 | |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 25 | DynamicLibrary::DynamicLibrary() : handle(0) { |
| 26 | handle = new HMODULE; |
| 27 | *((HMODULE*)handle) = GetModuleHandle(NULL); |
| 28 | |
| 29 | if (*((HMODULE*)handle) == 0) { |
| 30 | ThrowError("Can't GetModuleHandle: "); |
| 31 | } |
| 32 | } |
| 33 | |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 34 | DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) { |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame] | 35 | handle = new HMODULE; |
| 36 | *((HMODULE*)handle) = LoadLibrary(filename); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 37 | |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame] | 38 | if (*((HMODULE*)handle) == 0) { |
Reid Spencer | 441cc2a | 2004-11-29 10:39:46 +0000 | [diff] [blame] | 39 | ThrowError("Can't LoadLibrary: "); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | DynamicLibrary::~DynamicLibrary() { |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame] | 44 | assert(handle !=0 && "Invalid DynamicLibrary handle"); |
| 45 | if (*((HMODULE*)handle)) |
| 46 | FreeLibrary(*((HMODULE*)handle)); |
| 47 | delete (HMODULE*)handle; |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) { |
| 51 | assert(handle !=0 && "Invalid DynamicLibrary handle"); |
Reid Spencer | 7a45389 | 2004-11-20 23:30:55 +0000 | [diff] [blame] | 52 | return (void*) GetProcAddress(*((HMODULE*)handle), symbolName); |
Reid Spencer | 0de02a6 | 2004-11-18 04:33:39 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | } |
| 56 | |
| 57 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |