Skip to content

Access violation Exception while .ToStructure<WlanApi.WLAN_BSS_LIST>(); #565

@xieyx6

Description

@xieyx6

The exception Access violation reading location 0x07B44000 occurs in some machines when executing the following line of code:
var bss = handle.ToStructure<WlanApi.WLAN_BSS_LIST>();

Here is the code snippet where the exception occurs:

private static List<WlanBssEntry> UpdateWlanBssEntry(Guid wlanInterfaceGuid, WlanApi.WLAN_AVAILABLE_NETWORK network)
{
    if (string.IsNullOrEmpty(network.dot11Ssid.ToString()))
    {
        return null;
    }

    if (WlanApi.WlanGetNetworkBssList(_clientHandle, wlanInterfaceGuid, network.dot11Ssid, network.dot11BssType, network.bSecurityEnabled, IntPtr.Zero, out var bssList).Failed)
    {
        Logger.Warn("Failed to get BSS list for network: " + network.dot11Ssid);
        return null;
    }
    if (bssList.IsInvalid || bssList.IsClosed)
    {
        return null;
    }

    var entries = new List<WlanBssEntry>((int)network.uNumberOfBssids);
    using (bssList)
    {
        var handle = bssList.DangerousGetHandle();
        if (handle == IntPtr.Zero)
        {
            return null;
        }

        var bss = handle.ToStructure<WlanApi.WLAN_BSS_LIST>();
        for (var bssIndex = 0; bssIndex < bss.dwNumberOfItems; bssIndex++)
        {
            var bssEntry = bss.wlanBssEntries[bssIndex];
            var ie = new byte[bssEntry.ulIeSize];
            var pie = IntPtr.Add(handle, (int)bssEntry.ulIeOffset + (sizeof(uint) * 2) + (Marshal.SizeOf<WlanApi.WLAN_BSS_ENTRY>() * bssIndex));
            Marshal.Copy(pie, ie, 0, (int)bssEntry.ulIeSize);

            entries.Add(ToWlanBssEntry(bssEntry, ie));
        }
    }

    return entries.OrderByDescending(it => it.LRssi).ToList();
}

Here is the stack trace of the exception:

mscorlib.dll!System.Runtime.InteropServices.Marshal.PtrToStructure(System.IntPtr ptr, System.Type structureType) Line 1277
	at System.Runtime.InteropServices\Marshal.cs(1277)
Vanara.PInvoke.Shared.dll!Vanara.InteropServices.SafeAnysizeStructBase<Vanara.PInvoke.WlanApi.WLAN_BSS_LIST>.FromNative(System.IntPtr allocatedMemory, int size) Line 76
	at Vanara.InteropServices\SafeAnysizeStructBase.cs(76)
Vanara.PInvoke.Shared.dll!Vanara.InteropServices.SafeAnysizeStructBase<System.__Canon>.Value.get() Line 26
	at Vanara.InteropServices\SafeAnysizeStructBase.cs(26)
Vanara.PInvoke.Shared.dll!Vanara.InteropServices.SafeAnysizeStructMarshaler<Vanara.PInvoke.WlanApi.WLAN_BSS_LIST>.Vanara.InteropServices.IVanaraMarshaler.MarshalNativeToManaged(System.IntPtr pNativeData, Vanara.PInvoke.SizeT allocatedBytes) Line 38
	at Vanara.InteropServices\SafeAnysizeStructMarshaler.cs(38)
Vanara.Core.dll!Vanara.Extensions.InteropExtensions.ToStructure(System.IntPtr ptr, System.Type destType, Vanara.PInvoke.SizeT allocatedBytes, Vanara.PInvoke.SizeT offset, out int bytesRead) Line 1540
	at Vanara.Extensions\InteropExtensions.cs(1540)
CoreAddin.dll!CoreAddin.ContractHandlers.WlanInfoContract.WlanInfoMonitor.UpdateWlanBssEntry(System.Guid wlanInterfaceGuid, Vanara.PInvoke.WlanApi.WLAN_AVAILABLE_NETWORK network) Line 229
...

Unfortunately, this exception cannot be reproduced on my own machine. I would like to know if you have any ideas about this exception and if there is any way to catch or handle it. Thank you very much!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions