Get current IP Adrress

Cringer

ProgressTalk.com Moderator
Staff member
Hello there - I need to be able to extract the IP Address of the current machine. Is there an API call that will do this sort of thing? I've had a quick look through the standard dlls and I can't find what I'm looking for. I suppose I could run os command for ipconfig and parse the address from that, but it's harder work than I hoped. ANy ideas please? Thanks.

By the way I'm running on windows.
 

shireeshn

Member
hi casper,
iam new learner,i think u can answer my damp question :).
1) i found the code, in the linked brower.
2) i copied the code the editor.
3) but i dont know where the file windows.p will be?
4)any way i ran the code i did not get any result, i did not find the calling procedure for "i-GetTcpInfo".

can u tell me abt windows.p, how it is use full(by name it feels it is an interface to windows i feel), so i want to know in detail, if possible can u help me.
 

Casper

ProgressTalk.com Moderator
Staff member
As you look more closely to the source then you see that i-GetTcpInfo is an internal procedure that you can run in the main block:

just add:

Code:
DEFINE VARIABLE pcName AS CHARACTER   NO-UNDO.
DEFINE VARIABLE pcIP AS CHARACTER   NO-UNDO.
RUN i-GetTcpInfo
    (OUTPUT pcName,
                 OUTPUT pcIp).
 
MESSAGE pcName pcIP
    VIEW-AS ALERT-BOX INFO BUTTONS OK.

in the main block (top of procedure) and you will see it works, (without the use of windows.p). Windows.p is by the way a library of procedures Jurjen wrote for his win32 api. You can download that as well there. Even though the sources are, as Jurjen mentiones, old. Most of them still work.

Regards,

Casper.
 

shireeshn

Member
Thanks casper i have run the prog sucussfully without windows.p,but wht is the use, how and where to use this windows.p, ur saying who developed also, then that may be usefull procedure. plzzzz help me in this :).

Thanks in advance Casper, and for immediate response :).
 

tamhas

ProgressTalk.com Sponsor
Did you look around on OE Hive where you got the procedure? All of that stuff is there in one book with examples.
 

Cringer

ProgressTalk.com Moderator
Staff member
I'm having a small problem with the code above. On the whole it works a charm, but if a user has multiple network connections active (such as active VPNs) it doesn't work. Currently my machine has the following IP Config:

Code:
Windows IP Configuration


Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . : <snip>
        IP Address. . . . . . . . . . . . : 192.168.1.113
        Subnet Mask . . . . . . . . . . . : <snip>
        Default Gateway . . . . . . . . . : <snip>

Ethernet adapter Local Area Connection 2:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 223.1.1.128
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . :
And the above code picks up Local Area Connection 2, whereas it should pick up the main local area connection. Is there something I can tweak to make this happen? If I disable the VPN that creates local area connection 2 then the IP is correct.
 

sphipp

Member
I suppose I could run os command for ipconfig and parse the address from that, but it's harder work than I hoped.

If you want to go down the ipconfig route then this should give you a list of IP Addresses, just choose ENTRY (1,cIPAddress) for the first one.

Code:
DEFINE VARIABLE cline      AS CHARACTER  NO-UNDO.
DEFINE VARIABLE cIPAddress AS CHARACTER  NO-UNDO.
INPUT THROUGH VALUE ("ipconfig |findstr /i ""ip"" |findstr /i ""address""").
REPEAT:
    cline = "".
    IMPORT UNFORMATTED cline.
    IF NUM-ENTRIES (cline,":") > 1 THEN ASSIGN cline = ENTRY (2,cline,":").
    cIPAddress = cIPAddress + (IF cIpAddress = "" THEN "" ELSE ",") + TRIM (cline).
END.
MESSAGE cipAddress
    VIEW-AS ALERT-BOX INFO BUTTONS OK.
 

Cringer

ProgressTalk.com Moderator
Staff member
I've slightly tweaked it to avoid the DOS box flashing up...

Code:
DEFINE VARIABLE lvLine       AS CHARACTER  NO-UNDO.
DEFINE VARIABLE lvIPAddress  AS CHARACTER  NO-UNDO.
DEFINE VARIABLE lvExportFile AS CHARACTER   NO-UNDO.
DEFINE VARIABLE lvPrefix     AS CHARACTER   NO-UNDO.

ASSIGN
  lvPrefix     = STRING(TODAY,"999999") + STRING(TIME)
  lvExportFile = SESSION:TEMP-DIRECTORY + lvPrefix + "ipconfig.txt".

OS-COMMAND SILENT VALUE("ipconfig |findstr /i ""ip"" |findstr /i ""address"" > " + lvExportFile).
INPUT FROM VALUE(lvExportFile). 
REPEAT:
    lvLine = "".
    IMPORT UNFORMATTED lvLine.
    IF NUM-ENTRIES (lvLine,":") > 1 THEN ASSIGN lvLine = ENTRY (2,lvLine,":").
    lvIPAddress = lvIPAddress + (IF lvIPAddress = "" THEN "" ELSE ",") + TRIM (lvLine).
END.
INPUT CLOSE.
MESSAGE cipAddress
    VIEW-AS ALERT-BOX INFO BUTTONS OK.
 
Top