File: link_dll_lib.cpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (91 lines) | stat: -rw-r--r-- 2,577 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-23 Bradley M. Bell
// ----------------------------------------------------------------------------
# include <cppad/utility/to_string.hpp>
# include <cppad/utility/link_dll_lib.hpp>

# ifndef _WIN32
//  dlopen, dlsym, dlerror, dlclose, RTLD_LAZY
# include <dlfcn.h>
# else
//  LoadLibrary, GetProcAddress, FreeLibrary, GetLastError, RTLD_LAZY
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
# define RTLD_LAZY 0
# endif

namespace CppAD { // BEGIN_CPPAD_NAMESPACE

# ifdef _WIN32
//
//  dlopen
void* link_dll_lib::dlopen(const char *filename, int flag)
{  HINSTANCE hinstance = LoadLibraryA(filename);
   return reinterpret_cast<void*>( hinstance );
}
// dlsym
void* link_dll_lib::dlsym(void* handle, const char* symbol)
{  HINSTANCE hinstance = reinterpret_cast<HINSTANCE>( handle );
   FARPROC   farproc   = GetProcAddress(hinstance, symbol);
   return reinterpret_cast<void*>( farproc );
}
// dlclose
int link_dll_lib::dlclose(void* handle)
{  HINSTANCE hinstance = reinterpret_cast<HINSTANCE>( handle );
   int result          = FreeLibrary(hinstance);
   return result;
}
// dlerror
const char* link_dll_lib::dlerror(void)
{  // should have a different result for each thread
   static char result[100];
   //
   DWORD dw        = GetLastError();
   std::string str = CppAD::to_string(dw);
   str             = "Microsoft GetLastError() = " + str;
   size_t  i = 0;
   while(i < 99 && i < str.size())
      result[i] = str[i];
   result[i] = '\0';
   //
   return result;
}
# endif
//
link_dll_lib::link_dll_lib(const std::string& dll_file, std::string& err_msg)
{  handle_ = dlopen(dll_file.c_str(), RTLD_LAZY);
   if( handle_ != nullptr )
      err_msg = "";
   else
   {  const char *err_str = dlerror();
      err_msg = "Error opening dll_file =" + dll_file;
      if( err_str != nullptr )
      {  err_msg += "\n";
         err_msg += err_str;
      }
   }
   ctor_err_msg_ = err_msg;
}
link_dll_lib::~link_dll_lib(void)
{  if( handle_ != nullptr )
      dlclose(handle_);
}
void* link_dll_lib::operator()
(const std::string& function_name, std::string& err_msg) const
// END_OPERATOR
{  if( handle_ == nullptr )
   {  err_msg = ctor_err_msg_;
      return nullptr;
   }
   void* fun_ptr = dlsym(handle_, function_name.c_str());
   if( fun_ptr == nullptr )
      err_msg = "Error finding function_name = " + function_name;
   else
      err_msg = "";
   return fun_ptr;
}

} // END_CPPAD_NAMESPACE