Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get local host info?

Status
Not open for further replies.

wkp63

MIS
Jul 16, 2009
3
0
0
MY
Hi! How to read the NIC adapter info like the MAC address using VFP command?

 
This is a bit undocumented, but I have used it to 'lock' software to particular adapters in the past... not recently though!

If you look here:


You will get more information.

Code:
PROCEDURE GETNICINFO
	#DEFINE ERROR_SUCCESS           0
	#DEFINE ERROR_NOT_SUPPORTED     50
	#DEFINE ERROR_INVALID_PARAMETER 87
	#DEFINE ERROR_BUFFER_OVERFLOW   111
	#DEFINE ERROR_NO_DATA           232

	DECLARE INTEGER GetAdaptersInfo IN iphlpapi;
		STRING @pAdapterInfo, LONG @pOutBufLen

	LOCAL LCBUFFER, LNBUFSIZE
	LNBUFSIZE = 0
	LCBUFFER = ""

	* this call usually returns the ERROR_BUFFER_OVERFLOW
	* with lnBufsize set to the required amount of memory
	= GETADAPTERSINFO(@LCBUFFER, @LNBUFSIZE)

	LCBUFFER = REPLI(CHR(0), LNBUFSIZE)
	IF GETADAPTERSINFO(@LCBUFFER, @LNBUFSIZE) <> ERROR_SUCCESS
		* still something is wrong
		RETURN ""
	ENDIF

	RETURN STRTRAN(SUBSTR(LCBUFFER, 433,15), CHR(0),"")

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Win32 API functions are case sensitive, so either you DECLARE GetAdaptersInfo ... AS GETADAPTERSINFO or you use it as it is.

On my Laptop this just shows 0.0.0.0, while this WMI script works. It lists all NICs and IP-Adresses.

see for a list of informations within a Win32_NetworkAdapterConfiguration.

Bye, Olaf.

Code:
Local loWMIService, lcQuery, loNetworkAdapterConfigurations, loNIC, lcIPAddress
lcQuery = "Select * from Win32_NetworkAdapterConfiguration"
loWMIService = Getobject('winmgmts:\\.\root\cimv2')
loNetworkAdapterConfigurations = loWMIService.ExecQuery(lcQuery)
For each loNIC in loNetworkAdapterConfigurations
   ? loNIC.Caption
   If loNIC.IPEnabled
      For each lcIPAddress in loNIC.IPAddress
          ? lcIPAddress
      EndFor 
   EndIf
   If !IsNull(loNic.MacAddress)
      ? "MAC Address:",loNic.MacAddress
   Endif
EndFor

Bye, Olaf.
 
That's interesting Olaf, and you are right!



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Olaf,
API functions are Case sensitive ONLY when you DECLARE them.
If you successfully declare a function then you could use whatever case you want.
You CAN'T use:
Code:
DECLARE ... GETADAPTERSINFO ...

But CAN use:
Code:
DECLARE ... GetAdaptersInfo ...


GETADAPTERSINFO(...)
GetAdaptersInfo()
GETAdaptersInfo()
....


Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
I meant he was right, it always returns 0.0.0.0



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
thanks for the great help here....

my purpose to get the NIC info because i need to use the MAC address/IP to block some of my workstation from accessing internet.
 
Thanks!

You're right Borislav, usage of DLL functions is not case sensitive, only declaration. There you see - after 10 years with foxpro still having some false assumptions. You never know all...

Bye, Olaf.
 
I *think* the reason it returns 0.0.0.0 is because there are more than one network interfaces (bluetooth, WiFi, multihoming etc.) and the call to GetAdaptersInfo() returns ALL of them (the clue is in the name!)

So, you would need tor ead the article on the MSDN page a bit more to get the right info.

B-)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
That's true, Griff.

But even if I change your routine to return the whole buffer (raw) with RETURN CHRTRAN(LCBUFFER, CHR(0),"") I don't get all the network adapters WMI shows. Maybe enough info, but then I'd prefer what Win32_NetworkAdapterConfiguration objects offer as informations.

Bye, Olaf.
 
I think you are right Olaf!

B-)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Another way to do this in Win2k and above is to use the GetIfTable() function in the iphlpapi.dll. Using it is much more complex than the other 2 suggestions in this thread, but the good news is that someone has already done the heavy lifting for you...


Make sure when cutting and pasting the example from the link above that you don't choose to translate the page (doubles up the code sometimes when pasting into a VFP prg).

boyd.gif

SweetPotato Software Website
My Blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top