DllWrapper.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. #include "DllWrapper.h"
  2. #include "Logger.h"
  3. #include <sstream>
  4. #include <iomanip>
  5. #include <algorithm>
  6. #define SDKDLL L"PCommMKit.dll"
  7. // 静态成员变量,用于在回调函数中访问类实例
  8. static DllWrapper *g_dll_wrapper_instance = nullptr;
  9. DllWrapper::DllWrapper()
  10. : dll_handle_(nullptr), init_monitor_(nullptr),
  11. start_monitor_(nullptr), stop_monitor_(nullptr),
  12. set_write_data_(nullptr), set_read_data_(nullptr),
  13. free_monitor_(nullptr), get_last_error_(nullptr)
  14. {
  15. // 设置静态实例指针
  16. g_dll_wrapper_instance = this;
  17. }
  18. DllWrapper::~DllWrapper()
  19. {
  20. Cleanup();
  21. g_dll_wrapper_instance = nullptr;
  22. }
  23. bool DllWrapper::Initialize()
  24. {
  25. if (initialized_)
  26. {
  27. return true;
  28. }
  29. // 获取可执行文件所在目录
  30. wchar_t buffer[MAX_PATH];
  31. GetModuleFileName(NULL, buffer, MAX_PATH);
  32. std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\");
  33. std::wstring exe_path = std::wstring(buffer).substr(0, pos);
  34. // 构建完整的DLL路径
  35. std::wstring full_dll_path = exe_path + L"\\" + SDKDLL;
  36. // 加载DLL
  37. dll_handle_ = LoadLibrary(full_dll_path.c_str());
  38. if (!dll_handle_)
  39. {
  40. LOG_ERROR("Failed to load DLL: " + std::to_string(GetLastError()));
  41. return false;
  42. }
  43. // 获取DLL函数地址
  44. init_monitor_ = (TInitMonitor)GetProcAddress(dll_handle_, "InitMonitor");
  45. start_monitor_ = (TMonitor)GetProcAddress(dll_handle_, "Monitor");
  46. stop_monitor_ = (TStop)GetProcAddress(dll_handle_, "Stop");
  47. set_write_data_ = (TSetWrite_Data)GetProcAddress(dll_handle_, "SetWrite_Data");
  48. set_read_data_ = (TSetRead_Data)GetProcAddress(dll_handle_, "SetRead_Data");
  49. free_monitor_ = (TFreeMonitor)GetProcAddress(dll_handle_, "FreeMonitor");
  50. get_last_error_ = (Tget_LastErrror)GetProcAddress(dll_handle_, "get_LastErrror");
  51. if (!init_monitor_ || !start_monitor_ || !stop_monitor_ ||
  52. !set_write_data_ || !set_read_data_ || !free_monitor_ || !get_last_error_)
  53. {
  54. LOG_ERROR("Failed to get function addresses from DLL");
  55. FreeLibrary(dll_handle_);
  56. dll_handle_ = nullptr;
  57. return false;
  58. }
  59. initialized_ = true;
  60. return true;
  61. }
  62. void DllWrapper::Cleanup()
  63. {
  64. if (dll_handle_)
  65. {
  66. FreeLibrary(dll_handle_);
  67. dll_handle_ = nullptr;
  68. }
  69. init_monitor_ = nullptr;
  70. start_monitor_ = nullptr;
  71. stop_monitor_ = nullptr;
  72. set_write_data_ = nullptr;
  73. set_read_data_ = nullptr;
  74. free_monitor_ = nullptr;
  75. get_last_error_ = nullptr;
  76. initialized_ = false;
  77. callback_initialized_ = false;
  78. }
  79. bool DllWrapper::InitMonitor(const std::string &key)
  80. {
  81. if (!initialized_)
  82. {
  83. LOG_ERROR("DLL not initialized");
  84. return false;
  85. }
  86. if (callback_initialized_)
  87. {
  88. return true;
  89. }
  90. // 将std::string转换为宽字符串
  91. std::wstring wide_key(key.begin(), key.end());
  92. // 调用DLL的InitMonitor函数
  93. callback_initialized_ = init_monitor_(wide_key.c_str(), OnMonitorEx);
  94. if (!callback_initialized_)
  95. {
  96. LOG_ERROR("Failed to initialize monitor: " + std::to_string(GetLastError()));
  97. return false;
  98. }
  99. return true;
  100. }
  101. bool DllWrapper::MonitorPort(int port)
  102. {
  103. if (!initialized_ || !callback_initialized_)
  104. {
  105. LOG_ERROR("DLL not initialized or monitor not initialized");
  106. return false;
  107. }
  108. // 调用DLL的Monitor函数
  109. bool result = start_monitor_(port);
  110. if (result)
  111. {
  112. std::lock_guard<std::mutex> lock(mutex_);
  113. monitored_ports_[port] = true;
  114. LOG_INFO("Started monitoring port: " + std::to_string(port));
  115. }
  116. else
  117. {
  118. LOG_ERROR("Failed to start monitoring port: " + std::to_string(port) + ", error: " + std::to_string(GetLastError()));
  119. }
  120. return result;
  121. }
  122. bool DllWrapper::StopPort(int port)
  123. {
  124. if (!initialized_)
  125. {
  126. LOG_ERROR("DLL not initialized");
  127. return false;
  128. }
  129. // 调用DLL的Stop函数
  130. bool result = stop_monitor_(port);
  131. if (result)
  132. {
  133. std::lock_guard<std::mutex> lock(mutex_);
  134. monitored_ports_.erase(port);
  135. LOG_INFO("Stopped monitoring port: " + std::to_string(port));
  136. }
  137. else
  138. {
  139. LOG_ERROR("Failed to stop monitoring port: " + std::to_string(port) + ", error: " + std::to_string(GetLastError()));
  140. }
  141. return result;
  142. }
  143. bool DllWrapper::ReadData(int port, const std::string &hex_data)
  144. {
  145. if (!initialized_)
  146. {
  147. LOG_ERROR("DLL not initialized");
  148. return false;
  149. }
  150. // 将十六进制字符串转换为字节数组
  151. std::vector<BYTE> bytes = Utils::ConvertHexStringToBytes(hex_data);
  152. // 调用DLL的SetRead_Data函数
  153. bool result = set_read_data_(port, reinterpret_cast<char *>(bytes.data()), static_cast<int>(bytes.size()));
  154. if (!result)
  155. {
  156. LOG_ERROR("Failed to set read data for port: " + std::to_string(port) + ", error: " + std::to_string(GetLastError()));
  157. }
  158. return result;
  159. }
  160. bool DllWrapper::WriteData(int port, const std::string &hex_data)
  161. {
  162. if (!initialized_)
  163. {
  164. LOG_ERROR("DLL not initialized");
  165. return false;
  166. }
  167. // 将十六进制字符串转换为字节数组
  168. std::vector<BYTE> bytes = Utils::ConvertHexStringToBytes(hex_data);
  169. // 调用DLL的SetWrite_Data函数
  170. bool result = set_write_data_(port, reinterpret_cast<char *>(bytes.data()), static_cast<int>(bytes.size()));
  171. if (!result)
  172. {
  173. LOG_ERROR("Failed to set write data for port: " + std::to_string(port) + ", error: " + std::to_string(GetLastError()));
  174. }
  175. return result;
  176. }
  177. bool DllWrapper::FreeMonitor()
  178. {
  179. if (!initialized_)
  180. {
  181. LOG_ERROR("DLL not initialized");
  182. return false;
  183. }
  184. // 调用DLL的FreeMonitor函数
  185. free_monitor_();
  186. // 清空监控端口列表
  187. std::lock_guard<std::mutex> lock(mutex_);
  188. monitored_ports_.clear();
  189. LOG_INFO("Freed monitor");
  190. return true;
  191. }
  192. int DllWrapper::GetLastError()
  193. {
  194. if (!initialized_)
  195. {
  196. LOG_ERROR("DLL not initialized");
  197. return -1;
  198. }
  199. // 调用DLL的get_LastErrror函数
  200. return get_last_error_();
  201. }
  202. void DllWrapper::SetEventCallback(CommEventCallback callback)
  203. {
  204. // 调用基类的实现来设置回调
  205. CommWrapperBase::SetEventCallback(callback);
  206. // 设置DLL事件回调,直接使用通用事件数据调用通用回调
  207. dll_event_callback_ = [this](const CommEventData &event_data)
  208. {
  209. // 直接使用通用事件数据调用通用回调
  210. if (event_callback_)
  211. {
  212. event_callback_(event_data);
  213. }
  214. };
  215. }
  216. std::string DllWrapper::GetErrorMessage(int error_code)
  217. {
  218. switch (error_code)
  219. {
  220. case -241:
  221. return "安装驱动失败";
  222. case -243:
  223. return "其他错误";
  224. case -244:
  225. return "驱动文件";
  226. case -255:
  227. return "绑定串口失败";
  228. case -254:
  229. return "第一次启动安装,必须以管理员权限运行";
  230. case -253:
  231. return "启动驱动服务失败";
  232. case -252:
  233. return "请检查Key是否有效";
  234. case -1:
  235. return "无效操作";
  236. case 0:
  237. return "成功";
  238. case 1:
  239. return "串口打开失败";
  240. case 2:
  241. return "串口配置失败";
  242. case 3:
  243. return "串口读取失败";
  244. case 4:
  245. return "串口写入失败";
  246. case 5:
  247. return "串口关闭失败";
  248. default:
  249. return "未知错误";
  250. }
  251. }
  252. bool DllWrapper::IsPortMonitored(int port)
  253. {
  254. std::lock_guard<std::mutex> lock(mutex_);
  255. auto it = monitored_ports_.find(port);
  256. return (it != monitored_ports_.end() && it->second);
  257. }
  258. // 静态回调函数,用于接收DLL事件
  259. LONG CALLBACK DllWrapper::OnMonitorEx(int Sequence, double dTime, DWORD ComNumber, DWORD Irp, DWORD dwSize, char *lpData)
  260. {
  261. // 检查是否有有效的类实例
  262. if (!g_dll_wrapper_instance)
  263. {
  264. return 0;
  265. }
  266. // 检查是否有设置回调函数
  267. if (!g_dll_wrapper_instance->event_callback_)
  268. {
  269. return 0;
  270. }
  271. try
  272. {
  273. // 创建CommEventData结构以传递给回调函数
  274. CommEventData event_data;
  275. event_data.event_type = static_cast<CommEventType>(Irp);
  276. event_data.port = static_cast<int>(ComNumber);
  277. if (lpData && dwSize > 0)
  278. {
  279. event_data.data.assign(reinterpret_cast<BYTE *>(lpData), reinterpret_cast<BYTE *>(lpData) + dwSize);
  280. }
  281. event_data.data_size = dwSize;
  282. // 调用用户设置的回调函数
  283. g_dll_wrapper_instance->event_callback_(event_data);
  284. LOG_DEBUG("DLL event processed successfully");
  285. }
  286. catch (const std::exception &e)
  287. {
  288. LOG_ERROR("Error processing DLL event: " + std::string(e.what()));
  289. return -1;
  290. }
  291. return 0;
  292. }