tags:

views:

174

answers:

2

Can someone convert this to Delphi 2010? After not being able to find delphi code to return WiFi Signal Strength I found this basic code. Can someone convert this to Delphi?

'**************************************
' Name: WiFi Signal Strength
' Description:Returns the Wifi signal st
'     rength in bars (1 to 5, 5 being good)
' By: Techni Rei Myoko
'
'This code is copyrighted and has' limited warranties.Please see http://w
'     ww.Planet-Source-Code.com/vb/scripts/Sho
'     wCode.asp?txtCodeId=71872&lngWId=1'for details.'**************************************

Option Explicit
Public WiFiHardwareName As String, WiFiDecibals As Long
Dim objWMIService As Object, isSet As Boolean   
Public Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long

Public Enum ConnectedState
    INTERNET_CONNECTION_MODEM = &H1
    INTERNET_CONNECTION_LAN = &H2
    INTERNET_CONNECTION_PROXY = &H4
    INTERNET_CONNECTION_MODEM_BUSY = &H8
    INTERNET_RAS_INSTALLED = &H10
    INTERNET_CONNECTION_OFFLINE = &H20
    INTERNET_CONNECTION_CONFIGURED = &H40
End Enum

Public Function WifiSignalStrength(Optional Computer As String = ".") As Long   
    On Error Resume Next
    Dim colItems As Object, objItem As Object
    WifiSignalStrength = IIf(isConnected, 5, 0)

    If Not isSet Then
        Set objWMIService = GetObject("winmgmts:\\" & Computer & "\root\wmi")
        isSet = True
    End If

    Set colItems = objWMIService.ExecQuery("Select * From MSNdis_80211_ReceivedSignalStrength")

    For Each objItem In colItems
        WiFiDecibals = objItem.NDIS80211ReceivedSignalStrength
        WiFiHardwareName = objItem.InstanceName

        Select Case WiFiDecibals
            Case 0: WiFiHardwareName = "Ethernet"
            Case Is > -57: WifiSignalStrength = 5 ' -56 To 0
            Case Is > -68: WifiSignalStrength = 4 '-67 To -57
            Case Is > -72: WifiSignalStrength = 3 '-71 To -68
            Case Is > -80: WifiSignalStrength = 2 '-79 To -72
            Case Is > -90: WifiSignalStrength = 1 '-89 To -80
            Case Else: WifiSignalStrength = 0
        End Select   
Next

End Function

'Connection

Public Function isConnected() As Boolean

    Dim dwFlags As Long, retval As Long
    retval = InternetGetConnectedState(dwFlags, 0&)
    isConnected = retval = 1
End Function
+3  A: 

Don't look for wifi, do it for WMi.

With WMI you can get information about a huge number of elements of the computer, including the wifi.

This component can help you and it works with Delphi 2010.

http://www.magsys.co.uk/delphi/magwmi.asp

Francis Lee
This pointer looks great, I've downloaded it and played. It is possible to use this method to enumerate and then find the allocated I/O address of a parallel port? Brian.
Brian Frost
+6  A: 

Go to Angus Johnson's website and download MagWMI (freeware). It contains Delphi functions for querying WMI from Delphi directly, and the demo app should allow you to both test the SELECT on your machine and view the results, and show you how to do so programmatically from your own code. (The version I have had to have two procedure declarations changed in MagWMI.pas to change two WideString parameters to simple strings in order to compile under D2010.)

Alternately, you can import the Windows WMI Scripting Library (Component|Import Component|Import Type Library, and search for "WBEM"), and use the classes Delphi creates for you during the import process.

Finally, you can use the ComObj unit and use the CreateOleObject() function to return an OleVariant, use it to execute the query, and deal with the mess of working with OleVariants and SafeArrays to iterate through. I started writing this for you, and decided it was not nice. <g>

The MagWMI unit changes all of the code you posted to a few statements, similar to this (taken pretty much from the MagWMI demo, and I can't use your exact Select, because there's no wireless on this desktop):

var
  Select: string;
  WMIResults := T2DimStrArray;   // MagWMI defined two dimensional string array
  Instances, Rows, i, j: Integer;
begin
  Select := 'SELECT * FROM Win32_OperatingSystem"';
  Rows := MagWMIGetInfo('', 'root\CIMV2', '', '', Select, WMIResults, Instances);
  if Rows > 0 then
  begin
    if Instances >= ListView1.Columns.Count then
      Instances := ListView1.Columns.Count - 1;
    for J := 0 to Instances do
      ListView1.Columns.Items[j].Caption := WMIResults[j, 0];
    for i := 1 to Rows do
    begin
      with ListView1.Items.Add do
      begin
        Caption := WMIResults[0, i];
        for j := 0 to Instances do
          SubItems.Add(WMIResults[j, i]);
      end;
    end;
  end;
end;

The code above produces output similar (with a lot more rows) to this:

Instance              1
========              ====================
BootDevice            \Device\HarddiskVolume1
BuildNumber           2600
BuildType             Multiprocessor Free
Caption               Microsoft Windows XP Professional
CodeSet               1252
// Snip about 50 more lines
Ken White