blob: 9809c987c864f3a82dc96e881f18241000300694 [file] [log] [blame]
rvargas@google.come106ef62011-03-26 03:48:03 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
evan@chromium.orgda642ae2009-04-21 09:56:07 +09002// 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
rvargas@google.come106ef62011-03-26 03:48:03 +090012#include "base/base_api.h"
evan@chromium.orgda642ae2009-04-21 09:56:07 +090013#include "build/build_config.h"
14
15#if defined(OS_WIN)
16#include <windows.h>
17#elif defined(OS_MACOSX)
thomasvl@chromium.org1b721c82010-11-16 06:22:55 +090018#import <CoreFoundation/CoreFoundation.h>
evan@chromium.orgda642ae2009-04-21 09:56:07 +090019#endif // OS_*
20
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090021#include "base/string16.h"
22
evan@chromium.org9adf1192010-11-13 06:53:16 +090023// Macro useful for writing cross-platform function pointers.
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090024#if defined(OS_WIN) && !defined(CDECL)
25#define CDECL __cdecl
26#else
27#define CDECL
28#endif
29
evan@chromium.orgda642ae2009-04-21 09:56:07 +090030class FilePath;
31
32namespace base {
33
34#if defined(OS_WIN)
35typedef HMODULE NativeLibrary;
evan@chromium.orgda642ae2009-04-21 09:56:07 +090036#elif defined(OS_MACOSX)
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090037enum NativeLibraryType {
38 BUNDLE,
39 DYNAMIC_LIB
40};
41struct NativeLibraryStruct {
42 NativeLibraryType type;
stuartmorgan@chromium.orgc7f74a22010-01-29 01:08:09 +090043 CFBundleRefNum bundle_resource_ref;
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090044 union {
45 CFBundleRef bundle;
46 void* dylib;
47 };
48};
49typedef NativeLibraryStruct* NativeLibrary;
evan@chromium.org875bb6e2009-12-29 09:32:52 +090050#elif defined(OS_POSIX)
evan@chromium.orgda642ae2009-04-21 09:56:07 +090051typedef void* NativeLibrary;
evan@chromium.orgda642ae2009-04-21 09:56:07 +090052#endif // OS_*
53
54// Loads a native library from disk. Release it with UnloadNativeLibrary when
55// you're done.
rvargas@google.come106ef62011-03-26 03:48:03 +090056BASE_API NativeLibrary LoadNativeLibrary(const FilePath& library_path);
evan@chromium.orgda642ae2009-04-21 09:56:07 +090057
ananta@chromium.org4de229a2010-11-20 08:33:13 +090058#if defined(OS_WIN)
59// Loads a native library from disk. Release it with UnloadNativeLibrary when
60// you're done.
61// This function retrieves the LoadLibrary function exported from kernel32.dll
62// and calls it instead of directly calling the LoadLibrary function via the
63// import table.
rvargas@google.come106ef62011-03-26 03:48:03 +090064BASE_API NativeLibrary LoadNativeLibraryDynamically(
65 const FilePath& library_path);
ananta@chromium.org4de229a2010-11-20 08:33:13 +090066#endif // OS_WIN
67
evan@chromium.orgda642ae2009-04-21 09:56:07 +090068// Unloads a native library.
rvargas@google.come106ef62011-03-26 03:48:03 +090069BASE_API void UnloadNativeLibrary(NativeLibrary library);
evan@chromium.orgda642ae2009-04-21 09:56:07 +090070
71// Gets a function pointer from a native library.
rvargas@google.come106ef62011-03-26 03:48:03 +090072BASE_API void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
73 const char* name);
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090074
75// Returns the full platform specific name for a native library.
76// For example:
77// "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux,
78// "mylib.dylib" on Mac.
rvargas@google.come106ef62011-03-26 03:48:03 +090079BASE_API string16 GetNativeLibraryName(const string16& name);
evan@chromium.orgda642ae2009-04-21 09:56:07 +090080
81} // namespace base
82
83#endif // BASE_NATIVE_LIBRARY_H_