使用WIFIMAN.DLL动态库结合Delphi实现Windows无线管理器。

本文介绍了一个基于WiFi-Manager DLL的Windows无线管理器实现,该DLL提供了丰富的接口用于管理和操作无线网络。作者通过Delphi封装了这个DLL,并提供了一系列实用的方法来控制无线网卡,包括连接、断开网络等。
摘要由CSDN通过智能技术生成

  去年的时候,公司让我做一个windows的无线管理器,那时候还是个愣头青,后来辗转找到了一个WIFI管理的DLL库——WiFi-Manager

  这个国外的库对windows的Wifi管理已经封装成了若干种方法,支持delphi和VS/VC以动态库的方式加载和使用它,官方给的DEMO也相当完整。当然,它是收费的。我偶然间得到了已经购买的DLL,在使用过程中算是比较顺利。(免费版在使用的时候会提示)

  那时候因为是预演的项目,所以在健壮性上我也没做多少优化,只是大概的把这个DLL提供的各种函数,用Delphi封装成了一个类。写得比较粗糙。

  软件的挫样:

 

 

  WifiController类

unit WifiController;

//=============WiFiController Class================
//=============  Create 2010/11/4  ================
//=============        YanJie      ================
//=============    Version:v0.1    ================

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, WiFiMan, WiFiDefine;

type


//===========Ctrler Class===============

  TWiFiController = class
  private
    function SetProfile(ProfHandle: Integer; rProf: rProfile; nAdpIndex: Integer): Boolean;      //设置profile
  public
    hFatherHandle: HWND;
    strWiFimanVersion: string;
    rAdapters: rAdaptersRecord;
    rAvaiNets: rArrAvailableNetRecord;
    rProfiles: rArrProfRecord;
    adpUsedIndex: Integer;     //当前使用的网卡索引
    strNetUsed: string;        //当前使用的网络

    constructor Create(hCallHandle: HWND); overload;
    destructor Destroy; override;

    function RefreshAdapaters: Boolean;                         //获取网卡信息
    function RefreshTrafficData(nIndex: Integer): Boolean;      //获取网卡的流量信息

    function RefreshNets(nAdpIndex, nMode: Integer): Boolean;            //获取网络信息 mode:1.搜索可用网络;0.返回当前列表
    function RefreshNetQuality(nAdpIndex, nNetIndex: Integer): Boolean;  //获取可用网络的信号质量

    function RefreshProf(nAdpIndex: Integer): Boolean;      //获取配置文件信息
    function NewProfile(rProf: rProfile; nAdpIndex: Integer): Boolean;    //新配置文件
    function EditProfile(nProfIndex: Integer; rProf: rProfile; nAdpIndex: Integer): Boolean;   //编辑
    function DelProfile(nAdpIndex: Integer; nProfIndex: Integer): Boolean; //删除
    function CreateProfileFromNet(nAdpIndex: Integer; nNetIndex: Integer; strKey: string; isAuto: Boolean; isBroadcast: Boolean; isC2C: Boolean; isUseWinC: Boolean; nKeyIndex: Integer): Boolean; //创建网络对应的配置文件



    function ConnectByName(nAdpIndex: integer; strNetName: string): Boolean;  //连接
    function Disconnect(nAdpIndex: integer): Boolean;  //断开

    procedure WaitForAdapter; //等待网卡执行

  end;

pWiFiCtrler = ^TWiFiController;


function HEX(b:byte):string;

function GetAuthByNum(mode:integer): enumAuthMode;
function GetAuthByStr(pBuf: PAnsiChar): enumAuthMode;
function GetAuthStr(eAuth: enumAuthMode): string;

function GetCipherByNum(mode:integer): enumCipherMode;
function GetCipherByStr(pBuf: PAnsiChar): enumCipherMode;
function GetCipherStr(eCip: enumCipherMode): string;

function GetConnectionType(pBuf: PAnsiChar): enumConnectionType;

implementation

function HEX(b:byte):string;
begin
  result:=inttohex(b, 2);
end;

constructor TWiFiController.Create(hCallHandle: HWND);
var
  status, ver, i: Integer;
begin
  Self.hFatherHandle := hCallHandle;

  Self.adpUsedIndex := -1;
  Self.strNetUsed := '';

  for i := 0 to 55 do
  begin
    Self.rAvaiNets.arrNets[i].nIndex := -1;
    Self.rProfiles.arrProf[i].nIndex := -1;
  end;

  for i := 0 to 9 do
  begin
    Self.rAdapters.arrAdapaters[i].nIndex := -1;
  end;

  ver := GetWIFIManagerVersion;
  Self.strWiFimanVersion := inttostr(ver div 100) + '.' + inttostr((ver mod 100) div 10) + '.' + inttostr(ver mod 10);

  status := GetWIFIServiceStatus;
  if status = 0 then  //开启windows WiFi Service
  begin
    SetWIFIServiceStatus(1); //start service
  end;

  status := GetWIFIServiceStatus;
  if status = 0 then
  begin
    SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_NO_SERVICE, status);
    Exit;
  end;

//===Enumerate Adapters====
  //Self.RefreshAdapaters;

//===Enumerate Available Networks===
  //Self.RefreshNets(adpUsedIndex, 1);

