1
#include <Bthsdpdef.h>
#include <BluetoothAPIs.h>
#include <Winsock2.h>
#include <Ws2bth.h>
#include <stdio.h>

#pragma comment(lib, "Bthprops.lib")


int main(void){
    WSADATA wsaData;
    int err;
    DWORD qs_len;
    WSAQUERYSET *qs;
    DWORD flags;
    HANDLE lphLookup;
    int iRet;
    char buff[4096];
    LPWSAQUERYSET pwsaResult = (LPWSAQUERYSET) buff;
    DWORD buffLen = sizeof(buff);
    BTH_ADDR result;

    WORD wVersionRquested = MAKEWORD(2,2);
    err = WSAStartup(wVersionRquested, &wsaData);   //Statrup function

    if(err != 0){
        printf("WSAStartup failed");
        return 1;
    }

    qs_len = sizeof(WSAQUERYSET);
    qs = (WSAQUERYSET *) malloc(qs_len); // Save memory for  WSAQUERYSET structere
    ZeroMemory(qs, qs_len); //zero WSAQUERYSET out

    qs->dwSize = sizeof(WSAQUERYSET);
    qs->dwNameSpace = NS_BTH;
    qs->lpcsaBuffer = NULL;
    flags = LUP_CONTAINERS | LUP_FLUSHCACHE | LUP_RETURN_NAME | LUP_RETURN_ADDR; //details of the device discovery

    iRet = WSALookupServiceBegin(qs, flags, &lphLookup);
    if(iRet == ERROR_SUCCESS){
        printf("WSALookupServiceBegin failed");
        return 1;
    }
    printf("\r\n\t\t\t\tScanning...");

    ZeroMemory(pwsaResult, sizeof(LPWSAQUERYSET));
    pwsaResult->dwSize = sizeof(WSAQUERYSET);
    pwsaResult->dwNameSpace = NS_BTH;
    pwsaResult->lpBlob = NULL;

    while(WSALookupServiceNext(lphLookup, flags, &buffLen, pwsaResult) != ERROR_SUCCESS){
        result = ((BTH_ADDR)qs->lpcsaBuffer->RemoteAddr.lpSockaddr);
        WSAAddressToString(qs->lpcsaBuffer->RemoteAddr.lpSockaddr, sizeof(SOCKADDR_BTH), NULL, (LPWSTR)buff, &buffLen);
        printf("Found: %s - %s", buff, qs->lpszServiceInstanceName);
    }

    WSALookupServiceEnd(lphLookup);
    free(qs);
    WSACleanup();
    return 0;
}

我在 microsoft sdk 文件夹中的 bthsdpdef.h 中有很多语法和缺少类型错误。如:

bthsdpdef.h(11):错误 C2146:语法错误:缺少 ';' 在标识符“LowPart”之前,

bthsdpdef.h(11):错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持默认整数

有什么问题?

4

2 回答 2

1

在文件顶部(#include 上方)添加以下行。这为我修复了编译。

#define WIN32_LEAN_AND_MEAN 1 // Don't include Winsock v1
#include <windows.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")

您的代码需要包含基本头文件。否则,当编译器来到 bthsdpdef.h 并看到以下内容时,它不知道 ULONGLONG 类型的定义。

struct SDP_LARGE_INTEGER_16 {
    ULONGLONG LowPart;
    LONGLONG HighPart;
};
于 2012-11-20T09:59:33.977 回答
0

我认为编译器错误中有一个提示(我知道,这很令人惊讶,但它确实发生了)。

这些很可能是 C API,但您正在编译为 C++。您的代码确实看起来像 C,因此请仔细检查您的编译器调用。

于 2012-11-19T14:31:59.647 回答