【问题标题】:Can't load QtBluetooth 5.12.0 under Windows 7在 Windows 7 下无法加载 QtBluetooth 5.12.0
【发布时间】:2019-01-25 08:49:46
【问题描述】:

我将一些代码从 Qt 5.6.0 迁移到 Qt 5.12.0,两者均使用 Visual Studio 2015 编译。它有一些代码使用 QtBluetooth 用于常规(非“低能耗”)蓝牙。在 5.6.0 中,这曾经可以完美运行。

使用 Qt 5.12.0,我的应用程序将无法加载。它报告缺少API-MS-WIN-CORE-WINRT-L1-1-0.DLLAPI-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL。我不明白为什么需要这些 WinRT 文件。 QtBluetooth.dll 的 Dependency Walker 报告这些库丢失。

我尝试使用 Qt 5.12.0 编译我的 selft 并作为 QtCreator 安装的一部分下载。我尝试了 Windows 7 和 10,Windows 10 运行良好。总是收到这个错误,我没有找到关于在哪里可以找到这些库或如何让 QtBluetooth 不使用它们的信息。

我应该怎么做才能在 Windows 下简单地运行基于 QtBluetooth 的应用程序?

编辑:提交的 Qt 错误:https://bugreports.qt.io/browse/QTBUG-73272

【问题讨论】:

  • 在 Windows 7 上使用 Qt-bluetooth 总是会导致我缺少库错误。对于非 LE 蓝牙,我使用的是 windows com-serial-interface。并不是说这是正确的,恕我直言,它应该在运行时失败,但有一个异常表明没有可用的 BT 后端。管理一个windows 7和windows 10版本让我很头疼。如果您对此有更多了解,请告诉我。
  • 我观察到 Qt 5.6 为 Win7 和 Win10 加载,而 Qt 5.12.0 没有加载...

标签: c++ qt qtbluetooth


【解决方案1】:

如果您对低能耗没有要求并且可以打扰您的用户使用 Windows 系统设置对话框配对设备,那么我建议为不使用 QtBluetooth 的 Windows 编写包装器代码。即

#include <Windows.h>

class win_con {
    ....

    HANDLE hcon;
    COMMTIMEOUTS *timeouts;

    // i.e. com_port = L"\\\\.\\COM1"; 
    void open_com(std::wstring com_port, int read_timeout, int write_timeout)
    {

        hcom = CreateFile(com_port.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, 
            OPEN_EXISTING, 0, nullptr);
        if (hcom == INVALID_HANDLE_VALUE) ...

        timeouts = new COMMTIMEOUTS();
        memset(timeouts, 0, sizeof(COMMTIMEOUTS));
        timeouts->ReadTotalTimeoutConstant = read_timeout;
        timeouts->WriteTotalTimeoutConstant = write_timeout;
        if (!SetCommTimeouts(hcom, timeouts)) ...

    }

    void write_data(QString data)
    {
        std::string stddata = data.toStdString();
        DWORD numwritten = 0;
        if (!WriteFile(hcom, stddata.c_str(),
                static_cast<DWORD>(stddata.length()), &numwritten, nullptr)) {
            ...
        }
    }

    QString read_data(int len)
    {
        #define BUFFER_SIZE 256
        char buffer[BUFFER_SIZE];
        DWORD data_read = 0;
        if (BUFFER_SIZE < len) ....
        for (int i = 0; i < BUFFER_SIZE; i++)
            buffer[i] = 0;

        ReadFile(hcom, &buffer, len, &data_read, nullptr);

        if (read == 0) ...
        if (read < len) ...

        return QString(buffer);
    }
}

【讨论】:

  • 是的,这是另一种选择。实际上你甚至可以使用QSerialPort 来做到这一点。无论如何,低能耗不适用于 Windows。
  • 如果您使用的是 Qt 5.10+,那么在 Windows 10(有和没有 UWP)上可以使用低能耗。只是应用程序无法在 Windows 7 上运行,因为缺少 DLL,即使它应该在运行时失败。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-06
  • 2017-03-27
  • 1970-01-01
  • 2011-04-25
  • 1970-01-01
  • 2011-03-29
相关资源
最近更新 更多