blob: d743454746ae173d9ac4bc9cb63e6a5606d6d698 [file] [log] [blame]
Reid Spencer0de02a62004-11-18 04:33:39 +00001//===- 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 Spencer7a453892004-11-20 23:30:55 +000015#include <windef.h>
Reid Spencer0de02a62004-11-18 04:33:39 +000016
17namespace llvm {
18using namespace sys;
19
20//===----------------------------------------------------------------------===//
21//=== WARNING: Implementation here must contain only Win32 specific code
22//=== and must not be UNIX code
23//===----------------------------------------------------------------------===//
24
Reid Spencer441cc2a2004-11-29 10:39:46 +000025DynamicLibrary::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 Spencer0de02a62004-11-18 04:33:39 +000034DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer7a453892004-11-20 23:30:55 +000035 handle = new HMODULE;
36 *((HMODULE*)handle) = LoadLibrary(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000037
Reid Spencer7a453892004-11-20 23:30:55 +000038 if (*((HMODULE*)handle) == 0) {
Reid Spencer441cc2a2004-11-29 10:39:46 +000039 ThrowError("Can't LoadLibrary: ");
Reid Spencer0de02a62004-11-18 04:33:39 +000040 }
41}
42
43DynamicLibrary::~DynamicLibrary() {
Reid Spencer7a453892004-11-20 23:30:55 +000044 assert(handle !=0 && "Invalid DynamicLibrary handle");
45 if (*((HMODULE*)handle))
46 FreeLibrary(*((HMODULE*)handle));
47 delete (HMODULE*)handle;
Reid Spencer0de02a62004-11-18 04:33:39 +000048}
49
50void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
51 assert(handle !=0 && "Invalid DynamicLibrary handle");
Reid Spencer7a453892004-11-20 23:30:55 +000052 return (void*) GetProcAddress(*((HMODULE*)handle), symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +000053}
54
55}
56
57// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab