1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_NATIVE_LIBRARY_H_
6 #define BASE_NATIVE_LIBRARY_H_
7 
8 // This file defines a cross-platform "NativeLibrary" type which represents
9 // a loadable module.
10 
11 #include <string>
12 
13 #include "../base/base_export.h"
14 #include "../base/compiler_specific.h"
15 //#include "base/strings/string16.h"
16 #include "../build/build_config.h"
17 
18 #if defined(OS_WIN)
19 #include <windows.h>
20 #elif defined(OS_MACOSX)
21 #import <CoreFoundation/CoreFoundation.h>
22 #endif  // OS_*
23 
24 namespace base {
25 
26 const char *int2char(int value, char* buffer);
27 
28 //class FilePath;
29 
30 #if defined(OS_WIN)
31 typedef HMODULE NativeLibrary;
32 #elif defined(OS_MACOSX)
33 enum NativeLibraryType {
34   BUNDLE,
35   DYNAMIC_LIB
36 };
37 enum NativeLibraryObjCStatus {
38   OBJC_UNKNOWN,
39   OBJC_PRESENT,
40   OBJC_NOT_PRESENT,
41 };
42 struct NativeLibraryStruct {
43   NativeLibraryType type;
44   CFBundleRefNum bundle_resource_ref;
45   NativeLibraryObjCStatus objc_status;
46   union {
47     CFBundleRef bundle;
48     void* dylib;
49   };
50 };
51 typedef NativeLibraryStruct* NativeLibrary;
52 #elif defined(OS_POSIX)
53 typedef void* NativeLibrary;
54 #endif  // OS_*
55 
56 struct BASE_EXPORT NativeLibraryLoadError {
57 #if defined(OS_WIN)
NativeLibraryLoadErrorNativeLibraryLoadError58   NativeLibraryLoadError() : code(0) {}
59 #endif  // OS_WIN
60 
61   // Returns a string representation of the load error.
62   std::string ToString() const;
63 
64 #if defined(OS_WIN)
65   DWORD code;
66 #else
67   std::string message;
68 #endif  // OS_WIN
69 };
70 
71 // Loads a native library from disk.  Release it with UnloadNativeLibrary when
72 // you're done.  Returns NULL on failure.
73 // If |error| is not NULL, it may be filled in on load error.
74 BASE_EXPORT NativeLibrary LoadNativeLibrary(const std::string& library_path,
75                                             NativeLibraryLoadError* error);
76 
77 #if defined(OS_WIN)
78 // Loads a native library from disk.  Release it with UnloadNativeLibrary when
79 // you're done.
80 // This function retrieves the LoadLibrary function exported from kernel32.dll
81 // and calls it instead of directly calling the LoadLibrary function via the
82 // import table.
83 BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically(
84     const std::string& library_path);
85 #endif  // OS_WIN
86 
87 // Unloads a native library.
88 BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library);
89 
90 // Gets a function pointer from a native library.
91 BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
92                                                       const char* name);
93 
94 // Returns the full platform specific name for a native library.
95 // For example:
96 // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux,
97 // "mylib.dylib" on Mac.
98 //BASE_EXPORT string16 GetNativeLibraryName(const string16& name);
99 
100 }  // namespace base
101 
102 #endif  // BASE_NATIVE_LIBRARY_H_
103