如何为WlanHostedNetworkSetProperty函数设置c#结构
本文关键字:设置 结构 函数 WlanHostedNetworkSetProperty | 更新日期: 2023-09-27 18:15:38
来自Microsoft文档:
DWORD WINAPI WlanHostedNetworkSetProperty(
_In_ HANDLE hClientHandle,
_In_ WLAN_HOSTED_NETWORK_OPCODE OpCode,
_In_ DWORD dwDataSize,
_In_ PVOID pvData,
_Out_opt_ PWLAN_HOSTED_NETWORK_REASON pFailReason,
_Reserved_ PVOID pvReserved
);
我实现 [DllImport("Wlanapi.dll", EntryPoint = "WlanHostedNetworkSetProperty")]
public static extern uint WlanHostedNetworkSetProperty(
IntPtr hClientHandle,
WLAN_HOSTED_NETWORK_OPCODE OpCode,
uint dwDataSize,
IntPtr pvData,
[Out] out WLAN_HOSTED_NETWORK_REASON pFailReason,
IntPtr pReserved
);
如果我只尝试启用或禁用Wlan,则此工作正常。但是,当我尝试设置ssid和对等体的数量时,我遇到了一个错误,因为pvData需要是指向以下结构体的指针:
来自Microsoft Documentation:
typedef struct _WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS {
DOT11_SSID hostedNetworkSSID;
DWORD dwMaxNumberOfPeers;
} WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS, *PWLAN_HOSTED_NETWORK_CONNECTION_SETTINGS;
和
typedef struct _DOT11_SSID {
ULONG uSSIDLength;
UCHAR ucSSID[DOT11_SSID_MAX_LENGTH];
} DOT11_SSID, *PDOT11_SSID;
这是我实现的两个结构:
public struct WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS
{
public DOT11_SSID hostedNetworkSSID;
public UInt32 dwMaxNumberOfPeers;
}
public struct DOT11_SSID
{
public UInt32 uSSIDLength;
public byte[] ucSSID;
}
最后,我是这样打电话的:
APCreator.WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS networkSetting = new APCreator.WLAN_HOSTED_NETWORK_CONNECTION_SETTINGS();
networkSetting.dwMaxNumberOfPeers = 5;
networkSetting.hostedNetworkSSID = new APCreator.DOT11_SSID();
networkSetting.hostedNetworkSSID.ucSSID = new byte[10];
//Populate ucSSID with network name
networkSetting.hostedNetworkSSID.uSSIDLength = 10;
int size = Marshal.SizeOf(networkSetting);
IntPtr pNetworkSetting = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(networkSetting, pNetworkSetting, true);
APCreator.WlanHostedNetworkSetProperty(HANDLE, APCreator.WLAN_HOSTED_NETWORK_OPCODE.wlan_hosted_network_opcode_connection_settings, (uint)size, pNetworkSetting, out ReasonEnum, Nill).ToString();
错误原因返回:wlan_hosted_network_reason_isufficient_resources
问题是:如何建立这些结构?
DOT11_SSID结构不正确。试试这个:
public struct DOT11_SSID
{
public UInt32 uSSIDLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DOT11_SSID_MAX_LENGTH)]
public byte[] ucSSID;
}
如果没有MarshalAs
属性,它试图将ucSSID
字段封送为指向字节数组的指针,而不是将该值包含在结构体中。
将DOT11_SSID_MAX_LENGTH
替换为标题中定义的任何值