while (count < bluetoothCount) { WSAQUERYSET result; memset(&result, 0, sizeof(result)); result.dwSize = sizeof(result); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &result); if (iResult != 0) { break; } count++; } &result报错,实参与形参不兼容
时间: 2023-06-17 14:08:32 浏览: 187
根据你提供的代码,问题出在 `&result` 这个参数上。`WSALookupServiceNext` 函数的第三个参数是一个指向 `WSAQUERYSET` 结构体的指针,而你的代码中使用了 `&result`,这是一个指向 `WSAQUERYSET` 结构体的指针的指针,所以会出现类型不兼容的错误。
解决方法是直接使用 `result`,而不是 `&result`。修改后的代码如下:
```c++
while (count < bluetoothCount) {
WSAQUERYSET result;
memset(&result, 0, sizeof(result));
result.dwSize = sizeof(result);
iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &result);
if (iResult != 0) {
break;
}
count++;
}
```
这样就可以避免类型不兼容的错误了。
相关问题
int main() { std::map<std::string, std::string> config = readIniFile("config.ini"); int bluetoothCount = std::stoi(config["bluetooth.count"]); std::string logFileName = config["log.filename"]; WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { std::cout << "WSAStartup failed: " << iResult << std::endl; return 1; } WSAQUERYSET service; memset(&service, 0, sizeof(service)); service.dwSize = sizeof(service); service.dwNameSpace = NS_BTH; HANDLE lookupHandle = NULL; iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle); if (iResult != 0) { std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl; WSACleanup(); return 1; } int count = 0; while (count < bluetoothCount) { WSAQUERYSET result; memset(&result, 0, sizeof(result)); result.dwSize = sizeof(result); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &result); if (iResult != 0) { break; } count++; } WSALookupServiceEnd(lookupHandle); WSACleanup(); if (count >= bluetoothCount) { std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } else { std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } return 0; } &result报错 改一下逻辑把
对于代码中的WSAQUERYSET result,可能是因为在WSALookupServiceNext函数中,result参数需要指向一个WSAQUERYSET结构体的指针,而代码中的result是一个局部变量,无法传递给WSALookupServiceNext函数,导致编译器报错。可以将result声明为指针类型,如下所示:
WSAQUERYSET* result = new WSAQUERYSET;
memset(result, 0, sizeof(WSAQUERYSET));
result->dwSize = sizeof(WSAQUERYSET);
同时,在代码结束前,需要释放result指向的空间,避免内存泄漏,可以添加如下语句:
delete result;
#include <Winsock2.h> #include <Windows.h> #include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector> #include <map> #pragma comment(lib, "ws2_32.lib") std::map<std::string, std::string> readIniFile(std::string fileName) { std::map<std::string, std::string> configMap; std::ifstream file(fileName); if (file.is_open()) { std::string line; std::string section = ""; while (getline(file, line)) { if (line.empty()) { continue; } if (line[0] == '[' && line[line.length() - 1] == ']') { section = line.substr(1, line.length() - 2); } else { std::stringstream ss(line); std::string key, value; getline(ss, key, '='); getline(ss, value); configMap[section + "." + key] = value; } } file.close(); } return configMap; } void writeLogFile(std::string fileName, std::string logText) { std::ofstream file(fileName, std::ios_base::app); if (file.is_open()) { file << logText << std::endl; file.close(); } } int main() { std::map<std::string, std::string> config = readIniFile("config.ini"); int bluetoothCount = std::stoi(config["bluetooth.count"]); std::string logFileName = config["log.filename"]; WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { std::cout << "WSAStartup failed: " << iResult << std::endl; return 1; } WSAQUERYSET service; memset(&service, 0, sizeof(service)); service.dwSize = sizeof(service); service.dwNameSpace = NS_BTH; HANDLE lookupHandle = NULL; iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle); if (iResult != 0) { std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl; WSACleanup(); return 1; } int count = 0; WSAQUERYSET* pResult = (WSAQUERYSET*)LocalAlloc(LPTR, sizeof(WSAQUERYSET)); while (count < bluetoothCount) { DWORD dwSize = sizeof(WSAQUERYSET); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pResult); if (iResult != 0) { break; } count++; } LocalFree(pResult); WSALookupServiceEnd(lookupHandle); WSACleanup(); if (count >= bluetoothCount) { std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } else { std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } return 0; } 给这段代码搜索蓝牙设备加上搜索时间
可以在搜索蓝牙设备的while循环中添加一个计时器,当计时器达到设定的搜索时间时,跳出循环。
例如,在main函数中添加一个变量searchTime,表示搜索蓝牙设备的最大时间,单位为秒。然后在while循环中添加一个计时器,每次循环结束将已经搜索的时间加上循环间隔(例如1秒),如果搜索时间超过了searchTime,就跳出循环。
修改后的代码如下:
int main() {
std::map<std::string, std::string> config = readIniFile("config.ini");
int bluetoothCount = std::stoi(config["bluetooth.count"]);
std::string logFileName = config["log.filename"];
int searchTime = std::stoi(config["bluetooth.searchtime"]); // 添加搜索时间配置项
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cout << "WSAStartup failed: " << iResult << std::endl;
return 1;
}
WSAQUERYSET service;
memset(&service, 0, sizeof(service));
service.dwSize = sizeof(service);
service.dwNameSpace = NS_BTH;
HANDLE lookupHandle = NULL;
iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle);
if (iResult != 0) {
std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl;
WSACleanup();
return 1;
}
int count = 0;
WSAQUERYSET* pResult = (WSAQUERYSET*)LocalAlloc(LPTR, sizeof(WSAQUERYSET));
int searchCount = 0; // 添加搜索计数器
while (searchCount < searchTime) { // 添加计时器
DWORD dwSize = sizeof(WSAQUERYSET);
iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pResult);
if (iResult != 0) {
break;
}
count++;
searchCount++; // 每次循环增加已搜索时间
Sleep(1000); // 等待1秒,再进行下一次搜索
if (searchCount >= searchTime) { // 如果已经搜索了设定的时间,跳出循环
break;
}
}
LocalFree(pResult);
WSALookupServiceEnd(lookupHandle);
WSACleanup();
if (count >= bluetoothCount) {
std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount);
writeLogFile(logFileName, logText);
}
else {
std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount);
writeLogFile(logFileName, logText);
}
return 0;
}
阅读全文