Qt Windows下根据GUID获取设备名称
void guidFormString(const char *pszGuid, GUID &guid) {
if (pszGuid == nullptr) return;
int temp[3] = {0, 0, 0};
sscanf_s(pszGuid, "%08x-%4hx-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", &(guid.Data1), &(guid.Data2)
, reinterpret_cast<unsigned int*>(&(guid.Data3)), reinterpret_cast<unsigned int*>(&guid.Data4[0])
, reinterpret_cast<unsigned int*>(&guid.Data4[1]), reinterpret_cast<unsigned int*>(&guid.Data4[2])
, reinterpret_cast<unsigned int*>(&guid.Data4[3]), reinterpret_cast<unsigned int*>(&guid.Data4[4])
, &temp[0], &temp[1], &temp[2]);
guid.Data4[5] = static_cast<unsigned char>(temp[0]);
guid.Data4[6] = static_cast<unsigned char>(temp[1]);
guid.Data4[7] = static_cast<unsigned char>(temp[2]);
}
QString getInterfaceNameFromDeviceGuid(const QString &sGuid, ulong &eCode) {
if (sGuid.isEmpty()) return "";
//1. 打开动态库,获取动态库实例
auto wlpstr = QString::fromLocal8Bit("Iphlpapi.dll");
auto strWlpstr = wlpstr.toStdWString();
auto lpcwStr = strWlpstr.c_str();
auto hDll = LoadLibrary(lpcwStr);
auto NhGetInterfaceNameFromDeviceGuid =
reinterpret_cast<type_NhGetInterfaceNameFromDeviceGuid>(GetProcAddress(hDll, "NhGetInterfaceNameFromDeviceGuid"));
//2. 将字符换类型的guid转换成结构体GUID
GUID guid;
guidFormString(sGuid.toStdString().c_str(), guid); // 我网卡的GUID,对应接口名为“本地连接”
//3. 根据GUID获取设备的名称
TCHAR buf[200] = {0};
DWORD len = sizeof(buf);
eCode = NhGetInterfaceNameFromDeviceGuid(&guid, reinterpret_cast<char*>(buf), &len, 0, 1);
QString sNetCardName = "";
if(eCode == 0) {
sNetCardName = QString::fromWCharArray(buf);
} else {
}
//4. 释放动态库资源
FreeLibrary(hDll);
return sNetCardName;
}
推荐一个免费分享程序得网站:https://www.chengxuunion.com/