Dynamic Library abstraction. This makes the abstraction of a single dynamic
[oota-llvm.git] / lib / System / Win32 / DynamicLibrary.inc
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"
15
16 namespace llvm {
17 using namespace sys;
18
19 //===----------------------------------------------------------------------===//
20 //=== WARNING: Implementation here must contain only Win32 specific code 
21 //===          and must not be UNIX code
22 //===----------------------------------------------------------------------===//
23
24 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
25   handle = LoadLibrary(filename);
26
27   if (handle == 0) {
28     char Buffer[100];
29     // FIXME: This should use FormatMessage
30     sprintf(Buffer, "Windows error code %d\n", GetLastError());
31     throw std::string(Buffer);
32   }
33 }
34
35 DynamicLibrary::~DynamicLibrary() {
36   if (handle)
37     FreeLibrary(handle);
38 }
39
40 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
41   assert(handle !=0 && "Invalid DynamicLibrary handle");
42   return GetProcAddress(handle, symbolName);
43 }
44
45 }
46
47 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab