PB中获取机器IP地址的方法

在使用PowerBuilder开发信息管理系统时,为了安全性,我们需要记录每个登录用户的信息,并生成日志。最关键的一点是知道哪个机器在运行程序。因此,只要获取到运行程序的机器IP地址,这个问题就解决了。虽然PowerBuilder中没有像Delphi那样现成的函数可以方便地获取机器的IP地址,但我们可以利用外部函数来实现。首先声明外部函数:

function int WSAStartup( uint UIVersionRequested, ref s_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"

然后在Form的open事件中执行以下代码:

s_wsadata l_WSAData
int li_version = 257
blob{4} lb_hostaddress
IF wsastartup(li_version, l_WSAData) = 0 THEN
    IF gethostname(ls_HostName, len(ls_HostName)) < 0 xss=removed xss=removed xss=removed xss=removed>

通过以上方法,可以获取到运行程序的机器IP地址。

pbl 文件大小:839.5KB