Join The Works program to have access to the most current content, and to be able to ask questions and get answers from Revelation staff and the Revelation community

At 11 NOV 2009 03:41:01PM Dale Jessop wrote:

Hi all,

I have some code which used to get the DNS name from an ip address, which I've modified so it will also get the DNS name of the local machine in which OI is running. However, whilst I have the HOSENT structure (from which I get the fully DNS name) I want to get the actual IP Address as well but that requires me to call inet_ntoa passing in a structure called in_addr.

Can anyone decipher this…

typedef struct in_addr {

union {
  struct {
    u_char s_b1,s_b2,s_b3,s_b4;
  } S_un_b;
  struct {
    u_short s_w1,s_w2;
  } S_un_w;
  u_long S_addr;
} S_un;

}IN_ADDR, *PIN_ADDR, FAR *LPIN_ADDR;


At 21 AUG 2012 11:12AM Jared Bratu wrote:

There are several different methods to get the address of the local workstation. I found some online examples that read the registry to retrieve the IP address. It has been adapted to check for multiple network interfaces and also differentiate between static and DHCP addresses. I hope this points you in the right direction. It hasn't been extensively tested.

0001 Function GetStationIPAddresses(void)

0002 Declare Function OleCreateInstance, OleCallMethod

0003

0004 MAX_NIC_CARDS = 50 ;*Maximum number of network interfaces to check

0005 AvailableNics = "" ;*Temporary GUID storage for available network interfaces

0006 IPAddressesOnSystem = "" ;*Available static and dhcp IP addresses from available network interfaces

0007

0008 oSh = OleCreateInstance("WScript.Shell")

0009

0010 *Loop through looking for network cards

0011 For i = 1 To MAX_NIC_CARDS

0012

0013 tmpRegBranch = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" : i : "\ServiceName"

0014 retVal = OleCallMethod(oSh, "RegRead", tmpRegBranch)

0015

0016 *If not empty then add GUID to check for IP address

0017 If retVal <> "" Then

0018 AvailableNics←1> = retVal

0019 End

0020

0021 Next

0022

0023 AvailableNicsCount = DCOUNT(AvailableNics, @FM)

0024

0025 *Loop through all the nic GUID's to check for DHCP and static IP addresses

0026 For i = 1 To AvailableNicsCount

0027

0028 *Check for static address

0029 tmpRegBranch="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" : AvailableNics<i> : "\IPAddress"

0030 retVal = OleCallMethod(oSh, "RegRead", tmpRegBranch)

0031 If retVal <> "" Then

0032 IPAddressesOnSystem←1> = retVal

0033 End

0034

0035 *Check for DHCP address

0036 tmpRegBranch="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" : AvailableNics<i> : "\DhcpIPAddress"

0037 retVal = OleCallMethod(oSh, "RegRead", tmpRegBranch)

0038 If retVal <> "" Then

0039 IPAddressesOnSystem←1> = retVal

0040 End

0041

0042 Next

0043

0044 Debug

0045 *IPAddressesOnSystem now contains all IP addresses on this workstation


At 12 NOV 2009 05:15AM Dale Jessop wrote:

Hey Jared Thanks!!!

you put me on the right direction. I've imprvoed the code a little so it pulls out all of the IP addresses, including the IP address assigned wireless adapters.

Cheers Dale.

Function GetStationIPAddresses(void)

  Declare Function OleCreateInstance, OleCallMethod
  Declare function RegOpenKeyEx, RegQueryValueEx, RegCloseKey
