Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Getting Subnet mask

Posted on 2003-04-01
4
Medium Priority
?
1,455 Views
Last Modified: 2012-05-07
How can I get the subnet mask address using Delphi from the computer where the application runs?

Jeroen
0
Comment
Question by:Jeroen666
4 Comments
 
LVL 9

Expert Comment

by:tkalchev
ID: 8246673
http://www.geocities.com/nickrepin/winprog.html#ipconf

It is in C++, but i am sure you can easily translate it in Deplhi
0
 

Accepted Solution

by:
hakanfa earned 200 total points
ID: 8267369

Well here follus the sourcecode for the unit "InterfaceInfo". Add the unit to your project and
call : EnumInterfaces..

Ex.

procedure TForm1.Button1Click(Sender: TObject);
begin
USock.EnumInterfaces(s);
ShowMessage(s);
end;

Hope it helps,
Hokki

*************************************************

unit InterfaceInfo;

interface

uses Windows, Winsock;

function EnumInterfaces(var sInt: string): Boolean;

function WSAIoctl(s: TSocket; cmd: DWORD; lpInBuffer: PCHAR; dwInBufferLen:
  DWORD;
  lpOutBuffer: PCHAR; dwOutBufferLen: DWORD;
  lpdwOutBytesReturned: LPDWORD;
  lpOverLapped: POINTER;
  lpOverLappedRoutine: POINTER): Integer; stdcall; external 'WS2_32.DLL';



const
  SIO_GET_INTERFACE_LIST = $4004747F;
  IFF_UP = $00000001;
  IFF_BROADCAST = $00000002;
  IFF_LOOPBACK = $00000004;
  IFF_POINTTOPOINT = $00000008;
  IFF_MULTICAST = $00000010;

type
  sockaddr_gen = packed record
    AddressIn: sockaddr_in;
    filler: packed array[0..7] of char;
  end;

type
  INTERFACE_INFO = packed record
    iiFlags: u_long;
    iiAddress: sockaddr_gen;
    iiBroadcastAddress: sockaddr_gen;
    iiNetmask: sockaddr_gen;
  end;

implementation

function EnumInterfaces(var sInt: string): Boolean;
var
  s: TSocket;
  wsaD: WSADATA;
  NumInterfaces: Integer;
  BytesReturned, SetFlags: u_long;
  pAddrInet: SOCKADDR_IN;
  pAddrString: PCHAR;
  PtrA: pointer;
  Buffer: array[0..20] of INTERFACE_INFO;
  i: Integer;
begin
  result := true;
  sInt := '';

  WSAStartup($0101, wsaD);

  s := Socket(AF_INET, SOCK_STREAM, 0);
  if (s = INVALID_SOCKET) then
    exit;

  try
    PtrA := @bytesReturned;
    if (WSAIoCtl(s, SIO_GET_INTERFACE_LIST, nil, 0, @Buffer, 1024, PtrA, nil,
      nil)
      <> SOCKET_ERROR) then
    begin
      NumInterfaces := BytesReturned div SizeOf(INTERFACE_INFO);

      for i := 0 to NumInterfaces - 1 do {Loop trough all interfaces}
      begin
        pAddrInet := Buffer[i].iiAddress.addressIn;
        pAddrString := inet_ntoa(pAddrInet.sin_addr);
        sInt := sInt + ' IP Address: ' + pAddrString + ',';
        pAddrInet := Buffer[i].iiNetMask.addressIn;
        pAddrString := inet_ntoa(pAddrInet.sin_addr);
        sInt := sInt + ' Subnet Mask: ' + pAddrString + ',';
      end;
    end;
  except
  end;
  CloseSocket(s);
  WSACleanUp;
  result := false;
end;
0
 

Author Comment

by:Jeroen666
ID: 8268024
Thanks a lot! I've got the subnetmask now..
0
 

Expert Comment

by:CleanupPing
ID: 9316512
Jeroen666:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question