Author Topic: Get IP address  (Read 3762 times)

0 Members and 1 Guest are viewing this topic.

Andrea

  • Water Moccasin
  • Posts: 2372
Get IP address
« on: July 17, 2012, 01:35:18 PM »
Hi all,..

I know there is some code to find the IP address.
but there is some others ( just sharing).

Code: [Select]


(defun c:getip ( / WMI CSERV EXQ gip)
 (vl-load-com)
  (setq WMI (vlax-create-object "WbemScripting.SWbemLocator"))
  (setq CSERV (VLAX-INVOKE WMI 'ConnectServer "." "\\root\\cimv2" nil nil nil nil nil nil))
  (setq EXQ (vlax-invoke CSERV 'ExecQuery "Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = true")) 
  (vlax-for item EXQ
  (setq gip (vlax-get item 'IPAddress))
  )   
(vlax-release-object wmi)
(vlax-release-object CSERV)
(vlax-release-object EXQ)
gip
)



(defun c:getip2 ( / IP Domain dhcpSubnet TheIP)
 (vl-load-com)
  (foreach guid (vl-registry-descendents (setq loc "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\TCPIP\\Parameters\\Interfaces\\"))
    (if (and
          (setq Domain (vl-registry-read (strcat loc guid) "DhcpDomain"))
          (setq IP (vl-registry-read (strcat loc guid) "DhcpIPAddress"))
          (not (vl-registry-read (strcat loc guid) "DhcpNetworkHint"))
          (setq dhcpSubnet (vl-registry-read (strcat loc guid) "DhcpSubnetMask"))
          (= dhcpSubnet "255.255.255.0")
        )
    (setq TheIP IP)
    )
    )
  theIP
 )
Keep smile...

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: Get IP address
« Reply #1 on: July 17, 2012, 02:31:32 PM »
I wrote a very similar one:
Code - Auto/Visual Lisp: [Select]
  1. (defun GetIP (/ SWbemLocator Service IPSet lst IP Address)
  2.   (setq SWbemLocator (vlax-create-object "WbemScripting.SWbemLocator"))        
  3.   (setq Service (vlax-invoke SWbemLocator 'ConnectServer))
  4.   (setq IPSet (vlax-invoke Service 'InstancesOf "Win32_NetworkAdapterConfiguration"))
  5.   (setq lst nil)
  6.   (vlax-for IP IPSet
  7.     (if (setq Address (vlax-get IP 'IPAddress ))
  8.       (setq lst (cons Address lst))
  9.     )
  10.   )
  11.   (vlax-release-object Service)
  12.   (vlax-release-object SWbemLocator)
  13.  
  14.   (reverse lst)
  15. )
I am a bilingualist,Chinese and Chinglish.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: Get IP address
« Reply #2 on: July 17, 2012, 02:40:44 PM »
If you have registered  the DynamicWrapper ,you can use this:
Code - Auto/Visual Lisp: [Select]
  1. ;;;function :Get the ip address and more details.
  2. (defun C:IP (/ wrap pTable pSize ret i size count IPAddress InterfaceIndex SubnetMask BroadCast ReassemblySize lst)
  3.   (setq wrap (vlax-create-object "DynamicWrapperX"))
  4.   (vlax-invoke wrap 'Register "MSVCRT" "calloc" "i=ll" "r=p")
  5.   (vlax-invoke wrap 'Register "MSVCRT" "free" "i=p")
  6.   (vlax-invoke wrap 'Register "iphlpapi" "GetIpAddrTable" "i=ppl" "r=l")
  7.   (vlax-invoke wrap 'Register "ws2_32" "inet_ntoa" "i=p" "r=s")
  8.  
  9.   ;;memory allocate
  10.   (setq pTable (vlax-invoke wrap 'calloc 1 28))
  11.   (setq pSize (vlax-invoke wrap 'calloc 1 4))
  12.   (vlax-invoke wrap 'numput 0 pSize)
  13.  
  14.   ;;the first test
  15.   (setq ret (vlax-invoke wrap 'GetIpAddrTable pTable pSize 0))
  16.  
  17.   ;;get the buffer
  18.   (setq size (vlax-invoke wrap 'NumGet pSize))
  19.   (vlax-invoke wrap 'free pTable)
  20.   (setq pTable (vlax-invoke wrap 'calloc 1 size))
  21.  
  22.   ;;the second test
  23.   (setq ret (vlax-invoke wrap 'GetIpAddrTable pTable pSize 0))
  24.  
  25.   ;;Get the details
  26.   (setq Count (vlax-invoke wrap 'NumGet pTable))
  27.   (setq i 0)
  28.   (setq lst nil)
  29.   (repeat Count
  30.     (setq IPAddress  (vlax-invoke wrap 'Numget pTable (+ i 4)))
  31.     (setq InterfaceIndex (vlax-invoke wrap 'NumGet pTable (+ i 8)))
  32.     (setq SubnetMask (vlax-invoke wrap 'NumGet pTable (+ i 12)))
  33.     (setq BroadCast (vlax-invoke wrap 'NumGet pTable (+ i 16)))
  34.     (setq ReassemblySize (vlax-invoke wrap 'NumGet pTable (+ i 20)))
  35.     (setq lst (cons (list InterfaceIndex
  36.                           ;(vlax-invoke wrap 'inet_ntoa IPAddress)
  37.                           ;(vlax-invoke wrap 'inet_ntoa SubnetMask)
  38.                           ;(vlax-invoke wrap 'inet_ntoa BroadCast)
  39.                           (inet_ntoa IPAddress)
  40.                           (inet_ntoa SubnetMask)
  41.                           (inet_ntoa BroadCast)
  42.                           ReassemblySize
  43.                     )
  44.                     lst
  45.               )
  46.     )
  47.     (setq i (+ i 24))
  48.   )
  49.  
  50.   ;;release objects  and free memory
  51.   (vlax-invoke wrap 'free pSize)
  52.   (vlax-invoke wrap 'free pTable)
  53.   (reverse lst)
  54. )
  55.  
  56. (defun inet_ntoa (n / x l)
  57.   (setq x n)
  58.   (repeat 4
  59.     (setq l (cons (logand x 255) l))
  60.     (setq x (lsh x -8))
  61.   )
  62.   (reverse l)
  63. )
I am a bilingualist,Chinese and Chinglish.

ahankhah

  • Guest
Re: Get IP address
« Reply #3 on: December 28, 2014, 07:17:51 AM »
highflyingbird,
I registered DynamicWrapper, but had no success getting "DynamicWrapperX" object (mentioned in the 4th line of code):

Quote
(setq wrap (vlax-create-object "DynamicWrapperX"))

and it returns nil.

What is wrong with my effort?