AvailableNics=" 
/*  equates for the base registry keys */
equ HKEY_CLASSES_ROOT$     to 0x80000000
equ HKEY_CURRENT_USER$     to 0x80000001
equ HKEY_LOCAL_MACHINE$    to 0x80000002
equ HKEY_USERS$            to 0x80000003
equ HKEY_PERFORMANCE_DATA$ to 0x80000004
equ HKEY_CURRENT_CONFIG$   to 0x80000005
equ HKEY_DYN_DATA$         to 0x80000006
equ KEY_QUERY_VALUE$       to 0x0001
equ ERROR_SUCCESS          to 0x0000 
options=0
samDesired=KEY_QUERY_VALUE$
KeyHandle=0
Hkey=HKEY_LOCAL_MACHINE$
SubKey=SYSTEM\CurrentControlSet\Services\TCPIP\Linkage":\00\
stat=0
null='
LockVariable KeyHandle as Long
stat=RegOpenKeyEx(Hkey, SubKey, options, samDesired, KeyHandle)
If Stat=ERROR_SUCCESS Then
  QueueName=str(\00\, 512)
  Reg_SZ=1
  cbBuf=512
  Key=Bind":\00\
  x=RegQueryValueEx(KeyHandle, Key, 0, Reg_SZ, QueueName, cbBuf)
  QueueName=trim(QueueName)
  swap "\Device\" with @fm in QueueName
  swap " " with "" in QueueName
  swap \00\ with "" in QueueName
  noGuids=count(QueueName,@fm)+(QueueName ne "")
  for lop=noGuids to 1 step -1
  	if (QueueName1,1={" AND QueueName-1,1=}") else
  	   QueueName=delete(QueueName,lop,0,0)
  	end
  next lop
  AvailableNics=QueueName
end
rv=RegCloseKey( KeyHandle)
MAX_NIC_CARDS=50        ; * Maximum number of network interfaces to check
IPAddressesOnSystem="  ; * Available static and dhcp IP addresses from available network interfaces
oSh=OleCreateInstance("WScript.Shell")
  • Loop through looking for network cards
For i=1 To MAX_NIC_CARDS 
	tmpRegBranch=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\" : i : "\ServiceName"
	retVal=OleCallMethod(oSh, "RegRead", tmpRegBranch)    
  • If not empty then add GUID to check for IP address
	If retVal  "" Then
		locate retval in AvailableNics using @fm setting pos else
			AvailableNics=retVal
		end
	End
Next
AvailableNicsCount=DCOUNT(AvailableNics, @FM)
  • Loop through all the nic GUID's to check for DHCP and static IP addresses
For i=1 To AvailableNicsCount
  • Check for static address
	tmpRegBranch=HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" : AvailableNics[i] : "\IPAddress"
	ipAddr=OleCallMethod(oSh, "RegRead", tmpRegBranch)
	If ipAddr  "" Then
		gosub addIPAddr
	End
  • Check for DHCP address
	tmpRegBranch=HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\" : AvailableNics[i] : "\DhcpIPAddress"
	ipAddr=OleCallMethod(oSh, "RegRead", tmpRegBranch)
	If ipAddr  "" Then
		gosub addIPAddr
	End      
Next
return

addIpAddr:

locate ipAddr in IPAddressesOnSystem using @fm setting pos else
  IPAddressesOnSystem=ipAddr
end

return


At 12 NOV 2009 08:58AM Bob Carten wrote:

This is a good job for WMI ( Windows management Interface )

WMI often has a way to answer questions about properties of your pc or domain, such as this one, or 'what groups is this user a member of?'. WMI is Microsoft's attempt to hide the complexity of the api calls and registry hives. WMI is difficult to learn, but easy to copy I use it because it is easy to find examples, Most examples do not translate well to Basic+. A workaround is to convert the script to a VbScript function, embed it / eval it from Basic+. The example below shows how to do this. It is a good technique to have in your back pocket.

0001  Function Get_IpAddresses(void)
0002  /*
0003  **  WMI Scripting Example
0004  **  lifted from http://msdn.microsoft.com/en-us/library/aa394595%28VS.85%29.aspx
0005  **
0006  ** Note: WMI collections / Arrays can be difficult to translate to Basic+,
0007  ** So used embedded vbscript instead
0008  **
0009  ** rjc  12Nov2009
0010  */
0011  
0012  Declare Function rti_Ole_GetObject
0013  Equ crlf$ To \0D0A\
0014  
0015  script = 'function GetAddresses()'
0016  script&lt;-1&gt; = 'set oList=CreateObject("Scripting.Dictionary")'
0017  script&lt;-1&gt; = 'strComputer=."'
0018  script&lt;-1&gt; = 'Set objWMIService=GetObject( _ '
0019  script&lt;-1&gt; = '    "winmgmts:\\" &amp; strComputer &amp; "\root\cimv2")'
0020  script&lt;-1&gt; = 'Set IPConfigSet=objWMIService.ExecQuery _'
0021  script&lt;-1&gt; = '    ("Select IPAddress from Win32_NetworkAdapterConfiguration ")'
0022  script&lt;-1&gt; = ''
0023  script&lt;-1&gt; = 'For Each IPConfig in IPConfigSet'
0024  script&lt;-1&gt; = '    If Not IsNull(IPConfig.IPAddress) Then '
0025  script&lt;-1&gt; = '        For i=LBound(IPConfig.IPAddress) _'
0026  script&lt;-1&gt; = '            to UBound(IPConfig.IPAddress)'
0027  script&lt;-1&gt; = '                oList.Add IPConfig.IPAddress(i),  "" '
0028  script&lt;-1&gt; = '        Next'
0029  script&lt;-1&gt; = '    End If'
0030  script&lt;-1&gt; = 'Next'
0031  script&lt;-1&gt; = 'addresses=oList.Keys()'
0032  script&lt;-1&gt; = 'GetAddresses=join(addresses, ",")'
0033  script&lt;-1&gt; = 'End Function'
0034  
0035  Swap @fm With crlf$ In script
0036  
0037  wsh = OLECreateInstance("MSScriptControl.ScriptControl")
0038  wsh-&gt;Language = "vbScript"
0039  x = wsh-&gt;AddCode(script)
0040  
0041  expression = 'GetAddresses()'
0042  ans = wsh-&gt;Eval(expression)
0043  Convert ',' To @vm In ans
0044  Return ans
0045  

At 12 NOV 2009 10:49AM Dale Jessop wrote:

Hi Bob,

Thanks! that is a neat trick, will try and remember that!

Dale.


At 12 NOV 2009 01:29PM John Bouley wrote:

Bob,

Thanks for the trick. I wonder how long it will take Microsoft to plug that hole? :-)

John


At 12 NOV 2009 05:22PM Bob Carten wrote:

It's not a hole, it's intended behavior for the scripting control.

The scripting host is flexible and really powerful. With power comes responsibility The problems with the scripting host arise when developers expose the host to unsafe scripts, as Microsoft did with office, the browser and the Windows shell.

View this thread on the Works forum...

  • third_party_content/community/commentary/forums_works/c7ff63c7eb84fbf98525766b00719e95.txt
  • Last modified: 2024/01/04 20:57
  • by 127.0.0.1