blob: 6afd06d6ab9ee85f2cd9948c3a0594256d0db23d [file] [log] [blame]
evan@chromium.orgda642ae2009-04-21 09:56:07 +09001// Copyright (c) 2009 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_
thakis@chromium.org01d14522010-07-27 08:08:24 +09007#pragma once
evan@chromium.orgda642ae2009-04-21 09:56:07 +09008
9// This file defines a cross-platform "NativeLibrary" type which represents
10// a loadable module.
11
12#include "build/build_config.h"
13
14#if defined(OS_WIN)
15#include <windows.h>
16#elif defined(OS_MACOSX)
thomasvl@chromium.org1b721c82010-11-16 06:22:55 +090017#import <CoreFoundation/CoreFoundation.h>
evan@chromium.orgda642ae2009-04-21 09:56:07 +090018#endif // OS_*
19
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090020#include "base/string16.h"
21
evan@chromium.org9adf1192010-11-13 06:53:16 +090022// Macro useful for writing cross-platform function pointers.
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090023#if defined(OS_WIN) && !defined(CDECL)
24#define CDECL __cdecl
25#else
26#define CDECL
27#endif
28
evan@chromium.orgda642ae2009-04-21 09:56:07 +090029class FilePath;
30
31namespace base {
32
33#if defined(OS_WIN)
34typedef HMODULE NativeLibrary;
evan@chromium.orgda642ae2009-04-21 09:56:07 +090035#elif defined(OS_MACOSX)
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090036enum NativeLibraryType {
37 BUNDLE,
38 DYNAMIC_LIB
39};
40struct NativeLibraryStruct {
41 NativeLibraryType type;
stuartmorgan@chromium.orgc7f74a22010-01-29 01:08:09 +090042 CFBundleRefNum bundle_resource_ref;
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090043 union {
44 CFBundleRef bundle;
45 void* dylib;
46 };
47};
48typedef NativeLibraryStruct* NativeLibrary;
evan@chromium.org875bb6e2009-12-29 09:32:52 +090049#elif defined(OS_POSIX)
evan@chromium.orgda642ae2009-04-21 09:56:07 +090050typedef void* NativeLibrary;
evan@chromium.orgda642ae2009-04-21 09:56:07 +090051#endif // OS_*
52
53// Loads a native library from disk. Release it with UnloadNativeLibrary when
54// you're done.
55NativeLibrary LoadNativeLibrary(const FilePath& library_path);
56
ananta@chromium.org4de229a2010-11-20 08:33:13 +090057#if defined(OS_WIN)
58// Loads a native library from disk. Release it with UnloadNativeLibrary when
59// you're done.
60// This function retrieves the LoadLibrary function exported from kernel32.dll
61// and calls it instead of directly calling the LoadLibrary function via the
62// import table.
63NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path);
64#endif // OS_WIN
65
evan@chromium.orgda642ae2009-04-21 09:56:07 +090066// Unloads a native library.
67void UnloadNativeLibrary(NativeLibrary library);
68
69// Gets a function pointer from a native library.
70void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090071 const char* name);
72
73// Returns the full platform specific name for a native library.
74// For example:
75// "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux,
76// "mylib.dylib" on Mac.
77string16 GetNativeLibraryName(const string16& name);
evan@chromium.orgda642ae2009-04-21 09:56:07 +090078
79} // namespace base
80
81#endif // BASE_NATIVE_LIBRARY_H_