Get the IP addressTag(s): WinAPI/Registry


PB 9 or less
[Structure]
str_wsadata
   unsignedinteger  version
   unsignedinteger  highversion
   character  description[257]
   character  systemstatus[129]
   nsignedinteger  maxsockets
   unsignedinteger  maxupddg
   string  vendorinfo

[External function]

function int WSAStartup (uint UIVerionrequested, ref str_wsadata lpWSAdata)
library "wsock32.DLL"
function int WSACleanup() library "wsock32.DLL"
function int WSAGetLastError() library "wsock32.DLL"
function int gethostname(ref string name, int namelen) library 
"wsock32.DLL"
function string GetHost(string lpszhost,ref blob lpszaddress) library
"pbws32.dll"

[Powerscript]

String ls_ip, ls_host
Blob{4} lb_host
Integer li_version, li_rc
str_wsadata lstr_wsadata

ls_host = Space(128)
li_version = 257

If WSAStartup(li_version, lstr_wsadata) = 0 Then
   If GetHostName(ls_host, Len(ls_host)) < 0 Then
      li_rc = WSAGetLastError()
   Else
      GetHost(ls_host, lb_host)
      ls_ip = String(Asc(String(BlobMid(lb_host, 1, 1)))) + "."
      ls_ip += String(Asc(String(BlobMid(lb_host, 2, 1)))) + "."
      ls_ip += String(Asc(String(BlobMid(lb_host, 3, 1)))) + "."
      ls_ip += String(Asc(String(BlobMid(lb_host, 4, 1))))
      li_rc = 0
   End If
   MessageBox("My IP", ls_ip)
Else
   li_rc = WSAGetLastError()
End If

WSACleanup()
NOTE : The pbws32.dll can be downloaded here. This dll was originally distributed with the FTP example in the PB5 Application Gallery (also included in the ZIP).

PB10 or more.

Small fixes are required since Powerbuilder is now unicode-based and the API used are ANSI-based.

[Structure]

... same ...

[External function]
function int WSAStartup (uint UIVerionrequested, ref str_wsadata lpWSAdata) &
     library "wsock32.DLL" alias for "WSAStartup;ansi"
function int WSACleanup() library "wsock32.DLL"
function int WSAGetLastError() library "wsock32.DLL"
function int gethostname(ref string name, int namelen) &
     library "wsock32.DLL"  alias for "gethostname;Ansi"
function string GetHost(string lpszhost,ref blob lpszaddress) &
     library "pbws32.dll" alias for "GetHost;Ansi"

[Powerscript]

...
      ls_ip = String(Asc(String(BlobMid(lb_host, 1, 1),Encodingansi!))) + "."
      ls_ip += String(Asc(String(BlobMid(lb_host, 2, 1),Encodingansi!))) + "."
      ls_ip += String(Asc(String(BlobMid(lb_host, 3, 1),Encodingansi!))) + "."
      ls_ip += String(Asc(String(BlobMid(lb_host, 4, 1),Encodingansi!)))
...



If you have a valid Visual Studio installation, you have a special ActiveX that can be used. Drop on a window the OLE control called "Microsoft Winsock Control (MSWINSCK.OCX)" and then from Powerscript :

String ls_ip 
 
ls_ip = ole_1.object.LocalIP
MESSAGEBOX("IP", LS_IP)

To get the IP address from a PB component hosted by EAServer, see this HowTo



blog comments powered by Disqus