Cast the void* handle data member to HMODULE* to keep the VC++ compiler
[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 #include <windef.h>
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) {
26   handle = new HMODULE;
27   *((HMODULE*)handle) = LoadLibrary(filename);
28
29   if (*((HMODULE*)handle) == 0) {
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() {
38   assert(handle !=0 && "Invalid DynamicLibrary handle");
39   if (*((HMODULE*)handle))
40     FreeLibrary(*((HMODULE*)handle));
41   delete (HMODULE*)handle;
42 }
43
44 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
45   assert(handle !=0 && "Invalid DynamicLibrary handle");
46   return (void*) GetProcAddress(*((HMODULE*)handle), symbolName);
47 }
48
49 }
50
51 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab