tags:

views:

246

answers:

3

can someone explain this to me like I am a two year old, no its not a fetish just I cant seem to figure out which is router, number 3? what does 4 mean?

uses
  WinInet;

const
  MODEM = 1;
  LAN = 2;
  PROXY = 4;
  BUSY = 8;

function GetConnectionKind(var strKind: string): Boolean;
var
  flags: DWORD;
begin
  strKind := '';
  Result := InternetGetConnectedState(@flags, 0);
  if Result then
  begin
    if (flags and MODEM) = MODEM then strKind := 'Modem';
    if (flags and LAN) = LAN then strKind := 'LAN';
    if (flags and PROXY) = PROXY then strKind := 'Proxy';
    if (flags and BUSY) = BUSY then strKind := 'Modem Busy';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  strKind: string;
begin
  if GetConnectionKind(strKind) then
    ShowMessage(strKind);
end;
+6  A: 

InternetGetConnectedState returns a bitmask in the first parameter that looks like this:

76543210  <-- bit numbers
 || ||||
 || |||+- INTERNET_CONNECTION_MODEM
 || ||+-- INTERNET_CONNECTION_LAN
 || |+--- INTERNET_CONNECTION_PROXY
 || +---- INTERNET_CONNECTION_MODEM_BUSY (No longer used)
 |+------ INTERNET_CONNECTION_OFFLINE
 +------- INTERNET_CONNECTION_CONFIGURED

If a given bit is set, the connection is of that type. So if bit nr. 2 is set, you're connected through a proxy.

Additionally, the function returns a TRUE/FALSE value, indicating whether you are connected to the internet.

The values you have in your code, 1, 2, 4, 8, corresponds to the decimal value of those bits, counting from the right.

Basically the code inspects each bit in turn, and sets the strKind variable to a text indicating the nature of the connection.

You're asking "which is router? 3?", and I assume you mean by that "how do I figure out that my connection is through a router?". I would assume this would be the same as the LAN connection, presumably the LAN has a bridge somewhere to access the internet through.

Lasse V. Karlsen
A: 

The constant values are flags which means two things: (1) you cannot have a "3" value and (2) you can have more than one value in the "flags" result. For example, for result 9 (1001 in binary) the first and last checks would be true.

For more info on the result meaning check the MSDN reference for InternetGetConnectedState.

Tihauan
+6  A: 

The codes 1, 2, 4, 8 represent bitmasks. I generally prefer to always use bitmasks in hex to avoid any confusion, its fairly easy to remember since the pattern continues in nibbles (the set of 4 binary bits).

HEX       BINARY       DEC
$01       00000001       1
$02       00000010       2
$04       00000100       4
$08       00001000       8
$10       00010000      16
$20       00100000      32
$40       01000000      64
$80       10000000     128

If you ever want to check two values at once, you can OR them together, for example $01 or $02 = $03 (binary 00000011). So a 3 would be BOTH a modem AND a lan.

A common practice to see if something is set or not, would be to AND it with the mask. for example, if my number is 3, and i "and" this with $02, then the result is $02 since the bit for both the mask AND the value were both set. If my number is 4 and I "and" this with $02, then the result is $00 since the bit for both the mask and the value were not set.

Of course this doesn't answer what I think your real question is. The router would be impossible to determine just by checking only this mask. This mask just tells you if your connected via a modem (aka Dialup) or a network adapter. The router would be beyond the network adapter, and would require further analysis of the network to accurately determine.

skamradt