我正在尝试调用WlanGet可用网络,但它失败并显示错误1168(ERROR_NOT_FOUND),然后重新运行成功。我认为这是我初始化out指针的方式,但是无法使其工作,即使我可以重复获取GUID指针。我正在构建一个托管包装供我自己使用,所以现在我的调用代码是一个Nunit框架。
运行失败的输出:
***** zz_Test_Fixture_TDD.NetworkListTester.NetworkListTest
Guid Pointer = 89327880
Client Handle and GUID pointers initialized
handle 1 Guid pointer = 89327880 Avail network pointer = 0
handle 1 Guid pointer = 89327880 Avail network pointer = 0
Wlan Get Avail networks Failed by error =1168
传球跑道输出:
***** zz_Test_Fixture_TDD.NetworkListTester.NetworkListTest
Guid Pointer = 89008544
Client Handle and GUID pointers initialized
handle 17 Guid pointer = 89008544 Avail network pointer = 0
handle 17 Guid pointer = 89008544 Avail network pointer = 89415752
Number of Networks Found = 18
这是我的代码:
[TestFixture]
public class NetworkListTester
{
IntPtr pclientHandle;
Guid ThisInterfaceGuid;
IntPtr pGuid;
[Test]
public void NetworkListTest()
{
pclientHandle = WiFiApi.GetClientHandle();
ThisInterfaceGuid = WiFiApi.GetCurrentInterfaceGuid(pclientHandle);
pGuid = WiFiApi.GetCurrentInterfaceGuidPointer(pclientHandle);
Console.WriteLine("Guid Pointer = " + pGuid);
Console.WriteLine("Client Handle and GUID pointers initialized");
IntPtr pAvailNetWorks = new IntPtr();
uint dwFlags = 3;
uint error = 99;
Console.WriteLine(" handle " + pclientHandle + " Guid pointer = " + pGuid + " Avail network pointer = " + pAvailNetWorks);
error = WiFiApi.WlanGetAvailableNetworkList(pclientHandle, pGuid, dwFlags, IntPtr.Zero, out pAvailNetWorks);
Console.WriteLine(" handle " + pclientHandle + " Guid pointer = " + pGuid + " Avail network pointer = " + pAvailNetWorks);
if (error != 0)
{
Console.WriteLine("Wlan Get Avail networks Failed by error =" + error);
//same impact with or without this retry
//also added a Thread.Sleep(60000); before the call with no impact
error = WiFiApi.WlanGetAvailableNetworkList(pclientHandle, pGuid, dwFlags, IntPtr.Zero, out pAvailNetWorks);
}
else
{
//tests intentionally outside of else so they will fail if an error.
}
//todo get managed pointer for available networks
WiFiApi.WLAN_AVAILABLE_NETWORKS_LIST NetworkList = (WiFiApi.WLAN_AVAILABLE_NETWORKS_LIST)Marshal.PtrToStructure(pAvailNetWorks, typeof(WiFiApi.WLAN_AVAILABLE_NETWORKS_LIST));
Console.WriteLine("Number of Networks Found = " + NetworkList.NumberOfNetworks);
Assert.Greater(NetworkList.NumberOfNetworks, 0);
Assert.Less(NetworkList.CurrentNetwork, NetworkList.NumberOfNetworks);
//close handle
WiFiApi.CloseClientHandle(pclientHandle);
WiFiApi.WlanFreeMemory(pGuid);
WiFiApi.WlanFreeMemory(pAvailNetWorks);
}
这是我正在调用的函数:
[DllImport("Wlanapi.dll")]
public static extern uint WlanGetAvailableNetworkList(
IntPtr _hClientHandle,
IntPtr _InterfaceGuid,
UInt32 _dwFlags,
IntPtr _pReserved,
out IntPtr ppAvailableNetworkList);