调用windows系统动态库实现wifi连接及问题

本文介绍了如何使用C++调用Windows系统库来实现WiFi连接,并通过C#调用C++封装的接口。在实践中遇到三个问题:1) `fnWlanSetProfile`后有时直接连接成功;2) `fnWlanSetProfile`和`fnWlanConnect`成功但实际未连接;3) 不总是能确保连接成功。寻求解决方案。
摘要由CSDN通过智能技术生成

有个项目是c#做的, 有个需求是程序启动自动连接指定wifi,想到windows有个系统库支持,就用c++调的系统库,然后c#再调c++封装好的接口,比较简单,先上代码

c++动态库部分

静态调用的时候好像有问题,没细琢磨,就直接改用动态加载了

.h

#pragma once

#include <wlanapi.h>
#pragma comment(lib,"wlanapi.lib")
#pragma comment(lib,"ole32.lib")

typedef int WINBOOL;

typedef DWORD(WINAPI* pfnWlanGetAvailableNetworkList)(
	HANDLE hClientHandle,
	const GUID* pInterfaceGuid,
	DWORD dwFlags,
	PVOID pReserved,
	PWLAN_AVAILABLE_NETWORK_LIST* ppAvailableNetworkList
	);

typedef DWORD(WINAPI* pfnWlanOpenHandle)(
	DWORD dwClientVersion,
	PVOID pReserved,
	PDWORD pdwNegotiatedVersion,
	PHANDLE phClientHandle
	);

typedef DWORD(WINAPI* pfnWlanEnumInterfaces)(
	HANDLE hClientHandle,
	PVOID pReserved,
	PWLAN_INTERFACE_INFO_LIST* ppInterfaceList
	);

typedef DWORD(WINAPI* pfnWlanQueryInterface)(
	HANDLE hClientHandle,
	const GUID* pInterfaceGuid,
	WLAN_INTF_OPCODE OpCode,
	PVOID pReserved,
	PDWORD pdwDataSize,
	PVOID* ppData,
	PWLAN_OPCODE_VALUE_TYPE pWlanOpcodeValueType
	);

typedef DWORD(WINAPI* pfnWlanSetProfile)(
	HANDLE hClientHandle,
	const GUID* pInterfaceGuid,
	DWORD dwFlags,
	LPCWSTR strProfileXml,
	LPCWSTR strAllUserProfileSecurity,
	WINBOOL bOverwrite,
	PVOID pReserved,
	DWORD* pdwReasonCode
	);

typedef DWORD(WINAPI* pfnWlanSetInterface)(
	HANDLE hClientHandle,
	const GUID* pInterfaceGuid,
	WLAN_INTF_OPCODE OpCode,
	DWORD dwDataSize,
	const PVOID pData,
	PVOID pReserved
	);

typedef DWORD (WINAPI* pfnWlanConnect)(
	HANDLE hClientHandle,
	const GUID* pInterfaceGuid,
	const PWLAN_CONNECTION_PARAMETERS pConnectionParameters,
	PVOID pReserved
	);

typedef DWORD (WINAPI* pfnWlanDisconnect)(
	_In_ HANDLE hClientHandle,
	_In_ CONST GUID* pInterfaceGuid,
	_Reserved_ PVOID pReserved
);

typedef DWORD (WINAPI* pfnWlanCloseHandle)(
	_In_ HANDLE hClientHandle,
	_Reserved_ PVOID pReserved
);

typedef VOID (WINAPI* pfnWlanFreeMemory)(
	_In_ PVOID pMemory
);

class exportDLl
{
public:
	static exportDLl* m_pInstance;
	static exportDLl* getInstance();

public:
	bool pLoadLibrary();
	void pReleaseLibrary();
	int pFindWifi(const char* sWifiName);
	int connect(const char* sXmlName);
private:
	bool wifiInit();
	int isConnect(const char* sWifiName);
private:
	pfnWlanGetAvailableNetworkList fnWlanGetAvailableNetworkList = nullptr;
	pfnWlanOpenHandle fnWlanOpenHandle = nullptr;
	pfnWlanEnumInterfaces fnWlanEnumInterfaces = nullptr;
	pfnWlanQueryInterface fnWlanQueryInterface = nullptr;
	pfnWlanSetProfile fnWlanSetProfile = nullptr;
	pfnWlanSetInterface fnWlanSetInterface = nullptr;
	pfnWlanConnect  fnWlanConnect = nullptr;
	pfnWlanDisconnect fnWlanDisconnect = nullptr;
	pfnWlanCloseHandle fnWlanCloseHandle = nullptr;
	pfnWlanFreeMemory fnWlanFreeMemory = nullptr;


	HMODULE hDll = nullptr;

	HANDLE hClient = NULL;
	DWORD dwMaxClient = 2;
	DWORD dwCurVersion = 0;
	DWORD dwResult = 0;
	PWLAN_CONNECTION_ATTRIBUTES pConnectInfo = NULL;
	DWORD connectInfoSize = sizeof(WLAN_CONNECTION_ATTRIBUTES);
	WLAN_OPCODE_VALUE_TYPE opCode = wlan_opcode_value_type_invalid;
	PWLAN_INTERFACE_INFO pIfInfo = NULL;
	PWLAN_INTERFACE_INFO_LIST pIfList = NULL;

	bool m_bisLoad = false;
};


extern "C"
{
	__declspec(dllexport) bool pLoadLibrary();
	__declspec(dllexport) int pFindWifi(const char* path);
	__declspec(dllexport) int connect(const char* filename);
}



.cpp

#include "pch.h"
#include "exportDLl.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

static string string_to_hex(const string& str)
{
    string result = "";
    string tmp;
    std::stringstream ss;
    for (int i = 0; i < str.size(); i++)
    {
        ss << hex << int(str[i]) << endl;
        ss >> tmp;
        result += tmp;
    }
    return result;
}
static LPCWSTR StringToLPCWSTR(std::string orig)
{
    size_t origsize = orig.length() + 1;
    const size_t newsize = 100;
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值