PC为XP SP3,ThinkPad SL400,自带有蓝牙功能,现在我在PC旁边打开手机蓝牙,使用PC的蓝牙搜索,可以搜索到手机蓝牙。
但是我自己编写的程序为什么就搜索不到呢?代码如下:
int _tmain(int argc, _TCHAR* argv[])
{
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());