我的应用程序遇到问题,我试图获取它运行的系统的所有网络配置。最终目标是找到优先级最高的MAC地址。
当我使用 QtCreator 运行代码时,代码运行正常并且可以工作,当我创建包含 dll 文件和 exe 文件的文件夹时,代码也运行正常。
但问题是,当我在其他 Windows 计算机(7 和 10)上运行此程序时,它会运行但不会返回或显示任何内容。我尝试以管理员身份运行它,但这也不起作用,并且此代码应该能够在所有 Windows 平台上运行。
有什么建议么?
我目前正在Windows 10并使用Qt 5.8 MSVC 2015
该 exe 文件在 Windows 10 上与这些 dll 一起运行:
- Qt5Core.dll
- Qt5Network.dll
- msvcp140.dll
- msvcr120.dll
- vcruntime140.dll
对于 Windows 7,这些 dll 也应该存在:
- api-ms-win-core-file-l1-2-0.dll
- api-ms-win-core-file-l2-1-0.dll
- api-ms-win-core-localization-l1-2-0.dll
- api-ms-win-core-processthreads-l1-1-1.dll
- api-ms-win-core-string-l1-1-0.dll
- api-ms-win-core-synch-l1-2-0.dll
- api-ms-win-core-timezone-l1-1-0.dll
- api-ms-win-crt-convert-l1-1-0.dll
- api-ms-win-crt-environment-l1-1-0.dll
- api-ms-win-crt-filesystem-l1-1-0.dll
- api-ms-win-crt-heap-l1-1-0.dll
- api-ms-win-crt-locale-l1-1-0.dll
- api-ms-win-crt-math-l1-1-0.dll
- api-ms-win-crt-multibyte-l1-1-0.dll
- api-ms-win-crt-runtime-l1-1-0.dll
- api-ms-win-crt-stdio-l1-1-0.dll
- api-ms-win-crt-string-l1-1-0.dll
- api-ms-win-crt-time-l1-1-0.dll
- api-ms-win-crt-utility-l1-1-0.dll
下面的链接是 exe 和 dll 文件一起:
https://ufile.io/e9htu https://ufile.io/e9htu
如果需要,这是我的代码:
main.cpp
#include <QCoreApplication>
#include "macfinder.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MACFinder macFinder;
macFinder.findMAC();
return a.exec();
}
macfinder.h
#ifndef MACFINDER_H
#define MACFINDER_H
#include <QObject>
#include <QNetworkConfiguration>
#include <QNetworkConfigurationManager>
#include <QNetworkInterface>
#include <QNetworkSession>
#include <QDebug>
class MACFinder : public QObject
{
Q_OBJECT
public:
explicit MACFinder(QObject *parent = 0);
void findMAC();
private:
QNetworkConfigurationManager ncm;
QString filterMAC(QList<QNetworkConfiguration> configs);
signals:
void foundMAC(QString MAC);
private slots:
void configurationsUpdateCompleted();
};
#endif // MACFINDER_H
macfinder.cpp
#include "macfinder.h"
MACFinder::MACFinder(QObject *parent) : QObject(parent)
{
}
QString MACFinder::filterMAC(QList<QNetworkConfiguration> configs)
{
qDebug() << "MAC and Index: ";
QString MAC;
int index;
QNetworkConfiguration nc;
foreach(nc,configs)
{
QNetworkSession networkSession(nc);
QNetworkInterface netInterface = networkSession.interface();
QString debugStr = QString::number(netInterface.index())
+ " | " + netInterface.humanReadableName() + " | "
+ nc.name() + " | " + netInterface.hardwareAddress();
if(netInterface.hardwareAddress().isEmpty())
{
qDebug() << "--> No MAC: " << debugStr;
continue;
}
if(netInterface.name().isEmpty())
{
qDebug() << "--> NO NAME: " << debugStr;
continue;
}
if(netInterface.index() == 0)
{
qDebug() << "--> NO INDEX: " << debugStr;
continue;
}
if(netInterface.flags() & QNetworkInterface::IsLoopBack)
{
qDebug() << "--> loopBack: " << debugStr;
continue;
}
if(netInterface.flags() & (QNetworkInterface::IsRunning | QNetworkInterface::IsUp))
{
qDebug() << "*** Accepted: " << debugStr;
if(MAC.isEmpty())
{
qDebug() << "setting MAC:" << debugStr;
MAC = netInterface.hardwareAddress();
index = netInterface.index();
}
else
{
if(netInterface.index() < index)
{
qDebug() << "setting MAC:" << debugStr;
MAC = netInterface.hardwareAddress();
index = netInterface.index();
}
else
qDebug() << "index is not lower: " << debugStr;
}
}
}
return MAC;
}
void MACFinder::findMAC()
{
qDebug() << "MACFinder::findMAC | updating all configurations";
connect(&ncm,SIGNAL(updateCompleted()),this,SLOT(configurationsUpdateCompleted()));
ncm.updateConfigurations();
}
void MACFinder::configurationsUpdateCompleted()
{
qDebug() << "MACFinder::configurationsUpdateCompleted";
disconnect(&ncm,SIGNAL(updateCompleted()),this,SLOT(configurationsUpdateCompleted()));
QNetworkConfiguration nc;
QList<QNetworkConfiguration> configs = ncm.allConfigurations(QNetworkConfiguration::Active);
qDebug() << "\nAllConfigs: ";
foreach (nc,configs)
{
qDebug() << nc.identifier() << nc.name() << nc.state();
}
QString MAC = filterMAC(configs);
qDebug() << "\nMAC:" << MAC;
if(MAC.isEmpty())
{
qDebug("no MAC address found");
}
emit foundMAC(MAC);
}