//===Enumerate Profile===
  //Self.RefreshProf(adpUsedIndex);

  EnableLog('log.txt', 1);
  WriteLog('Wireless Log');

end;

destructor TWifiController.Destroy;
begin
  Self.Disconnect(Self.adpUsedIndex);
  FreeAllResources;
  inherited;
end;


function TWiFiController.RefreshAdapaters: Boolean;
var
  res, i, n: Integer;
  buf: array[0..511] of Char;
  ip, mask, gate, dns:array[0..511] of Char;
  strBuf: string;
  d1, d2, d3, d4, d5, d6: Byte;
begin

  Result := True;
  res := EnumerateAdapters;
  Self.rAdapters.nSum := 0;

  ZeroMemory( @Self.rAdapters, SizeOf(rAdaptersRecord) );

  //FillMemory( @Self.rAdapters, SizeOf(rAdaptersRecord), Byte(-1) );

 // SendMessage(Self.hFatherHandle, WIFI_MSG, 0, 0);


  if res > ERROR_OFFSET then
  begin
    SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_ADP_ERR, res);
    Result := False;
    Exit;
  end;

  Self.rAdapters.nSum := res;

  
  for i:=0 to res-1 do
  begin
    Self.rAdapters.arrAdapaters[i].isUsed := False;

    ZeroMemory(@buf,SizeOf(buf));

    Self.rAdapters.arrAdapaters[i].nIndex := i;
    //===getName===
    n := GetAdapterName(i, buf, SizeOf(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_ADP_ERR, n);
      Result := False;
      Continue;
    end;
    strBuf := StrPas(buf);
    Self.rAdapters.arrAdapaters[i].strName := strBuf;
    //===getGUID===
    n := GetAdapterGUID(i, buf, SizeOf(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_ADP_ERR, n);
      Result := False;
      Continue;
    end;
    strBuf := StrPas(buf);
    Self.rAdapters.arrAdapaters[i].strGUID := strBuf;

//========getNetworkImformation========

    //===Name===
    n := GetCurrentNetworkName(i, buf, SizeOf(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_ADP_ERR, n);
      Result := False;
      Continue;
    end;
    strBuf := StrPas(buf);
    Self.rAdapters.arrAdapaters[i].strNetName := strBuf;
    Self.strNetUsed := strBuf;
    if buf = '' then    //没在使用网络
    begin
      Self.rAdapters.arrAdapaters[i].strNetName := '';
      Self.rAdapters.arrAdapaters[i].strNetMac := '';
      Self.rAdapters.arrAdapaters[i].nNetSpeed := 0;
    end
    else    //如果正在使用网络才进行MAC的获取
    begin
      Self.rAdapters.arrAdapaters[i].isUsed := True;
      Self.adpUsedIndex := i;
      //===MAC===
      n := GetCurrentNetworkMac(i, d1, d2, d3, d4, d5, d6);
      if n > ERROR_OFFSET then
      begin
        Self.rAdapters.arrAdapaters[i].strNetMac := '';
      end
      else
      begin
        Self.rAdapters.arrAdapaters[i].strNetMac := HEX(d1)+':'+HEX(d2)+':'+HEX(d3)+':'+HEX(d4)+':'+HEX(d5)+':'+HEX(d6);
      end;
    end;

    //===Speed===
    n := GetCurrentNetworkSpeed(i);
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_ADP_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rAdapters.arrAdapaters[i].nNetSpeed := n;

    //===Channel===
    n := GetCurrentNetworkChannel(i);
    if n > ERROR_OFFSET then Self.rAdapters.arrAdapaters[i].strNetChannel := ''
    else Self.rAdapters.arrAdapaters[i].strNetChannel := inttostr(n);

    //===IP Info===
    ZeroMemory(@ip,SizeOf(ip));
    ZeroMemory(@mask,SizeOf(mask));
    ZeroMemory(@gate,SizeOf(gate));
    n := GetAdapterCurrentIPInfo(i, ip, mask, gate, 255);
    if n > ERROR_OFFSET then
    begin
      Self.rAdapters.arrAdapaters[i].strIP := '';
      Self.rAdapters.arrAdapaters[i].strMask := '';
      Self.rAdapters.arrAdapaters[i].strGate := '';
    end
    else
    begin
      Self.rAdapters.arrAdapaters[i].strIP := StrPas(ip);
      Self.rAdapters.arrAdapaters[i].strMask := StrPas(mask);
      Self.rAdapters.arrAdapaters[i].strGate := StrPas(gate);
    end;

    //===DNS Info===
    ZeroMemory(@dns,SizeOf(dns));
    n := GetAdapterDNS(i, dns, 255);
    if n > ERROR_OFFSET then
    begin
      Self.rAdapters.arrAdapaters[i].strDns := '';
    end
    else Self.rAdapters.arrAdapaters[i].strDns := StrPas(dns);

//========Adapter Info=======
    //===MAC===
    n := GetAdapterMac(i, d1, d2, d3, d4, d5, d6);
    if n > ERROR_OFFSET then Self.rAdapters.arrAdapaters[i].strAdapterMAC := ''
    else Self.rAdapters.arrAdapaters[i].strAdapterMAC := HEX(d1)+':'+HEX(d2)+':'+HEX(d3)+':'+HEX(d4)+':'+HEX(d5)+':'+HEX(d6);
    //===Is Native===
//    if IsAdapterNativeWIFI(i) = 1 then Self.rAdapters.arrAdapaters[i].isNative := True
//    else Self.rAdapters.arrAdapaters[i].isNative := False;
    //===Traffic===
    Self.RefreshTrafficData(i);

    Result := True;


  end;
end;


function TWiFiController.RefreshTrafficData(nIndex: Integer): Boolean;
var
  n: Integer;
  InH, InL, OutH, OutL: DWORD;
begin
  n := GetAdapterTraffic(nIndex, InH, InL, OutH, OutL);
  if n > ERROR_OFFSET then
  begin
    //Self.rAdapters.arrAdapaters[nIndex].nInData := 0;
    //Self.rAdapters.arrAdapaters[nIndex].nOutData := 0;
    Result := False;
  end
  else
  begin
    Self.rAdapters.arrAdapaters[nIndex].nInData := InL + int64(InH) shl 32;     //bytes
    Self.rAdapters.arrAdapaters[nIndex].nOutData := OutL + int64(OutH) shl 32;
    Result := True;
  end;
end;

function TWiFiController.RefreshNets(nAdpIndex, nMode: Integer): Boolean;
var
  n, i, res: Integer;
  buf: array[0..511] of ansichar;
  d1, d2, d3, d4, d5, d6: byte;
begin

  Self.rAvaiNets.nSum := 0;
  Result := True;

  //获取可用网络信息,返回个数
  res := EnumerateAvailableNetworks(nAdpIndex, nMode);
  if res > ERROR_OFFSET then
  begin
    SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, res);
    Result := False;
    Exit;
  end;
  Self.rAvaiNets.nSum := res;

  //遍历网络
  for i := 0 to res - 1 do
  begin

    Self.rAvaiNets.arrNets[i].nIndex := i;

    ZeroMemory(@buf, SizeOf(buf));

    //====Name====
    n := GetAvailableNetworkName(nAdpIndex, i, buf, SizeOf(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rAvaiNets.arrNets[i].strName := StrPas(buf);
    //====MAC====
    n := GetAvailableNetworkMac(nAdpIndex, i, d1, d2, d3, d4, d5, d6);
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rAvaiNets.arrNets[i].strMAC := HEX(d1)+':'+HEX(d2)+':'+HEX(d3)+':'+HEX(d4)+':'+HEX(d5)+':'+HEX(d6);
    //====Type====
    n := GetAvailableNetworkType(nAdpIndex, i);
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Continue;
    end;
    case n of
      dot11_BSS_type_infrastructure: Self.rAvaiNets.arrNets[i].strType := 'Infrastructure';
      dot11_BSS_type_independent: Self.rAvaiNets.arrNets[i].strType := 'Ad hoc';
      dot11_BSS_type_any: Self.rAvaiNets.arrNets[i].strType := '';
    else
      Self.rAvaiNets.arrNets[i].strType := 'Unknown(' + IntToStr(n) + ')';
    end;
    //====Quality====
    Self.RefreshNetQuality(nAdpIndex, i);
    //====Secure====
    n := IsAvailableNetworkSecure(nAdpIndex, i);
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Continue;
    end;
    if n = 0 then
    begin
      Self.rAvaiNets.arrNets[i].isSecure := False;
    end
    else
    begin
      Self.rAvaiNets.arrNets[i].isSecure := True;
    end;
    //====AuthMode====
    n := GetAvailableNetworkAuthMode(nAdpIndex, i);
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rAvaiNets.arrNets[i].eAuthMode := GetAuthByNum(n);
    //====CipherMode====
    n := GetAvailableNetworkCipherMode(nAdpIndex, i);
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rAvaiNets.arrNets[i].eCipherMode := GetCipherByNum(n);
    //====Channel====
    n := GetAvailableNetworkChannel(nAdpIndex, i);
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rAvaiNets.arrNets[i].nChannel := n;
  end;

end;



function TWiFiController.RefreshNetQuality(nAdpIndex, nNetIndex: Integer): Boolean;
var
  n: Integer;
begin
  //====SignalQuality====
  n:=GetAvailableNetworkSignalQuality(nAdpIndex, nNetIndex);
  if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Exit;
    end;
  Self.rAvaiNets.arrNets[nNetIndex].nSignalQuality := n;
  //====RSSI====
  n:=GetAvailableNetworkRSSI(nAdpIndex, nNetIndex);
  if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_NET_ERR, n);
      Result := False;
      Exit;
    end;
  Self.rAvaiNets.arrNets[nNetIndex].nRSSI := n;
  Result := True;
end;

function TWiFiController.RefreshProf(nAdpIndex: Integer): Boolean;
var
  res, i, n, ProfHandle: Integer;
  buf: array[0..511] of ansichar;
begin

  Self.rProfiles.nSum := 0;
  Result := True;

  //SendMessage(Self.hFatherHandle, WIFI_MSG, 0, 0);

  

  Self.WaitForAdapter;

  res:=EnumerateProfiles(nAdpIndex);
  if res>ERROR_OFFSET then
  begin
    SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_ENUM_PROF_ERR, res);
    Result := False;
    Exit;
  end;
  
  Self.rProfiles.nSum := res;

  for i:=0 to res-1 do
  begin

    Self.rProfiles.arrProf[i].nIndex := i;
    //Self.WaitForAdapter;
    //====Prof Name====
    n := GetProfileName(nAdpIndex, i, buf, sizeof(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_READ_PROF_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rProfiles.arrProf[i].strName := StrPas(@buf);

    ProfHandle := GetTmpProfileFromAdapter(nAdpIndex, i);
    if ProfHandle > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_READ_PROF_ERR, ProfHandle);
      Result := False;
      Continue;
    end;

    //====Prof 对应网络名====
    n := GetTmpProfileOption(ProfHandle, '.ssid', buf, sizeof(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_READ_PROF_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rProfiles.arrProf[i].strNetName := StrPas(@buf);

    //====Prof 是否广播====
    n := GetTmpProfileOption(ProfHandle, '.nonBroadcast', buf, sizeof(buf));
    if n > ERROR_OFFSET then
    begin
      Self.rProfiles.arrProf[i].isBroadcast := True;
    end
    else
      Self.rProfiles.arrProf[i].isBroadcast := not (CompareText(buf, 'true') = 0);

    //====Prof 鉴权方式====
    n := GetTmpProfileOption(ProfHandle, '.Authentication', buf, sizeof(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_READ_PROF_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rProfiles.arrProf[i].eAuthentication := GetAuthByStr(buf);

    //====Prof Encryption====
    n := GetTmpProfileOption(ProfHandle, '.Encryption', buf, sizeof(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_READ_PROF_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rProfiles.arrProf[i].eEncryption := GetCipherByStr(buf);

    //====Prof is KeyProtected====
    n := GetTmpProfileOption(ProfHandle, '.IsKeyProtected', buf, sizeof(buf));
    if n > ERROR_OFFSET then
    begin
      Self.rProfiles.arrProf[i].isKeyProtected := False;
    end
    else
      Self.rProfiles.arrProf[i].isKeyProtected := (CompareText(buf, 'true') = 0);

    //====Prof Key====
    n := GetTmpProfileOption(ProfHandle, '.KeyMaterial', buf, sizeof(buf));
    if (n > ERROR_OFFSET) or (Self.rProfiles.arrProf[i].isKeyProtected) then
    begin
      Self.rProfiles.arrProf[i].strKey := '';
    end
    else
      Self.rProfiles.arrProf[i].strKey := StrPas(@buf);

    //====Prof Key Index====
    n := GetTmpProfileOption(ProfHandle, '.KeyIndex', buf, sizeof(buf));
    if n > ERROR_OFFSET then Self.rProfiles.arrProf[i].nKeyIndex := 1
    else Self.rProfiles.arrProf[i].nKeyIndex := StrToIntDef(buf, 0) + 1;

    //====autoconnect===
    n := GetTmpProfileOption(ProfHandle, '.ConnectionMode', buf, sizeof(buf));
    if n > ERROR_OFFSET then Self.rProfiles.arrProf[i].isAutoConnect := True
    else Self.rProfiles.arrProf[i].isAutoConnect := (CompareText(buf, 'auto') = 0);

    //====network type===
    n := GetTmpProfileOption(ProfHandle, '.ConnectionType', buf, sizeof(buf));
    if n > ERROR_OFFSET then
    begin
      SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_READ_PROF_ERR, n);
      Result := False;
      Continue;
    end;
    Self.rProfiles.arrProf[i].eConnectionType := GetConnectionType(@buf);

    //====802.1x====
    n := GetTmpProfileOption(ProfHandle, '.PeapUseWindowsCredentials', buf, sizeof(buf));
    if n > ERROR_OFFSET then Self.rProfiles.arrProf[i].isUseWinCredentials := False
    else Self.rProfiles.arrProf[i].isUseWinCredentials := (CompareText(buf, 'true') = 0);

    FreeTmpProfile(ProfHandle);
  end;
end;



function TWiFiController.SetProfile(ProfHandle: Integer; rProf: rProfile; nAdpIndex: Integer): Boolean;
var
  nIEE802, nWinCred, res: Integer;
begin

  SetTmpProfileOption(ProfHandle, '.name', UnicodeToAnsi(rProf.strName) );
  SetTmpProfileOption(ProfHandle, '.ssid', UnicodeToAnsi(rProf.strNetName) );

  if rProf.isBroadcast then
    DeleteTmpProfileOption(ProfHandle, '.nonBroadcast')
  else
    SetTmpProfileOption(ProfHandle, '.nonBroadcast', 'true');


  if rProf.eConnectionType = IBSS then
    SetTmpProfileOption(ProfHandle, '.ConnectionType', 'IBSS')
  else
    SetTmpProfileOption(ProfHandle, '.ConnectionType', 'ESS');



  if rProf.isAutoConnect then
    SetTmpProfileOption(ProfHandle, '.ConnectionMode', 'auto')
  else
    SetTmpProfileOption(ProfHandle, '.ConnectionMode', 'manual');

  SetTmpProfileOption(ProfHandle, '.Authentication', UnicodeToAnsi(GetAuthStr(rProf.eAuthentication)) );

  SetTmpProfileOption(ProfHandle, '.Encryption', UnicodeToAnsi(GetCipherStr(rProf.eEncryption)) );


  case rProf.eEncryption of
    NONE, WEP:
    begin
      rProf.eKeyType := NETWORKKEY;
      SetTmpProfileOption(ProfHandle, '.KeyType', 'networkkey');
    end;
    TKIP, AES:
    begin
      rProf.eKeyType := PASSPHRASE;
      SetTmpProfileOption(ProfHandle, '.KeyType', 'passPhrase')
    end;  
  else
    begin
      rProf.eKeyType := NETWORKKEY;
      SetTmpProfileOption(ProfHandle, '.KeyType', 'networkkey');
    end;
  end;


  if rProf.isKeyProtected = False then
  begin
    SetTmpProfileOption(ProfHandle, '.IsKeyProtected', 'False');
  end;

  SetTmpProfileOption(ProfHandle, '.KeyMaterial', UnicodeToAnsi(rProf.strKey) );

  SetTmpProfileOption(ProfHandle, '.KeyIndex', UnicodeToAnsi(inttostr(rProf.nKeyIndex)) );

  {$IFDEF DEBUG}
  SaveTmpProfile(ProfHandle, 'c:\e.txt'); //DEBUG
  {$ENDIF}
  
  nIEE802 := 0;
  nWinCred := 0;

  if (rProf.eAuthentication = WPA) or (rProf.eAuthentication = WPA2) then
  begin
    nIEE802 := 1;

    if rProf.isUseWinCredentials then
      nWinCred := 1
    else
      nWinCred := 0;
  end;

  SetDefaultEapConfig(ProfHandle, nIEE802, nWinCred);

  res := SetTmpProfileToAdapter(ProfHandle, nAdpIndex);
  
  FreeTmpProfile(ProfHandle);

  if res > ERROR_OFFSET then
  begin
    SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_SET_PROF_ERR, res);
    Result := False;
    Exit;
  end;


  Result := True;
end;

function TWiFiController.NewProfile(rProf: rProfile; nAdpIndex: Integer): Boolean;
var
  ProfHandle: Integer;
begin
  ProfHandle := CreateTmpProfile(0);
  Result := Self.SetProfile(ProfHandle, rProf, nAdpIndex);
end;  

function TWiFiController.EditProfile(nProfIndex: Integer; rProf: rProfile; nAdpIndex: Integer): Boolean;
var
  ProfHandle: Integer;
begin
  ProfHandle := GetTmpProfileFromAdapter(nAdpIndex, nProfIndex);
  Result := Self.SetProfile(ProfHandle, rProf, nAdpIndex);
end;

function TWiFiController.DelProfile(nAdpIndex: Integer; nProfIndex: Integer): Boolean;
var
  res: Integer;
begin
  res := DeleteProfile(nAdpIndex, nProfIndex);
  if res > ERROR_OFFSET then
  begin
    SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_DEL_PROF_ERR, res);
    Result := False;
    Exit;
  end;
  Result := True;
end;  

function TWiFiController.CreateProfileFromNet(nAdpIndex: Integer; nNetIndex: Integer; strKey: string; isAuto: Boolean; isBroadcast: Boolean; isC2C: Boolean; isUseWinC: Boolean; nKeyIndex: Integer): Boolean;
var
  rProf: rProfile;
begin
  rProf.strName := Self.rAvaiNets.arrNets[nNetIndex].strName;
  rProf.strNetName := Self.rAvaiNets.arrNets[nNetIndex].strName;
  rProf.eAuthentication := Self.rAvaiNets.arrNets[nNetIndex].eAuthMode;
  rProf.eEncryption := Self.rAvaiNets.arrNets[nNetIndex].eCipherMode;

  rProf.isKeyProtected := False;

  // ===== 用户选择 =====
  rProf.isAutoConnect := isAuto;
  rProf.isBroadcast := isBroadcast;

  if isC2C then
    rProf.eConnectionType := IBSS
  else
    rProf.eConnectionType := ESS;

  rProf.nKeyIndex := nKeyIndex;

  rProf.isUseWinCredentials := isUseWinC;

  if Self.rAvaiNets.arrNets[nNetIndex].isSecure then
  begin
    rProf.strKey := strKey;
  end
  else
    rProf.isKeyProtected := False;

  if Self.NewProfile(rProf, nAdpIndex) then
    Result := True
  else
    Result := False;

end;  




function TWiFiController.ConnectByName(nAdpIndex: integer; strNetName: string): Boolean;
var
  res: Integer;
begin
  Self.WaitForAdapter;
  res := ConnectToNetwork(nAdpIndex, UnicodeToAnsi(strNetName) );    //连接必须有该连接名的配置文件profile
  if res > ERROR_OFFSET then
  begin
    SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_CON_ERR, res);
    Result := False;
    Exit;
  end;
  Self.strNetUsed := strNetName;
  //CheckAdapterBusyStatus(nAdpIndex, 7000);
  Result := True;
end;  

function TWiFiController.Disconnect(nAdpIndex: integer): Boolean;
var
  res: Integer;
begin
  res := DisconnectFromNetwork(nAdpIndex);
  if res > ERROR_OFFSET then
  begin
    SendMessage(Self.hFatherHandle, WIFI_MSG, MSG_DISCON_ERR, res);
    Result := False;
    Exit;
  end;
  Self.strNetUsed := '';
  Self.adpUsedIndex := 0;
  Result := True;
end;  

procedure TWiFiController.WaitForAdapter;
begin
  CheckAdapterBusyStatus(Self.adpUsedIndex, 7000);
end;  


function GetAuthByNum(mode: integer): enumAuthMode;
begin
  case mode of
    Ndis802_11AuthModeOpen: result:=OPEN;
    Ndis802_11AuthModeShared: result:=SHARED;
    Ndis802_11AuthModeAutoSwitch: result:=AUTOSWITCH;
    Ndis802_11AuthModeWPA: result:=WPA;
    Ndis802_11AuthModeWPAPSK: result:=WPAPSK;
    Ndis802_11AuthModeWPANone: result:=WPANONE;
    Ndis802_11AuthModeWPA2: result:=WPA2;
    Ndis802_11AuthModeWPA2PSK: result:=WPA2PSK;
   else result:=AUTH_UNKNOW;
  end;
end;

function GetAuthByStr(pBuf: PAnsiChar): enumAuthMode;
begin
  Result := OPEN;
  if CompareText(pBuf, 'open')=0 then Result := OPEN;
  if CompareText(pBuf, 'shared')=0 then Result := SHARED;
  if CompareText(pBuf, 'WPA')=0 then Result := WPA;
  if CompareText(pBuf, 'WPAPSK')=0 then Result := WPAPSK;
  if CompareText(pBuf, 'WPA2')=0 then Result := WPA2;
  if CompareText(pBuf, 'WPA2PSK')=0 then Result := WPA2PSK;
end;

function GetAuthStr(eAuth: enumAuthMode): string;
begin
  case eAuth of
    OPEN:    Result := 'open';
    SHARED:  Result := 'shared';
    WPA:     Result := 'WPA';
    WPAPSK:  Result := 'WPAPSK';
    WPA2:    Result := 'WPA2';
    WPA2PSK: Result := 'WPA2PSK';
  else
    Result := 'open';
  end;
end;  


function GetCipherByNum(mode: integer): enumCipherMode;
begin
  case mode of
    DOT11_CIPHER_ALGO_NONE: result:=NONE;
    DOT11_CIPHER_ALGO_WEP40: result:=WEP40;
    DOT11_CIPHER_ALGO_TKIP: result:=TKIP;
    DOT11_CIPHER_ALGO_CCMP: result:=AES;
    DOT11_CIPHER_ALGO_WEP104: result:=WEP104;
    DOT11_CIPHER_ALGO_WPA_USE_GROUP: result:=WPAGROUP;
    DOT11_CIPHER_ALGO_WEP: result:=WEP;
   else result:=ENC_UNKNOW;
  end;
end;

function GetCipherByStr(pBuf: PAnsiChar): enumCipherMode;
begin
  Result := NONE;
  if CompareText(pBuf, 'none')=0 then Result := NONE;
  if CompareText(pBuf, 'WEP')=0 then Result := WEP;
  if CompareText(pBuf, 'TKIP')=0 then Result := TKIP;
  if CompareText(pBuf, 'AES')=0 then Result := AES;
end;  

function GetCipherStr(eCip: enumCipherMode): string;
begin
  case eCip of
    NONE:    Result := 'none';
    WEP:     Result := 'WEP';
    TKIP:    Result := 'TKIP';
    AES:     Result := 'AES';
  else
    Result := 'none';
  end;
end;  

function GetConnectionType(pBuf: PAnsiChar): enumConnectionType;
begin
  if StrPas(pBuf) = 'ESS' then
    Result := ESS
  else if StrPas(pBuf) = 'IBSS' then
    Result := IBSS
  else
    Result := CT_UNKNOW;
end;

end.

  WiFiDefined:

unit WiFiDefine;

interface

uses
  Messages, Windows, Classes, ComDll, Registry, WiFiMan;

const


//==========MSG============
  WIFI_MSG        = WM_USER + 200;
  WIFI_CREATE     = WM_USER + 201;
  WIFI_CONNECT    = WM_USER + 202;
  WIFI_REFRESH    = WM_USER + 203;
  WIFI_DISCONNECT = WM_USER + 204;



  
//==========wParam==========
  MSG_NO_SERVICE        = $00007001;
  MSG_ENUM_ADP_ERR      = $00007002;
  MSG_ENUM_NET_ERR      = $00007003;
  MSG_ENUM_PROF_ERR     = $00007004;
  MSG_SET_PROF_ERR      = $00007005;
  MSG_DEL_PROF_ERR      = $00007006;
  MSG_CON_ERR           = $00007007;
  MSG_DISCON_ERR        = $00007008;
  MSG_READ_PROF_ERR     = $00007009;


  MSG_CREATE_SUCCESS    = $00007101;
  MSG_REFLESH_SUCCESS   = $00007102;
  MSG_HAS_CON           = $00007103;
  MSG_INIT_TRAFFIC      = $00007104;
  MSG_DISCON_SUC        = $00007105;

type
  //===========Enum===============
  enumAuthMode = (      //网络鉴权模式
    OPEN,
    SHARED,
    AUTOSWITCH,
    WPA,
    WPAPSK,
    WPANONE,
    WPA2,
    WPA2PSK,
    AUTH_UNKNOW
  );

  enumCipherMode = (      //加密模式
    NONE,
    WEP40,
    TKIP,
    AES,
    WEP104,
    WPAGROUP,
    WEP,
    ENC_UNKNOW
  );

  enumKeyType = (      //密码格式
    NETWORKKEY,
    PASSPHRASE
  );

  enumConnectionType = (     //连接模式
    IBSS,
    ESS,
    CT_UNKNOW
  );

//  enumEapType = (       //Eap
//    NONE,
//    PEAP,
//    TTLS,
//    TLS,
//    MD5,
//    MSCHAPV2
//  );

  rConnStu = (       //连接状态
    DISCONNECTED,
    DISCONNECTING,
    CONNECTING,
    CONNECTED
  );



type

  //=============records==================

  rAdapaterProperty = packed record   //网卡信息
    nIndex: Integer;          // 索引
    strName: string;          // 网卡名称
    strGUID: string;          // GUID
    strNetName: string;       // 当前网络名
    strNetMac: string;        // MAC
    nNetSpeed: Integer;       // Speed  bit/s
    strNetChannel: string;    // Channel
    strIP: string;            // IP
    strMask: string;          // Mask
    strGate: string;          // Gate
    strDns: string;           // Dns
    strAdapterMAC: string;    // AdapterMAC
    isUsed: Boolean;          // Used
    nInData: Int64;           // InData     bytes
    nOutData: Int64;          // OutData    bytes
  end;


  rAdaptersRecord = packed record   //网卡集合
    nSum: Integer;
    arrAdapaters: array[0..9] of rAdapaterProperty;
  end;


  rNetProperty = packed record    //网络信息
    nIndex: Integer;                   // 索引
    strName: string;                   // Name
    strMAC: string;                    // MAC
    strType: string;                   // Type
    isSecure: Boolean;                 // Secure
    eAuthMode: enumAuthMode;           // AuthMode    {open,shared,WPA,WPAPSK,WPA2,WPA2PSK}
    eCipherMode: enumCipherMode;       // CipherMode  {none,WEP,TKIP,AES}
    nChannel: Integer;                 // Channel
    nSignalQuality: Integer;           // 信号强度 xx%
    nRSSI: Integer;                    // RSSI    dB
  end;

  rArrAvailableNetRecord = packed record   //可用网络集合
    nSum: Integer;
    arrNets: array[0..55] of rNetProperty;
  end;

  rProfile = packed record    //Profile配置文件
    nIndex: Integer;                          //索引
    strName: string;                          //配置文件名
    strNetName: string;                       //对应网络名
    isBroadcast: Boolean;                     //是否广播
    eConnectionType: enumConnectionType;      //连接方式   ad-hoc: 'IBSS' | else: 'ESS'
    isAutoConnect: Boolean;                   //自动连接     '.ConnectionMode'
    eAuthentication: enumAuthMode;            //鉴权方式   {open,shared,WPA,WPAPSK,WPA2,WPA2PSK}
    eEncryption: enumCipherMode;              //加密方式   {none,WEP,TKIP,AES}
    isKeyProtected: Boolean;                  //是否密码保护
    strKey: string;                           //密码
    eKeyType: enumKeyType;                    //KeyType     WEP(none,WEP): 'networkKey' | WPA etc(TKIP,AES): 'passPhrase'
    nKeyIndex: Integer;                       //密码索引
    isEnable802: Boolean;                     //是否开启802.1X
    isUseWinCredentials: Boolean;             //是否使用windows证书
//    eEapType: enumEapType;                    //Eap类型
//    strEapLogin: string;                      //Eap帐号
//    strEapPwd: string;                        //Eap密码
//    strEapCertFile: string;                   //Eap授权文件
  end;

  rArrProfRecord = packed record
    nSum: Integer;
    arrProf: array[0..55] of rProfile;
  end;
  
implementation

end.

  控制线程

unit ComThread;

interface


uses
  WiFiDefine, WifiController, Messages, Windows, Classes, SysUtils, Graphics;

type
  TComThread = class(TThread)
  private
    FH_Main: HWND;



  protected
    procedure Execute; override;
  public

    WiFiCtrler: TWiFiController;

    constructor Create(main: HWND); overload;
    destructor Destroy; override;

  end;    

implementation

uses
  Main, Key, frameData;

constructor TComThread.Create(main: HWND);
begin
  FH_Main := main;
  inherited Create(False);
end;



destructor TComThread.Destroy;
begin

  Self.WiFiCtrler.Disconnect(Self.WiFiCtrler.adpUsedIndex);

  inherited;
end;

procedure TComThread.Execute;
var
  msg: TMsg;
  i, nAdper, nSelected: Integer;
begin

  // Create WiFiController 
  Self.WiFiCtrler := TWiFiController.Create(Self.FH_Main);

  if Self.WiFiCtrler.RefreshAdapaters then
  begin

    //frameData.nLastRvd := Self.WiFiCtrler

    if Self.WiFiCtrler.RefreshNets(0, 1) {and Self.WiFiCtrler.RefreshProf(0)} then
    begin
      Self.WiFiCtrler.RefreshProf(0);
      SendMessage( frmMain.frdt.Handle, WIFI_MSG, MSG_CREATE_SUCCESS, Integer(@Self.WiFiCtrler) );
    end;
  end;

  if Self.WiFiCtrler.strNetUsed <> '' then
  begin
    SendMessage(FH_Main, WIFI_MSG, MSG_HAS_CON, 0);
  end;

  while True do
  begin
    if PeekMessage(msg,0,0,0,PM_REMOVE) then
    begin
      case msg.message of


        WIFI_CREATE:
        begin
          //Self.WiFiCtrler := TWiFiController.Create(Self.ThreadID);
          SendMessage( FH_Main, WIFI_MSG, MSG_CREATE_SUCCESS, Integer(@Self.WiFiCtrler) );
        end;

        WIFI_REFRESH:
        begin
          nAdper := msg.wParam;
          if Self.WiFiCtrler.RefreshNets(nAdper, 1) then
          begin
            Self.WiFiCtrler.RefreshProf(nAdper);
            SendMessage( FH_Main, WIFI_MSG, MSG_REFLESH_SUCCESS, Integer(@Self.WiFiCtrler) );
          end;

        end;

        WIFI_CONNECT:
        begin

          nAdper := frmMain.frdt.cbbAdapters.ItemIndex;
          nSelected := msg.wParam;

          Self.WiFiCtrler.WaitForAdapter;
          Self.WiFiCtrler.Disconnect(nAdper);
          Self.WiFiCtrler.WaitForAdapter;

          //流量初始化
          SendMessage(FH_Main, WIFI_MSG, MSG_INIT_TRAFFIC, Integer(@Self.WiFiCtrler));


          case msg.lParam of


            0:  //存在prof,无修改密码(或无密码需要)
            begin
              Self.WiFiCtrler.ConnectByName(nAdper, Self.WiFiCtrler.rAvaiNets.arrNets[nSelected].strName);
            end;


            1:  // 存在prof,修改了密码
            begin

              for i := 0 to Self.WiFiCtrler.rProfiles.nSum - 1 do
              begin
                if Self.WiFiCtrler.rProfiles.arrProf[i].strNetName = Self.WiFiCtrler.rAvaiNets.arrNets[nSelected].strName then
                begin
                  Self.WiFiCtrler.DelProfile(nAdper, i);
                  Break;
                end;
              end;

              Self.WiFiCtrler.WaitForAdapter;

              if Self.WiFiCtrler.CreateProfileFromNet(nAdper,
                                                  nSelected,   //Net Index
                                                  frmKey.edtKey.Text,       //key
                                                  False,    //auto
                                                  True,     //broadcast
                                                  False,    //computer to computer
                                                  False,    //Use Win Credentials (802x enable)
                                                  0)        //keyindex
              then
              begin
                //Self.WiFiCtrler.RefreshProf(nAdper);
                Self.WiFiCtrler.WaitForAdapter;
                Self.WiFiCtrler.ConnectByName(nAdper, Self.WiFiCtrler.rAvaiNets.arrNets[nSelected].strName);

              end;  


            end;


            2:  // 无prof,不需要密码
            begin

              if Self.WiFiCtrler.CreateProfileFromNet(nAdper,
                                                  nSelected,   //Net Index
                                                  '',       //key
                                                  False,    //auto
                                                  True,     //broadcast
                                                  False,    //computer to computer
                                                  False,    //Use Win Credentials (802x enable)
                                                  0)        //keyindex
              then
              begin
                //Self.WiFiCtrler.RefreshProf(nAdper);
                Self.WiFiCtrler.WaitForAdapter;
                Self.WiFiCtrler.ConnectByName(nAdper, Self.WiFiCtrler.rAvaiNets.arrNets[nSelected].strName);
              end;  



            end;  


            3:  // 无prof,需要密码
            begin

              if Self.WiFiCtrler.CreateProfileFromNet(nAdper,
                                                  nSelected,   //Net Index
                                                  frmKey.edtKey.Text,       //key
                                                  False,    //auto
                                                  True,     //broadcast
                                                  False,    //computer to computer
                                                  False,    //Use Win Credentials (802x enable)
                                                  0)        //keyindex
              then
              begin
                //Self.WiFiCtrler.RefreshProf(nAdper);
                Self.WiFiCtrler.WaitForAdapter;
                Self.WiFiCtrler.ConnectByName(nAdper, Self.WiFiCtrler.rAvaiNets.arrNets[nSelected].strName);
              end;  


            end;  

          end;

          Self.WiFiCtrler.WaitForAdapter;
          //Self.WiFiCtrler.RefreshProf(nAdper);

        end;

        WIFI_DISCONNECT:
        begin

          nAdper := msg.wParam;
          Self.WiFiCtrler.WaitForAdapter;
          if Self.WiFiCtrler.Disconnect(nAdper) then
          begin
            Self.WiFiCtrler.WaitForAdapter;
            Self.WiFiCtrler.RefreshAdapaters;
            Self.WiFiCtrler.RefreshProf(nAdper);
            SendMessage(FH_Main, WIFI_MSG, MSG_DISCON_SUC, 0);
          end;  

        end;  



      end;

    end;  
  end;
end;  


end.

 

  本人水平有限,写的代码可能会让高手们见笑,希望各位在此不吝赐教。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值