blob: b2add2ffe90ffbd699ef0cc08804e3505bb37882 [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
25DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
Reid Spencer7a453892004-11-20 23:30:55 +000026 handle = new HMODULE;
27 *((HMODULE*)handle) = LoadLibrary(filename);
Reid Spencer0de02a62004-11-18 04:33:39 +000028
Reid Spencer7a453892004-11-20 23:30:55 +000029 if (*((HMODULE*)handle) == 0) {
Reid Spencer0de02a62004-11-18 04:33:39 +000030 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
37DynamicLibrary::~DynamicLibrary() {
Reid Spencer7a453892004-11-20 23:30:55 +000038 assert(handle !=0 && "Invalid DynamicLibrary handle");
39 if (*((HMODULE*)handle))
40 FreeLibrary(*((HMODULE*)handle));
41 delete (HMODULE*)handle;
Reid Spencer0de02a62004-11-18 04:33:39 +000042}
43
44void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
45 assert(handle !=0 && "Invalid DynamicLibrary handle");
Reid Spencer7a453892004-11-20 23:30:55 +000046 return (void*) GetProcAddress(*((HMODULE*)handle), symbolName);
Reid Spencer0de02a62004-11-18 04:33:39 +000047}
48
49}
50
51// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab