c - 为什么 WlanOpenHandle() 在这里失败了?

标签 c windows winapi wifi

我正在尝试用 C 语言编写一些使用 native WiFi API 的 WinAPI 代码。目前,我卡在了最开始,打开 WiFi handle 。

这是我目前使用的代码:

#include <stdio.h>
#include <Windows.h>
#include <wlanapi.h>
#pragma comment(lib, "Wlanapi.lib")

int main(int argc, char* argv[]) {
    HANDLE *wh = NULL;
    unsigned long nv;
    int rc;

    if ((rc = WlanOpenHandle(1, NULL, &nv, wh)) != ERROR_SUCCESS) {
        printf("WlanOpenHandle(): ");
        switch (rc) {
            case ERROR_INVALID_PARAMETER:
                printf("Invalid param.\n");
                break;
            default:
                printf("Unknown error.\n");
        }
        goto cleanup;
    }

    cleanup:
    WlanCloseHandle(wh, NULL);
    getchar();
    return 0;
}

这段代码打印出“WlanOpenHandle(): Invalid param.”。

为什么要这样做?从 API 文档来看,我似乎正确地传递了所有内容。我还尝试将 2 作为 WlanOpenHandle() 的第一个参数。

(请注意,这不是我的完整代码,但是这个片段已经过测试以显示上述问题。在实际代码中,开关中有更多的情况,并且 goto< 下的代码更多s 到 cleanup。)

最佳答案

您正在为 phClientHandle 参数传递 NULL 指针。试试这个:

HANDLE wh; // note: HANDLE, not HANDLE*
// ...
if ((rc = WlanOpenHandle(1, NULL, &nv, &wh)) != ERROR_SUCCESS) // note: &wh

关于c - 为什么 WlanOpenHandle() 在这里失败了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29046141/

相关文章:

Windows 上的 C++ : executable path with whitespace in system() call

c - 所有者绘制选项卡控件 - 选项卡项目的文本

winapi - Windows API 提取zip 文件?

c - (char *)__strdup 在 c 中做什么

c - C中的链表程序挂了

c - C 中的 pieslice() 函数

c++ - 编译对象的布局

windows - Git 无法在 Windows 网络环境中暂存或提交 SourceTree

c++ - C++检测Windows操作系统

c++ - 从句柄获取文件路径