1 //===- Win32/DynamicLibrary.cpp - Win32 DL Implementation -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Jeff Cohen and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of DynamicLibrary.
12 //===----------------------------------------------------------------------===//
22 #pragma comment(lib, "dbghelp.lib")
27 //===----------------------------------------------------------------------===//
28 //=== WARNING: Implementation here must contain only Win32 specific code
29 //=== and must not be UNIX code.
30 //===----------------------------------------------------------------------===//
32 static std::vector<HMODULE> OpenedHandles;
35 static BOOL CALLBACK ELM_Callback(PSTR ModuleName,
40 // Ignore VC++ runtimes prior to 7.1. Somehow some of them get loaded
42 if (stricmp(ModuleName, "msvci70") != 0 &&
43 stricmp(ModuleName, "msvcirt") != 0 &&
44 stricmp(ModuleName, "msvcp50") != 0 &&
45 stricmp(ModuleName, "msvcp60") != 0 &&
46 stricmp(ModuleName, "msvcp70") != 0 &&
47 stricmp(ModuleName, "msvcr70") != 0 &&
48 stricmp(ModuleName, "msvcrt") != 0 &&
49 stricmp(ModuleName, "msvcrt20") != 0 &&
50 stricmp(ModuleName, "msvcrt40") != 0) {
51 OpenedHandles.push_back((HMODULE)ModuleBase);
57 DynamicLibrary::DynamicLibrary() : handle(0) {
58 handle = GetModuleHandle(NULL);
59 OpenedHandles.push_back((HMODULE)handle);
62 DynamicLibrary::DynamicLibrary(const char*filename) : handle(0) {
63 HMODULE a_handle = LoadLibrary(filename);
66 ThrowError(std::string(filename) + ": Can't open : ");
69 OpenedHandles.push_back(a_handle);
72 DynamicLibrary::~DynamicLibrary() {
76 // GetModuleHandle() does not increment the ref count, so we must not free
77 // the handle to the executable.
78 if (handle != GetModuleHandle(NULL))
79 FreeLibrary((HMODULE)handle);
82 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
83 E = OpenedHandles.end(); I != E; ++I) {
85 // Note: don't use the swap/pop_back trick here. Order is important.
86 OpenedHandles.erase(I);
91 void DynamicLibrary::LoadLibraryPermanently(const char* filename) {
93 HMODULE a_handle = LoadLibrary(filename);
96 ThrowError(std::string(filename) + ": Can't open : ");
98 OpenedHandles.push_back(a_handle);
100 // When no file is specified, enumerate all DLLs and EXEs in the
102 EnumerateLoadedModules(GetCurrentProcess(), ELM_Callback, 0);
105 // Because we don't remember the handle, we will never free it; hence,
106 // it is loaded permanently.
109 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) {
110 for (std::vector<HMODULE>::iterator I = OpenedHandles.begin(),
111 E = OpenedHandles.end(); I != E; ++I) {
112 FARPROC ptr = GetProcAddress((HMODULE)*I, symbolName);
120 void *DynamicLibrary::GetAddressOfSymbol(const char *symbolName) {
121 assert(handle != 0 && "Invalid DynamicLibrary handle");
122 return (void *) GetProcAddress((HMODULE)handle, symbolName);
127 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab