OpcUaCanOpen
CANopen OPC-UA server
dlcanbus.cpp
Go to the documentation of this file.
1 // This is the main DLL file.
2 
3 #include "dlcanbus.h"
4 #include "UaCanTrace.h"
5 
6 /*
7  * dlcanbus.cpp
8  *
9  * Created on: Apr 4, 2011
10  * Author: vfilimon
11  */
12 
13 #ifndef WIN32
14 #include "dlfcn.h"
15 //#include <windows.h> //I've ommited this line.
16 #endif
17 
19 
20  p_libComponent = NULL;
21  maker_CanAccessObj = NULL;
22 // if (!openInterface(name) )
23 // p_canObj = NULL;
24 }
25 
27 
28 // if (p_canObj)
29 // delete p_canObj;
30 #ifdef WIN32
31  FreeLibrary(p_libComponent);
32 #else
33  dlclose(p_libComponent);
34 #endif
35 }
36 
37 bool dlcanbus::openInterface(char *ncomponent)
38 {
39  char name[BUF_SIZE];
40 
41  componentName = ncomponent;
42 #ifdef WIN32
43  wchar_t wname[2*BUF_SIZE];
44  sprintf_s(name,BUF_SIZE,"%scan.dll",ncomponent);
45  size_t len = strlen(name)+1;
46  // wchar_t *wname = new wchar_t[len];
47  memset(wname,0,2*len);
48  ::MultiByteToWideChar( CP_ACP, NULL,name, -1, wname,(int)len );
49 
50  p_libComponent = LoadLibrary(wname);
51  // delete []wname;
52 
53  if (p_libComponent == 0) {
54  long err = GetLastError();
55  LOG(Log::INF, CanOpen::UaCanTrace::NoTrace) << "Cannot create component " << name << " " << err;
56  cout << "Cannot create component " << name << " " << err << endl;
57  return false;
58  }
59  maker_CanAccessObj = (create_canObj *)GetProcAddress(p_libComponent,"getCanbusAccess");
60  if (!maker_CanAccessObj)
61  {
62  long err = GetLastError();
63  LOG(Log::INF, CanOpen::UaCanTrace::NoTrace) << "Cannot create component " << name << " " << err;
64  FreeLibrary(p_libComponent);
65  }
66 #endif
67 #ifndef WIN32
68  const char * err;
69  sprintf(name,"lib%scan.so",ncomponent);
70  p_libComponent = dlopen(name, RTLD_NOW);
71 
72  if (p_libComponent == 0) {
73  char *err = dlerror();
74  if (err) {
75  LOG(Log::INF, CanOpen::UaCanTrace::NoTrace) << "dlopen " << err ;
76 
77  cout << "dlopen error" << err << endl;
78  return false;
79  }
80  }
81 
82  maker_CanAccessObj = (create_canObj *)dlsym(p_libComponent,"getCanbusAccess");
83 
84  err = dlerror();
85  if (err) {
86  LOG(Log::INF, CanOpen::UaCanTrace::NoTrace) << "dlopen " << err ;
87  cout << "dlsym " << err << endl;
88  return false;
89  }
90  if (!maker_CanAccessObj)
91  {
92  dlclose(p_libComponent);
93  LOG(Log::INF, CanOpen::UaCanTrace::NoTrace) << "Process load FAILED!";
94  cout << "Process load FAILED!" << endl;
95  return false;
96  }
97 
98 #endif
99  return true;
100 }