#include "DllWrapper.h" #include "Logger.h" #include #include #include #define SDKDLL L"PCommMKit.dll" // 静态成员变量,用于在回调函数中访问类实例 static DllWrapper *g_dll_wrapper_instance = nullptr; DllWrapper::DllWrapper() : dll_handle_(nullptr), init_monitor_(nullptr), start_monitor_(nullptr), stop_monitor_(nullptr), set_write_data_(nullptr), set_read_data_(nullptr), free_monitor_(nullptr), get_last_error_(nullptr) { // 设置静态实例指针 g_dll_wrapper_instance = this; } DllWrapper::~DllWrapper() { Cleanup(); g_dll_wrapper_instance = nullptr; } bool DllWrapper::Initialize() { if (initialized_) { return true; } // 获取可执行文件所在目录 wchar_t buffer[MAX_PATH]; GetModuleFileName(NULL, buffer, MAX_PATH); std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\"); std::wstring exe_path = std::wstring(buffer).substr(0, pos); // 构建完整的DLL路径 std::wstring full_dll_path = exe_path + L"\\" + SDKDLL; // 加载DLL dll_handle_ = LoadLibrary(full_dll_path.c_str()); if (!dll_handle_) { LOG_ERROR("Failed to load DLL: " + std::to_string(GetLastError())); return false; } // 获取DLL函数地址 init_monitor_ = (TInitMonitor)GetProcAddress(dll_handle_, "InitMonitor"); start_monitor_ = (TMonitor)GetProcAddress(dll_handle_, "Monitor"); stop_monitor_ = (TStop)GetProcAddress(dll_handle_, "Stop"); set_write_data_ = (TSetWrite_Data)GetProcAddress(dll_handle_, "SetWrite_Data"); set_read_data_ = (TSetRead_Data)GetProcAddress(dll_handle_, "SetRead_Data"); free_monitor_ = (TFreeMonitor)GetProcAddress(dll_handle_, "FreeMonitor"); get_last_error_ = (Tget_LastErrror)GetProcAddress(dll_handle_, "get_LastErrror"); if (!init_monitor_ || !start_monitor_ || !stop_monitor_ || !set_write_data_ || !set_read_data_ || !free_monitor_ || !get_last_error_) { LOG_ERROR("Failed to get function addresses from DLL"); FreeLibrary(dll_handle_); dll_handle_ = nullptr; return false; } initialized_ = true; return true; } void DllWrapper::Cleanup() { if (dll_handle_) { FreeLibrary(dll_handle_); dll_handle_ = nullptr; } init_monitor_ = nullptr; start_monitor_ = nullptr; stop_monitor_ = nullptr; set_write_data_ = nullptr; set_read_data_ = nullptr; free_monitor_ = nullptr; get_last_error_ = nullptr; initialized_ = false; callback_initialized_ = false; } bool DllWrapper::InitMonitor(const std::string &key) { if (!initialized_) { LOG_ERROR("DLL not initialized"); return false; } if (callback_initialized_) { return true; } // 将std::string转换为宽字符串 std::wstring wide_key(key.begin(), key.end()); // 调用DLL的InitMonitor函数 callback_initialized_ = init_monitor_(wide_key.c_str(), OnMonitorEx); if (!callback_initialized_) { LOG_ERROR("Failed to initialize monitor: " + std::to_string(GetLastError())); return false; } return true; } bool DllWrapper::MonitorPort(int port) { if (!initialized_ || !callback_initialized_) { LOG_ERROR("DLL not initialized or monitor not initialized"); return false; } // 调用DLL的Monitor函数 bool result = start_monitor_(port); if (result) { std::lock_guard lock(mutex_); monitored_ports_[port] = true; LOG_INFO("Started monitoring port: " + std::to_string(port)); } else { LOG_ERROR("Failed to start monitoring port: " + std::to_string(port) + ", error: " + std::to_string(GetLastError())); } return result; } bool DllWrapper::StopPort(int port) { if (!initialized_) { LOG_ERROR("DLL not initialized"); return false; } // 调用DLL的Stop函数 bool result = stop_monitor_(port); if (result) { std::lock_guard lock(mutex_); monitored_ports_.erase(port); LOG_INFO("Stopped monitoring port: " + std::to_string(port)); } else { LOG_ERROR("Failed to stop monitoring port: " + std::to_string(port) + ", error: " + std::to_string(GetLastError())); } return result; } bool DllWrapper::ReadData(int port, const std::string &hex_data) { if (!initialized_) { LOG_ERROR("DLL not initialized"); return false; } // 将十六进制字符串转换为字节数组 std::vector bytes = Utils::ConvertHexStringToBytes(hex_data); // 调用DLL的SetRead_Data函数 bool result = set_read_data_(port, reinterpret_cast(bytes.data()), static_cast(bytes.size())); if (!result) { LOG_ERROR("Failed to set read data for port: " + std::to_string(port) + ", error: " + std::to_string(GetLastError())); } return result; } bool DllWrapper::WriteData(int port, const std::string &hex_data) { if (!initialized_) { LOG_ERROR("DLL not initialized"); return false; } // 将十六进制字符串转换为字节数组 std::vector bytes = Utils::ConvertHexStringToBytes(hex_data); // 调用DLL的SetWrite_Data函数 bool result = set_write_data_(port, reinterpret_cast(bytes.data()), static_cast(bytes.size())); if (!result) { LOG_ERROR("Failed to set write data for port: " + std::to_string(port) + ", error: " + std::to_string(GetLastError())); } return result; } bool DllWrapper::FreeMonitor() { if (!initialized_) { LOG_ERROR("DLL not initialized"); return false; } // 调用DLL的FreeMonitor函数 free_monitor_(); // 清空监控端口列表 std::lock_guard lock(mutex_); monitored_ports_.clear(); LOG_INFO("Freed monitor"); return true; } int DllWrapper::GetLastError() { if (!initialized_) { LOG_ERROR("DLL not initialized"); return -1; } // 调用DLL的get_LastErrror函数 return get_last_error_(); } void DllWrapper::SetEventCallback(CommEventCallback callback) { // 调用基类的实现来设置回调 CommWrapperBase::SetEventCallback(callback); // 设置DLL事件回调,直接使用通用事件数据调用通用回调 dll_event_callback_ = [this](const CommEventData &event_data) { // 直接使用通用事件数据调用通用回调 if (event_callback_) { event_callback_(event_data); } }; } std::string DllWrapper::GetErrorMessage(int error_code) { switch (error_code) { case -241: return "安装驱动失败"; case -243: return "其他错误"; case -244: return "驱动文件"; case -255: return "绑定串口失败"; case -254: return "第一次启动安装,必须以管理员权限运行"; case -253: return "启动驱动服务失败"; case -252: return "请检查Key是否有效"; case -1: return "无效操作"; case 0: return "成功"; case 1: return "串口打开失败"; case 2: return "串口配置失败"; case 3: return "串口读取失败"; case 4: return "串口写入失败"; case 5: return "串口关闭失败"; default: return "未知错误"; } } bool DllWrapper::IsPortMonitored(int port) { std::lock_guard lock(mutex_); auto it = monitored_ports_.find(port); return (it != monitored_ports_.end() && it->second); } // 静态回调函数,用于接收DLL事件 LONG CALLBACK DllWrapper::OnMonitorEx(int Sequence, double dTime, DWORD ComNumber, DWORD Irp, DWORD dwSize, char *lpData) { // 检查是否有有效的类实例 if (!g_dll_wrapper_instance) { return 0; } // 检查是否有设置回调函数 if (!g_dll_wrapper_instance->event_callback_) { return 0; } try { // 创建CommEventData结构以传递给回调函数 CommEventData event_data; event_data.event_type = static_cast(Irp); event_data.port = static_cast(ComNumber); if (lpData && dwSize > 0) { event_data.data.assign(reinterpret_cast(lpData), reinterpret_cast(lpData) + dwSize); } event_data.data_size = dwSize; // 调用用户设置的回调函数 g_dll_wrapper_instance->event_callback_(event_data); LOG_DEBUG("DLL event processed successfully"); } catch (const std::exception &e) { LOG_ERROR("Error processing DLL event: " + std::string(e.what())); return -1; } return 0; }