OpcUaCanOpen
CANopen OPC-UA server
CanLibLoaderWin.cpp
Go to the documentation of this file.
1 
23 #include "CanLibLoaderWin.h"
24 #include <string>
25 #include <stdexcept>
26 #include <sstream>
27 #include "LogIt.h"
28 
29 namespace CanModule
30 {
31 
32  typedef __declspec(dllimport) CCanAccess *f_CCanAccess();
33 
38  void CanLibLoaderWin::ErrorExit(LPTSTR lpszFunction)
39  {
40  LPVOID lpMsgBuf;
41  LPVOID lpDisplayBuf;
42  DWORD dw = GetLastError();
43 
44  FormatMessage(
45  FORMAT_MESSAGE_ALLOCATE_BUFFER |
46  FORMAT_MESSAGE_FROM_SYSTEM |
47  FORMAT_MESSAGE_IGNORE_INSERTS,
48  NULL,
49  dw,
50  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
51  (LPTSTR) &lpMsgBuf,
52  0, NULL );
53 
54  // Display the error message and exit the process
55  lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
56  (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
57  StringCchPrintf((LPTSTR)lpDisplayBuf,
58  LocalSize(lpDisplayBuf) / sizeof(TCHAR),
59  TEXT("%s failed with error %d: %s"),
60  lpszFunction, dw, lpMsgBuf);
61  MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
62 
63  LocalFree(lpMsgBuf);
64  LocalFree(lpDisplayBuf);
65  //ExitProcess(dw);
66  }
67 
68 
70  : CanLibLoader(libName), m_pDynamicLibrary(0)
71  {
72  LOG(Log::TRC, lh) << "inherited logItComponentHandle= " << lh;
73  dynamicallyLoadLib(libName);
74  }
75 
77  FreeLibrary(m_pDynamicLibrary);
78  }
89  {
90  std::ostringstream ss;
91  ss << libName << "can.dll";
92 
93  //LOG(Log::DBG) << "Proceeding to load library " << ss.str();
94  //m_pDynamicLibrary = ::LoadLibrary(ss.str().c_str());
95 
96  LOG(Log::DBG, lh) << "Proceeding to ExA load library " << ss.str();
97  m_pDynamicLibrary = ::LoadLibraryExA(ss.str().c_str(), NULL, 0x00000010 /* LOAD_IGNORE_CODE_AUTHZ_LEVEL */);
98 
99  //We check for errors while loading the library
100  if ( m_pDynamicLibrary != NULL ) {
101  LOG(Log::DBG, lh) << " loaded the dynamic library: [" << ss.str() << "]";
102  } else {
103  string msg = string(__FUNCTION__) + string("Error: could not load the dynamic library ") + ss.str();
104  LOG(Log::ERR, lh) << msg;
105  if ( ss.str() == "ancan.dll"){
106  LOG(Log::ERR, lh) << " WARNING: anagate vendor libs do not install on your system, they are just copied. Make sure the hidden dependend libs (i.e. AnaGateCan64.dll) are in your lib search path!";
107  LOG(Log::ERR, lh) << " WARNING: easiest solution: copy them into the same directory as the CANX-tester binary";
108  }
109 // ErrorExit(TEXT( "Error: could not load the dynamic library " ));
110  throw std::runtime_error( msg.c_str() );
111  }
112  }
113 
121  {
122  LOG(Log::TRC, lh) << __FUNCTION__ << ": Accessing method get getCanBusAccess";
123  f_CCanAccess *canAccess = (f_CCanAccess *)GetProcAddress(m_pDynamicLibrary, "getCanBusAccess");
124 
125  std::cout << __FILE__ << " " << __LINE__ << " " << __LINE__ << " CanAccess= 0x" << canAccess << std::endl;
126 
127 
128  // We check for errors again. If there is an error the library is released from memory.
129  if (!canAccess) {
130  throw std::runtime_error( string(__FUNCTION__) + string(": Error: could not locate the function"));
131  }
132  // We call the function getHalAccess we got from the library. This will give us a pointer to an object, wich we store.
133  LOG(Log::TRC, lh) << __FUNCTION__ << ": getCanBusAccess: got an object ptr from library, OK";
134 
135 
136  return (CCanAccess*)(canAccess());
137  }
138 }