使用 WinAPI-Wlan QueryInterface- Delphi 以编程方式查询频道号

Programatically queries Channel Number using WinAPI-WlanQueryInterface- Delphi

在查询 WlanQueryInterface 以获取频道号时看到 return 值是一个大整数,如 1820789 等。有帮助吗?

uses nduWlanTypes, nduWlanAPI;

Function GetWifiChannelTest: String;

var

  hClient: THandle;
  dwVersion: DWORD;
  ResultInt: DWORD;
  pInterface: Pndu_WLAN_INTERFACE_INFO_LIST;
  i: Integer;
  pInterfaceGuid: TGUID;
  pdwDataSize: DWORD;
  ppData: Tndu_WLAN_INTF_OPCODE;
  pI: Pinteger;
  p: pvoid;

begin

  ResultInt := WlanOpenHandle(1, nil, @dwVersion, @hClient);

  try

    if ResultInt <> ERROR_SUCCESS then
    begin
      ShowMessage('Error Open CLient' + IntToStr(ResultInt));
      Exit;
    end;

    ResultInt := WlanEnumInterfaces(hClient, nil, @pInterface);

    if ResultInt <> ERROR_SUCCESS then
    begin
      ShowMessage('Error Enum Interfaces ' + IntToStr(ResultInt));
      Exit;
    end;

    for i := 0 to pInterface^.dwNumberOfItems - 1 do
    begin

      pInterfaceGuid := pInterface^.InterfaceInfo[pInterface^.dwIndex]
        .InterfaceGuid;
      ResultInt := WlanQueryInterface(hClient, @pInterfaceGuid,
        wlan_intf_opcode_channel_number, nil, @pdwDataSize, @ppData, nil);

      try

        if (ResultInt = ERROR_SUCCESS) and (pdwDataSize = SizeOf(ppData)) then
        begin
          p := @ppData;
          pI := pvoid(p);
          Result := IntToStr(pI^);
    // the result is 1820789 ,but i need channel number like 10, or 11 etc...
        end;

      except
      end;

    end;

  finally

    WlanCloseHandle(hClient, nil);

  end;

end;

您没有正确枚举接口。您不应使用 dwIndex 作为 InterfaceInfo[] 数组的索引。请改用循环计数器 i

此外,您没有正确调用 WlanQueryInterface()wlan_intf_opcode_channel_number 输出 ULONG 值,而不是 WLAN_INTF_OPCODE 值。

此外,您正在泄漏 WlanEnumInterfaces() 分配的 WLAN_INTERFACE_INFO_LIST

尝试更像这样的东西:

uses
  nduWlanTypes, nduWlanAPI;

Function GetWifiChannelTest: String;
var
  hClient: THandle;
  dwVersion: DWORD;
  ResultInt: DWORD;
  pIntfList: PWLAN_INTERFACE_INFO_LIST;
  i: DWORD;
  IntfGuid: TGUID;
  dwDataSize: DWORD;
  ChannelNumber: ULONG;
begin
  Result := '';

  ResultInt := WlanOpenHandle(1, nil, @dwVersion, @hClient);
  if ResultInt <> ERROR_SUCCESS then
  begin
    ShowMessage('Error Open Client: ' + IntToStr(ResultInt));
    Exit;
  end;

  try
    ResultInt := WlanEnumInterfaces(hClient, nil, @pIntfList);
    if ResultInt <> ERROR_SUCCESS then
    begin
      ShowMessage('Error Enumerating Interfaces: ' + IntToStr(ResultInt));
      Exit;
    end;

    try
      for i := 0 to pIntfList^.dwNumberOfItems - 1 do
      begin
        IntfGuid := pIntfList^.InterfaceInfo[i].InterfaceGuid;

        ResultInt := WlanQueryInterface(hClient, @IntfGuid, wlan_intf_opcode_channel_number, nil, @dwDataSize, @ChannelNumber, nil);
        if ResultInt = ERROR_SUCCESS then
        begin
          Result := IntToStr(ChannelNumber);
          Exit;
        end;
      end;
    finally
      WlanFreeMemory(pIntfList);
    end;
  finally
    WlanCloseHandle(hClient, nil);
  end;
end;

UPDATE:经过进一步审查,似乎 wlan_intf_opcode_channel_number 可能输出一个 指针 ULONG,所以你需要给它一个 指向 指向 ULONG 的指针,然后你可以取消引用输出的指针以获得实际的 ULONG。试试这个:

uses
  nduWlanTypes, nduWlanAPI;

Function GetWifiChannelTest: String;
var
  hClient: THandle;
  dwVersion: DWORD;
  ResultInt: DWORD;
  pIntfList: PWLAN_INTERFACE_INFO_LIST;
  i: DWORD;
  IntfGuid: TGUID;
  dwDataSize: DWORD;
  pChannelNumber: PULONG; // <--
begin
  Result := '';

  ResultInt := WlanOpenHandle(1, nil, @dwVersion, @hClient);
  if ResultInt <> ERROR_SUCCESS then
  begin
    ShowMessage('Error Open Client: ' + IntToStr(ResultInt));
    Exit;
  end;

  try
    ResultInt := WlanEnumInterfaces(hClient, nil, @pIntfList);
    if ResultInt <> ERROR_SUCCESS then
    begin
      ShowMessage('Error Enumerating Interfaces: ' + IntToStr(ResultInt));
      Exit;
    end;

    try
      for i := 0 to pIntfList^.dwNumberOfItems - 1 do
      begin
        IntfGuid := pIntfList^.InterfaceInfo[i].InterfaceGuid;

        ResultInt := WlanQueryInterface(hClient, @IntfGuid, wlan_intf_opcode_channel_number, nil, @dwDataSize, @pChannelNumber, nil);
        if ResultInt = ERROR_SUCCESS then
        begin
          Result := IntToStr(pChannelNumber^); // <--
          Exit;
        end;
      end;
    finally
      WlanFreeMemory(pIntfList);
    end;
  finally
    WlanCloseHandle(hClient, nil);
  end;
end;