Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WlanGetAvailableNetworkList contains maybe wrong amount of items (wlanapi.h) #911

Open
apo-d opened this issue Oct 12, 2024 · 1 comment
Labels
blocked Waiting on an external issue to be resolved bug Something isn't working p: win32 Issue with package:win32

Comments

@apo-d
Copy link

apo-d commented Oct 12, 2024

Description

Using the wlanapi.h method WlanGetAvailableNetworkList, i receive a WLAN_AVAILABLE_NETWORK_LIST with dwNumberOfItems > 1 (around 8), but when i try to access the availableNetworksList.Network[1] i get a Range error ("only valid value is 0: 1").

Steps to reproduce

  • import package:win32/win32.dart
  • call WlanOpenHandle to get the clientHandle
  • call WlanEnumInterfaces to get the guid of the network interface
  • call WlanGetAvailableNetworkList, get the WLAN_AVAILABLE_NETWORK_LIST struct, check the dwNumberOfItems and then iterate on the Networks array

Expected behavior

I can use a normal for-i loop until dwNumberOfItems - 1 and receive different WLAN_AVAILABLE_NETWORK items.

Environment

Windows 11, Dell computer

@apo-d apo-d added the bug Something isn't working label Oct 12, 2024
@halildurmus
Copy link
Owner

halildurmus commented Oct 12, 2024

(Related to #399)

The reason you're getting a RangeError when accessing elements of the Network array in the WLAN_AVAILABLE_NETWORK_LIST is because the array is annotated with @Array(1):

@Array(1)
external Array<WLAN_AVAILABLE_NETWORK> Network;

This annotation tells the compiler that the array contains only a single element, which leads to an out-of-bounds error when trying to access subsequent elements.

This array is actually defined as a variable-length array in C, but Dart doesn't support variable-sized arrays yet. As a result, it's projected as a single-element array. This causes bounds-checking issues when you try to access any element beyond the first.

To access the full list of networks, you can get a pointer to the beginning of the array by calculating the memory address of the array and then iterating over it:

extension on Pointer<WLAN_AVAILABLE_NETWORK_LIST> {
  List<WLAN_AVAILABLE_NETWORK> get network {
    final networks = <WLAN_AVAILABLE_NETWORK>[];

    // Get a pointer to the beginning of the array.
    final ptr = Pointer<WLAN_AVAILABLE_NETWORK>.fromAddress(
      address +
      sizeOf<Uint32>() + // dwNumberOfItems
      sizeOf<Uint32>()   // dwIndex
    );

    // Iterate through the networks.
    for (var i = 0; i < ref.dwNumberOfItems; i++) {
      final network = (ptr + i).ref;
      networks.add(network);
    }

    return networks;
  }
}

You can call this extension method from a pointer to the WLAN_AVAILABLE_NETWORK_LIST struct returned by the WlanGetAvailableNetworkList function. Please let me know if this works.

@halildurmus halildurmus added blocked Waiting on an external issue to be resolved p: win32 Issue with package:win32 labels Oct 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Waiting on an external issue to be resolved bug Something isn't working p: win32 Issue with package:win32
Projects
None yet
Development

No branches or pull requests

2 participants