通过windows API获取不到PC个人热点名称,通过读取注册表获取个人个点配置文件路径再去解析这个文件
bool GetSubKeyInfo(HKEY hKey, const std::wstring& subKeyName, std::string& ProfileGUID)
{
bool bTrue = false;
HKEY hSubKey;
if (RegOpenKeyExW(hKey, subKeyName.c_str(), 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
{
DWORD valueType;
DWORD dataSize;
DWORD dwResult;
WCHAR portType[256];
WCHAR profileList[256];
// 获取 PortType 的值 个人热点portType 为16
dataSize = sizeof(portType);
dwResult = RegQueryValueExW(hSubKey, L"PortType", NULL, &valueType, reinterpret_cast<BYTE*>(portType), &dataSize);
size_t size = wcslen(portType);
std::wstring str(portType, size);
std::string utf8Str(str.begin(), str.end());
if (dwResult == ERROR_SUCCESS && !utf8Str.empty() && 16 == utf8Str[0])
{
// 获取 ProfileList 的值
dataSize = sizeof(profileList);
dwResult = RegQueryValueExW(hSubKey, L"ProfileList", NULL, &valueType, reinterpret_cast<BYTE*>(profileList), &dataSize);
if (dwResult == ERROR_SUCCESS)
{
size = wcslen(profileList);
std::wstring str(profileList, size);
std::string utf8Str(str.begin(), str.end());
ProfileGUID = utf8Str;
bTrue = true;
}
}
RegCloseKey(hSubKey);
}
return bTrue;
}
void GetWifiConfigFilePath(std::string& strConfigFilePath)
{
// 配置文件路径
std::string interfaceGUID = ""; // 替换为实际的网络适配器标识符
std::string ProfileGUID = ""; // 移动热点配置文件的唯一标识符
HKEY hKey;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\WlanSvc\\Interfaces", 0, KEY_READ | KEY_WOW64_64KEY, &hKey) == ERROR_SUCCESS)
{
HKEY hSubKey;
std::wstring subKey;
if (RegOpenKeyExW(hKey, subKey.c_str(), 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
{
const DWORD bufferSize = 256;
WCHAR valueName[bufferSize];
DWORD valueNameSize = bufferSize;
DWORD index = 0;
while (RegEnumKeyExW(hSubKey, index, valueName, &valueNameSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
bool bTrue = GetSubKeyInfo(hKey, valueName, ProfileGUID);
if (bTrue)
{
size_t size = wcslen(valueName);
std::wstring str(valueName, size);
std::string utf8Str(str.begin(), str.end());
interfaceGUID = utf8Str;
strConfigFilePath = "C:\\ProgramData\\Microsoft\\Wlansvc\\Profiles\\Interfaces\\" + interfaceGUID + "\\" + ProfileGUID + ".xml";
break;
}
++index;
valueNameSize = bufferSize;
}
RegCloseKey(hSubKey);
}
RegCloseKey(hKey);
}
}
// 获取PC个人热点WIFI名称
QString GetPcPersonWifiName()
{
// 没连接wifi时去获取个人热点
std::string strWifiName = "";
std::string stdConfigFilePath = "";
GetWifiConfigFilePath(stdConfigFilePath);
// 打开配置文件
std::ifstream configFile(stdConfigFilePath);
if (configFile.is_open())
{
// 查找移动热点名称
std::string line;
std::string hotspotName;
while (std::getline(configFile, line))
{
// 假设配置文件中的热点名称行以 "<name>" 开头
if (line.find("<name>") != std::string::npos)
{
std::size_t startPos = line.find("<name>") + 6;
std::size_t endPos = line.find("</name>");
strWifiName = line.substr(startPos, endPos - startPos).c_str();
if (strWifiName.compare("WFD_GROUP_OWNER_PROFILE") != 0)
{
break;
}
}
}
// 关闭配置文件
configFile.close();
}
return strWifiName.c_str();
}
// 获取PC连接的wifi名称
QString GetConnectedWifiName(QString strInterfaceName = "")
{
QString strWifiName;
HANDLE hClient = nullptr;
DWORD dwMaxClient = 2; // 最大客户端数量,根据实际情况调整
DWORD dwCurVersion = 0;
DWORD dwResult = 0;
// 初始化WLAN API
dwResult = WlanOpenHandle(dwMaxClient, nullptr, &dwCurVersion, &hClient);
if (dwResult != ERROR_SUCCESS)
{
qDebug() << QString::fromLocal8Bit("无法打开WLAN句柄");
return strWifiName;
}
// 获取接口列表
PWLAN_INTERFACE_INFO_LIST pIfList = nullptr;
dwResult = WlanEnumInterfaces(hClient, nullptr, &pIfList);
if (dwResult != ERROR_SUCCESS) {
qDebug() << QString::fromLocal8Bit("无法枚举WLAN接口");
WlanCloseHandle(hClient, nullptr);
return strWifiName;
}
// 遍历接口列表
for (DWORD i = 0; i < pIfList->dwNumberOfItems; i++) {
PWLAN_INTERFACE_INFO pIfInfo = &pIfList->InterfaceInfo[i];
// 获取接口状态
WLAN_INTERFACE_STATE state = pIfInfo->isState;
if (state == wlan_interface_state_connected) {
// 获取当前连接的网络
PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = nullptr;
DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
dwResult = WlanQueryInterface(hClient, &pIfInfo->InterfaceGuid, wlan_intf_opcode_current_connection, nullptr, &connectInfoSize, reinterpret_cast<PVOID*>(&pConnectInfo), nullptr);
if (dwResult == ERROR_SUCCESS)
{
strWifiName = QString::fromWCharArray(pConnectInfo->strProfileName);
qDebug() << QString::fromLocal8Bit("当前连接的WiFi或热点名称:") << strWifiName;
WlanFreeMemory(pConnectInfo);
}
}
}
// 释放资源
WlanFreeMemory(pIfList);
WlanCloseHandle(hClient, nullptr);
return strWifiName;
}