0

I have been playing around with wlanapi in windows. I did not have any problem compiling or running until I tried using the function WlanScan. Then I was unable to compile due to "WlanScan" not being declared in the scope. I wrote a really short program illustrating this using two functions: WlanOpenHandle that works and WlanScan which doesn't.

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

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

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

Compiling that single file like this:

g++ main.cpp -lwlanapi

Results in this error:

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

What could be the cause of this? I've been able to use a handful of functions from the wlanapi. I am on Windows 7 compiling with minGW.

EDIT: In accordance to what u/ IInspectable said, I changed the command used to compile to:

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

And it worked!

EFTH
  • 475
  • 5
  • 14
  • 1
    Your `main` function needs to return an integer. – Thomas Matthews Aug 15 '14 at 19:55
  • @ThomasMatthews True, but that's for the next question -- don't get ahead of yourself. :) – Forest Kunecke Aug 15 '14 at 20:01
  • @FKunecke What do you mean? This is c++, the compiler takes care of that – EFTH Aug 15 '14 at 20:03
  • You declared the `main()` function as returning an `int`. Where does you code satisfy the declaration? Ideally, you should specify the return value to the operating system rather than relying on compilers. – Thomas Matthews Aug 15 '14 at 20:05
  • @ThomasMatthews That may be true. But it is irrelevant to my question. – EFTH Aug 15 '14 at 20:09
  • @EFTH I updated my answer; I'm curious though -- try just adding `#pragma comment(lib, "wlanapi.lib")` to your current code and see what happens. – Forest Kunecke Aug 15 '14 at 20:10
  • @FKunecke Thank you for the help. But adding that line did not help. – EFTH Aug 15 '14 at 20:13
  • @EFTH did it give you the same error? I'm not able to reproduce your problem. – Forest Kunecke Aug 15 '14 at 20:14
  • @FKunecke Yeah, same error. I've looked around in my machine and been able to find two wlanapi.h files so far, both of which declare the function – EFTH Aug 15 '14 at 20:25
  • 2
    @ThomasMatthews : it should, but doesn't need to (implicit `return 0`) – quantdev Aug 15 '14 at 20:38
  • @ThomasMatthews: [What should main() return in C and C++?](http://stackoverflow.com/a/204483/1889329) – IInspectable Aug 16 '14 at 12:38
  • 2
    Make sure the preprocessor symbol `_WIN32_WINNT` is set to `_WIN32_WINNT_WINXP` (or higher, e.g. `_WIN32_WINNT_VISTA`, `_WIN32_WINNT_WIN7`). The symbol must be defined prior to including the `` header file. It's recommended to pass it on the compiler's command line. – IInspectable Aug 16 '14 at 12:47
  • 1
    @IInspectable Thank you! That worked. I updated the question to include this solution. If you put this in a real answer I can mark this question done and dealt with. – EFTH Aug 17 '14 at 07:51

1 Answers1

0

It looks like someone else has had this problem before:

How To Compile C++ Code that has a 'wlanapi.h' and 'windows.h' dependency

The recommended solution is to just put it into Visual Studio and compile it using that; MinGW probably isn't able to find the library.


Using VS2010 I created a VC++ console application (with a precompiled header), and I was able to get the following to compile without any errors:

// 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);

}

And here's my precompiled header:

// 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")
Community
  • 1
  • 1
Forest Kunecke
  • 2,160
  • 15
  • 32
  • The magic happens inside `"targetver.h"`. If you don't provide this file (which Visual Studio generated for you automatically), it does nothing to help answer the question. In addition, the question states a **compiler** error (vs. a **linker** error), so this has nothing to do with not finding a library. – IInspectable Aug 16 '14 at 13:02