0

我一直在 Windows 中玩 wlanapi。在我尝试使用函数WlanScan之前,我在编译或运行时没有任何问题。然后由于没有在范围内声明“WlanScan”,我无法编译。我编写了一个非常简短的程序来说明这一点,它使用两个函数:有效的WlanOpenHandle和无效的WlanScan

#include <windows.h>
#include <wlanapi.h>

int main()
{
    HANDLE hClient;
    WlanOpenHandle(2, 0, 0, &hClient);

    WlanScan(hClient, 0, 0, 0, 0);
}

像这样编译单个文件:

g++ main.cpp -lwlanapi

导致此错误:

main.cpp: In function 'int main()':
main.cpp:9:30: error: 'WlanScan' was not declared in this scope
  WlanScan(hClient, 0, 0, 0, 0);
                              ^

这可能是什么原因?我已经能够使用 wlanapi 中的一些功能。我在使用 minGW 编译的 Windows 7 上。

编辑: 根据 u/IInspectable 所说,我将用于编译的命令更改为:

g++ -D_WIN32_WINNT=_WIN32_WINNT_WIN7 main.cpp -lwlanapi

它奏效了!

4

1 回答 1

0

看起来其他人以前遇到过这个问题:

如何编译具有“wlanapi.h”和“windows.h”依赖项的 C++ 代码

推荐的解决方案是将其放入 Visual Studio 并使用它进行编译;MinGW 可能无法找到该库。


使用 VS2010,我创建了一个 VC++ 控制台应用程序(带有预编译的头文件),并且我能够编译以下内容而没有任何错误:

// wlanapi_Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE hClient;
    WlanOpenHandle(2, 0, 0, &hClient);

    WlanScan(hClient, 0, 0, 0, 0);

}

这是我的预编译头文件:

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here


#include <windows.h>
#include <wlanapi.h>

#pragma comment(lib, "wlanapi.lib")
于 2014-08-15T19:51:17.073 回答