Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

c++ - WSALookupServiceBegin()上的错误代码10022(无效的预订)(Error code 10022 (Invalid arugment) on WSALookupServiceBegin())

I'm currently attempting to do Bluetooth device discovery with the Window Winsock API.

(我目前正在尝试使用Window Winsock API进行蓝牙设备发现。)

I am fairly new to C and C++ coding, so I apologize if I have any silly mistakes here.

(我是C和C ++编码的新手,如果在这里有任何愚蠢的错误,我深表歉意。)

#include <winsock2.h>
#include <ws2tcpip.h>
#include <ws2bth.h>
#include <stdio.h>

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

int main() {
    int iResult;

    // Initialize winsock
    WSADATA wsaData;
    iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d
", iResult);
        return 1;
    }

    // Create query parameters
    WSAQUERYSETW querySet;
    memset(&querySet, 0, sizeof(querySet));
    querySet.dwSize = sizeof(querySet);
    querySet.dwNameSpace = NS_BTH;

    // Create flags and handle
    DWORD flags = LUP_DEEP | LUP_RETURN_ALL;
    HANDLE handle;

    // Begin service lookup
    iResult = WSALookupServiceBeginW(&querySet, flags, &handle);
    if(iResult != 0) {
        printf("Failed to begin service lookup: %d
", WSAGetLastError());
        return 1;
    }
}

I have been looking all over for examples of using WinSock to do Bluetooth device discovery, however I have been completely unable to find any.

(我一直在寻找使用WinSock进行蓝牙设备发现的示例,但是我一直找不到。)

I am certain I am simply using the wrong keywords in my search.

(我确定我只是在搜索中使用了错误的关键字。)

However, when running this code it goes fine up until the actual call to WSALookupServiceBeginW() .

(但是,在运行此代码时,它将一直有效到实际调用WSALookupServiceBeginW()为止。)

When calling this method, I get an error code of 10022 which according to the Microsoft documentation means I have passed an invalid argument.

(调用此方法时,出现错误代码10022,根据Microsoft文档,这意味着我传递了无效的参数。)

However, I am unsure which argument is invalid.

(但是,我不确定哪个参数无效。)

Is it my query parameters?

(是我的查询参数吗?)

My flags?

(我的旗帜?)

My querySet?

(我的querySet?)

If anyone knows what I am doing wrong in this specific block of code or knows a good place I can find information on how to use the WinSock API, I would also greatly appreciate that.

(如果有人知道我在此特定代码块中做错了什么,或者知道一个合适的地方,我可以找到有关如何使用WinSock API的信息,我也将不胜感激。)

EDIT 1: I am not new to coding, just to C and C++ coding (and the Windows APIs in general).

(编辑1:我不是编码新手,只是C和C ++编码(和一般Windows API)。)

I wouldn't say I am an expert, but I would say I am fairly proficient in the Java programming language.

(我不会说我是专家,但是我会说我非常精通Java编程语言。)

  ask by Whirvis translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue I was having is that I was using the wrong flags in the WSALookupServiceBegin() function.

(我遇到的问题是我在WSALookupServiceBegin()函数中使用了错误的标志。)

The flags should have contained the values of LUP_CONTAINERS and LUP_FLUSH_CACHE , but they did not.

(这些标志应该包含LUP_CONTAINERSLUP_FLUSH_CACHE的值,但它们没有。)

Thanks to the people in the comments for the solution and more information on how to better use Winsock!

(感谢评论中提供解决方案的人员以及有关如何更好地使用Winsock的更多信息!)

:)

(:))


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...