XP蓝牙搜索程序无效

nonono1111 2013-03-18 08:37:44
按照bluetooth essentials for programmers这本书编写的windows XP上面的寻找蓝牙的程序,从网上下了一个差不多的,但是总是在WSALookupServicebegin()这个函数这出错,返回-1,不知道是哪里出错了,哪位大牛能给点指导!


void CMy3Dlg::OnBnClickedButton3()
{
// TODO: 在此添加控件通知处理程序代码
// TODO: Add your control notification handler code here


WSADATA wsaData;
DWORD dwResult;
HANDLE hLookup = 0;
WSAQUERYSET lpRestrictions;
GUID guid = SVCID_HOSTNAME;
dwResult = WSAStartup(MAKEWORD(2,2), &wsaData);

if (dwResult != 0)
{
printf("Cannot startup Winsock, error code %d\n", dwResult);
exit(1);
}
else
printf("WSAStartup is OK!\n");

ZeroMemory(&lpRestrictions, sizeof(WSAQUERYSET));
lpRestrictions.dwSize = sizeof(WSAQUERYSET);
lpRestrictions.lpServiceClassId = &guid;

dwResult = WSALookupServiceBegin(&lpRestrictions, LUP_RETURN_NAME, &hLookup);
if (dwResult != SOCKET_ERROR)
{
DWORD dwLength = 0;
WSAQUERYSET * pqs = NULL;

printf("WSALookupServiceBegin() is OK!\r\n");

pqs = (WSAQUERYSET *) malloc(sizeof(WSAQUERYSET) + 100);
dwLength = sizeof(WSAQUERYSET) + 100;

do
{
if (WSALookupServiceNext(hLookup, 0, &dwLength, pqs) != 0)
{
dwResult = WSAGetLastError();
if((dwResult == WSA_E_NO_MORE) || (dwResult == WSAENOMORE))
{
printf("No more record found!\n");
break;
}
printf("WSALookupServiceNext() failed with error code %d\n", WSAGetLastError());
}
else
{
dwResult = 0;
printf("WSALookupServiceNext() looks fine!\r\n");
}

if (dwResult == WSAEFAULT)
{
if (pqs)
{
printf("Freeing pqs...\r\n");
free(pqs);
}

// Reallocate

pqs = (WSAQUERYSET *) malloc(dwLength);
if (!pqs)
{
printf("Could not allocate memory: %d\n", GetLastError());
exit(2);

}
else
{
printf("Memory allocated for pqs successfully!\r\n");
continue;
}
}

// Output it since we have it now
if ((dwResult == 0) && (pqs))
{
printf(" Service instance name: %S\n", pqs->lpszServiceInstanceName);
printf(" Name space num: %d\r\n", pqs->dwNameSpace);
printf(" Num of protocols: %d\r\n", pqs->dwNumberOfProtocols);
//printf(" Version: %d\r\n", pqs->lpVersion->dwVersion);
}
} while ((dwResult != WSA_E_NO_MORE) && (dwResult != WSAENOMORE));

// clean-up

free(pqs);
if(WSALookupServiceEnd(hLookup) == 0){
printf("hLookup handle was released!\n");
}
else{
printf("WSALookupServiceEnd(hLookup) failed with error code %d\n", WSAGetLastError());
}

}
else
{
printf("Error on WSALookupServiceBegin: %d\n", WSAGetLastError());
exit(3);
}

if(WSACleanup() == 0){
printf("WSACleanup() is OK!\n");
}
else{
printf("WSACleanup() failed with error code %d\n", WSAGetLastError());
}

printf("按回车键退出!\n");
char c = getchar();

}
...全文
88 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