Initialize a HANDLE in Visual Studio C++

Solved
daronmaster Posted messages 326 Registration date   Status Member Last intervention   -  
daronmaster Posted messages 326 Registration date   Status Member Last intervention   -
Hello,

I am currently working on a small project in Visual Studio 2008, which is a "Win32 console application" project.

In this project, I am using a function that needs to take multiple arguments, one of which is a HANDLE, but I can't find how to initialize this HANDLE.

In my opinion, the difficulty arises because it is a Win32 console application, and usually, it is WinForms projects that use HANDLEs.

I hope I have expressed myself clearly; if not, please let me know.

Thank you for your help
Configuration: Windows XP Internet Explorer 7.0

11 answers

BoOst
 
can you provide the function you use and its list of arguments?
if it is a window handle and you are not using it, you can pass the value NULL in principle, then it depends on the function of course ;)
0
BoOst
 
In fact, the handle "WIFI" must be obtained through "wlanOpenHandle", a function that you must call beforehand.

https://docs.microsoft.com/en-us/windows/win32/api/wlanapi/nf-wlanapi-wlanopenhandle?redirectedfrom=MSDN

You pass 1 or 2 into the "clientversion" parameter depending on the version of your Windows,
then you will obtain the handle to use in the following function in "phClientHandle".

(A Google search on these 2 function names should return some examples, depending on the language you are using)

Good luck :)
0
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
Well, one good thing about a forum is that you can reply to yourself:

In WlanopenHandle, if the last two parameters are null, then the function returns an error code, so no initialization to null for out1 and out2.

Okay, but now I have this error message:

Run-Time Check Failure #3 - The variable 'out2' is being used without being initialized.

And there I don't think I can help myself, so "please help me"

How to initialize a pointer without setting it to null?
0
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
here is the function used:

DWORD WINAPI WlanGetAvailableNetworkList(
__in HANDLE hClientHandle,
__in const GUID* pInterfaceGuid,
__in DWORD dwFlags,
__reserved PVOID pReserved,
__out PWLAN_AVAILABLE_NETWORK_LIST* ppAvailableNetworkList
);

the problem is that the handle cannot be null

thank you for your help
-1
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
By the way, I went to check on the MSDN and I don't see how to do it.
-1
bitrate Posted messages 441 Registration date   Status Member Last intervention   98
 
As stated on MSDN, the HANDLE requested as input is returned by the WlanOpenHandle function.

Just call it beforehand.

++
-1
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
Hello everyone, first of all thank you, and then I still need help

here is the famous function to use to get the handle:

DWORD WINAPI WlanOpenHandle(
__in DWORD dwClientVersion,
__reserved PVOID pReserved,
__out PDWORD pdwNegotiatedVersion,
__out PHANDLE phClientHandle
);

my first question (certainly silly): is PHANDLE a pointer to a HANDLE?

second question: here is the code as I use it:

PDWORD out1=NULL;

PHANDLE out2=NULL;

if (WlanOpenHandle(1,NULL,out1,out2) != ERROR_SUCCESS ) cout << "error openHandle" << endl;

PWLAN_AVAILABLE_NETWORK_LIST p;

char c1;

GUID g;

HRESULT res = CoCreateGuid(&g);

if (res != S_OK) cout << "error CoCreateGuid" << endl;

DWORD d = WlanGetAvailableNetworkList(*out2,(const GUID *)&g ,0x00000001,NULL,&p);

//here I have a switch on the result d to see if I have an error

when I compile I get an error:

Unhandled exception at 0x00412128 in Scan.exe: 0xC0000005: Access violation reading location 0x00000000.

Scan being the project name

is this possibly due to the third parameter of WlanGetAvailableNetworkList?

thank you again
-1
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
Well, I just saw that the error message in the console was "error openHandle", so the problem must come from the arguments passed. However, I don't think I made a mistake.

I need your help.

Thank you.
-1
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
Here is my new code:

DWORD out1;

HANDLE out2;

DWORD d1 = WlanOpenHandle(2,null,&out1,&out2);

switch(d1){

case ERROR_INVALID_PARAMETER :cout <<" error: parameter" << endl;break;

case ERROR_NOT_ENOUGH_MEMORY :cout <<" error: memory allocation" << endl;break;

case ERROR_REMOTE_SESSION_LIMIT_EXCEEDED:cout <<" error: too much handle" << endl;break;

case ERROR_SUCCESS:cout << "no problem" << endl;break;

default: cout << "default" << endl;break;
}

PWLAN_AVAILABLE_NETWORK_LIST p;

char c1;

GUID g;

HRESULT res = CoCreateGuid(&g);

if (res != S_OK) cout << "error :CoCreateGuid" << endl;

DWORD d = WlanGetAvailableNetworkList(out2,(const GUID *)&g ,WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES,NULL,&p);

switch(d){

case ERROR_SUCCESS : cout << " no problem " << endl;break;

case ERROR_INVALID_PARAMETER : cout << " error: parameter " << endl;break;

case ERROR_INVALID_HANDLE : cout << " error :handle " << endl;break;

case ERROR_NDIS_DOT11_POWER_STATE_INVALID : cout << " error :wifi turned off() " << endl;break;

//case RPC_STATUS : cout << " problem ??? " << endl; exit(-1);break;

case ERROR_NOT_ENOUGH_MEMORY : cout << " error :memory allocation" << endl;break;

default:break;
}

/*

result in the console (thanks to cout):

default
error :handle

then a window opens with the error message:

Windows has triggered a breakpoint in Scan.exe.

This may be due to a corruption of the heap, which indicates a bug in Scan.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while Scan.exe has focus.

The output window may have more diagnostic information.

you can click on break or continue

I do continue and the program terminates

this does not satisfy me because it means there is still a problem with the HANDLE

thank you for your help, I will return to the forum tonight, I have to go now

*/
-1
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
need help please
-1
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
s'il vous plaît
-1
daronmaster Posted messages 326 Registration date   Status Member Last intervention   44
 
I mark it as resolved.
-1