Skip to main content

Problem:

How to get a machine's MAC address and IP address in a .NET managed COBOL Application?

Resolution:

The MAC address of a network card is a unique number that is burned into each card produced. It will always be unique and will not be duplicated on any other network card.

V2 of the .NET Framework introduced some new classes in the System.Net.NetworkInformation namespace that allows  to get details information on the machines network configuration.

An example how to use these classes is attached.

The code to get the IP and MAC address is:

           set IPProps to cIPGlobalProperties::"GetIPGlobalProperties"()

           

           set label1::"Text" to string::"Concat"("Machine Name = ",IPProps::"HostName","      Domain = ",IPProps::"DomainName")

           

           set netcards to cNetworkInterface::"GetAllNetworkInterfaces"()      

           if (netcards = null or netcards::"Length" < 1)

               invoke cMessageBox::"Show"("No Network Cards Detected.")

               exit method

           end-if

           

       *> Iterate through all the Netcards on the Machine

       

           perform varying netcard through netcards

               *>set netcard::"NetworkInterfaceType"

               

               set ipprop to netcard::"GetIPProperties"()  

               set ipInfo to ipprop::"UnicastAddresses"::"Item"(0) as cIPAddressInformation

               

               set netinfo to ipInfo::"Address"  *> IP Address

               set netinfo to string::"Concat"(netinfo,"   MAC = ",netcard::"GetPhysicalAddress"())   *> MAC    

               

               set netinfo to string::"Concat"(netinfo, " Desc = ", netcard::"Description")  *> Description of CARD

               

               set netinfo to string::"Concat"(netinfo, " Type = ", netcard::"NetworkInterfaceType")  *> Tyoe

               

               invoke listbox1::"Items"::"Add"(netinfo)    *> Add into the Listbox

               

           end-perform

The full program and project is attached.

Attachments:

IPAddress.zip

Old KB# 4400