有个项目是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;