?
Solved

Getting Subnet mask

Posted on 2003-04-01
4
Medium Priority
?
1,416 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 22 hours left to enroll

762 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