Home | History | Annotate | Download | only in base
      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 #pragma once
      8 
      9 // This file defines a cross-platform "NativeLibrary" type which represents
     10 // a loadable module.
     11 
     12 #include "base/base_api.h"
     13 #include "build/build_config.h"
     14 
     15 #if defined(OS_WIN)
     16 #include <windows.h>
     17 #elif defined(OS_MACOSX)
     18 #import <CoreFoundation/CoreFoundation.h>
     19 #endif  // OS_*
     20 
     21 #include "base/string16.h"
     22 
     23 // Macro useful for writing cross-platform function pointers.
     24 #if defined(OS_WIN) && !defined(CDECL)
     25 #define CDECL __cdecl
     26 #else
     27 #define CDECL
     28 #endif
     29 
     30 class FilePath;
     31 
     32 namespace base {
     33 
     34 #if defined(OS_WIN)
     35 typedef HMODULE NativeLibrary;
     36 #elif defined(OS_MACOSX)
     37 enum NativeLibraryType {
     38   BUNDLE,
     39   DYNAMIC_LIB
     40 };
     41 struct NativeLibraryStruct {
     42   NativeLibraryType type;
     43   CFBundleRefNum bundle_resource_ref;
     44   union {
     45     CFBundleRef bundle;
     46     void* dylib;
     47   };
     48 };
     49 typedef NativeLibraryStruct* NativeLibrary;
     50 #elif defined(OS_POSIX)
     51 typedef void* NativeLibrary;
     52 #endif  // OS_*
     53 
     54 // Loads a native library from disk.  Release it with UnloadNativeLibrary when
     55 // you're done.  Returns NULL on failure.
     56 // If |err| is not NULL, it may be filled in with an error message on
     57 // error.
     58 BASE_API NativeLibrary LoadNativeLibrary(const FilePath& library_path,
     59                                          std::string* error);
     60 
     61 #if defined(OS_WIN)
     62 // Loads a native library from disk.  Release it with UnloadNativeLibrary when
     63 // you're done.
     64 // This function retrieves the LoadLibrary function exported from kernel32.dll
     65 // and calls it instead of directly calling the LoadLibrary function via the
     66 // import table.
     67 BASE_API NativeLibrary LoadNativeLibraryDynamically(
     68     const FilePath& library_path);
     69 #endif  // OS_WIN
     70 
     71 // Unloads a native library.
     72 BASE_API void UnloadNativeLibrary(NativeLibrary library);
     73 
     74 // Gets a function pointer from a native library.
     75 BASE_API void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
     76                                                    const char* name);
     77 
     78 // Returns the full platform specific name for a native library.
     79 // For example:
     80 // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux,
     81 // "mylib.dylib" on Mac.
     82 BASE_API string16 GetNativeLibraryName(const string16& name);
     83 
     84 }  // namespace base
     85 
     86 #endif  // BASE_NATIVE_LIBRARY_H_
     87