[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 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 | |||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 11 | #include "base/base_export.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 12 | #include "build/build_config.h" |
13 | |||||
14 | #if defined(OS_WIN) | ||||
15 | #include <windows.h> | ||||
16 | #elif defined(OS_MACOSX) | ||||
[email protected] | 6770f2a | 2010-11-15 21:22:55 | [diff] [blame] | 17 | #import <CoreFoundation/CoreFoundation.h> |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 18 | #endif // OS_* |
19 | |||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 20 | #include "base/string16.h" |
21 | |||||
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 22 | // Macro useful for writing cross-platform function pointers. |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 23 | #if defined(OS_WIN) && !defined(CDECL) |
24 | #define CDECL __cdecl | ||||
25 | #else | ||||
26 | #define CDECL | ||||
27 | #endif | ||||
28 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 29 | namespace base { |
30 | |||||
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame^] | 31 | class FilePath; |
32 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 33 | #if defined(OS_WIN) |
34 | typedef HMODULE NativeLibrary; | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 35 | #elif defined(OS_MACOSX) |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 36 | enum NativeLibraryType { |
37 | BUNDLE, | ||||
38 | DYNAMIC_LIB | ||||
39 | }; | ||||
40 | struct NativeLibraryStruct { | ||||
41 | NativeLibraryType type; | ||||
[email protected] | d8921c43 | 2010-01-28 16:08:09 | [diff] [blame] | 42 | CFBundleRefNum bundle_resource_ref; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 43 | union { |
44 | CFBundleRef bundle; | ||||
45 | void* dylib; | ||||
46 | }; | ||||
47 | }; | ||||
48 | typedef NativeLibraryStruct* NativeLibrary; | ||||
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 49 | #elif defined(OS_POSIX) |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 50 | typedef void* NativeLibrary; |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 51 | #endif // OS_* |
52 | |||||
53 | // Loads a native library from disk. Release it with UnloadNativeLibrary when | ||||
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 54 | // you're done. Returns NULL on failure. |
55 | // If |err| is not NULL, it may be filled in with an error message on | ||||
56 | // error. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 57 | BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
58 | std::string* error); | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 59 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 60 | #if defined(OS_WIN) |
61 | // Loads a native library from disk. Release it with UnloadNativeLibrary when | ||||
62 | // you're done. | ||||
63 | // This function retrieves the LoadLibrary function exported from kernel32.dll | ||||
64 | // and calls it instead of directly calling the LoadLibrary function via the | ||||
65 | // import table. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 66 | BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically( |
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 67 | const FilePath& library_path); |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 68 | #endif // OS_WIN |
69 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 70 | // Unloads a native library. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 71 | BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 72 | |
73 | // Gets a function pointer from a native library. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 74 | BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
75 | const char* name); | ||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 76 | |
77 | // Returns the full platform specific name for a native library. | ||||
78 | // For example: | ||||
79 | // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, | ||||
80 | // "mylib.dylib" on Mac. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 81 | BASE_EXPORT string16 GetNativeLibraryName(const string16& name); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 82 | |
83 | } // namespace base | ||||
84 | |||||
85 | #endif // BASE_NATIVE_LIBRARY_H_ |