How to get the users IP address

Posted by ojfoggin on 15-Sep-2009 09:06

Hi,

I'm trying to create a record that requires the user's IP address.  I know how to use os-command to run ipconfig (Windows XP) but this just opens the command prompt.

Is there any way of using os-command so that it returns a character of the IP Address?

Or is there something else that can be used instead?

Thanks for the help

Oliver

All Replies

Posted by Admin on 15-Sep-2009 09:20

Which Version of Progress? Is GUI for .NET an option? There's a bunch of stuff in the System.Net.* namespaces.

Posted by ojfoggin on 15-Sep-2009 09:22

Sorry,

Progress 10.1B  GUI for .NET isn't an option at the moment (unfortunately).

Thansk for the help

Posted by ojfoggin on 15-Sep-2009 09:24

I've just worked out that I can ipconfig and output it to a file and then read the file back in to the code.

I just hope there's some other way around it.

Thanks again

Posted by Admin on 15-Sep-2009 09:27

This works for me on 10.2A:

USING System.* .

USING System.Net.* .

DEFINE VARIABLE strMachineName AS CHARACTER   NO-UNDO.

DEFINE VARIABLE ipHost AS IPHostEntry NO-UNDO .

DEFINE VARIABLE ipAddr AS IPAddress NO-UNDO EXTENT .

strMachineName = Dns:GetHostName().

ipHost = Dns:GetHostByName(strMachineName) .

ipAddr = ipHost:AddressList .

MESSAGE strMachineName SKIP

        ipHost:ToString() SKIP

        ipAddr[1]:ToString()

    VIEW-AS ALERT-BOX INFO BUTTONS OK.

Posted by ojfoggin on 15-Sep-2009 09:30

Thanks again.

That doesn't seem to work with 10.1B

Oh well, I'll have a go at getting the ipconfig thing to work.

Thanks

Oliver

Posted by Admin on 15-Sep-2009 09:32

I know, but I couldn't resist... Windows API access using .NET classes is just too much fun.

Good luck anyway!

Posted by ojfoggin on 15-Sep-2009 10:04

OK, I've got a bit of code that will do the trick if anyone wants to use it.

It's a bit clunky but it's the best I coudl come up with.

(I don't know how to put code sections)

Hmm... a quote will have to do for now.

DEFINE VARIABLE lc-line AS CHAR NO-UNDO.

DEFINE VARIABLE lc-address AS CHAR NO-UNDO.

DEFINE STREAM s-load.


DOS SILENT "ipconfig > c:\outputfile.txt".

INPUT STREAM s-load FROM "c:\outputfile.txt".

REPEAT:

     IMPORT STREAM s-load UNFORMATTED lc-line.

     IF TRIM ( lc-line ) BEGINS "IP Address" THEN

     DO:

          ASSIGN lc-address = SUBSTRING ( lc-line , INDEX ( lc-line , ":" ) + 2 ).

          LEAVE.

     END.

END.

INPUT STREAM s-load CLOSE.

OS-DELETE "c:\outputfile.txt".

MESSAGE lc-address VIEW-AS ALERT-BOX.

I realise it will only work on windows and it will only get the fir NIC's IP Address so it's not the best but it works for me using the work PCs.

Thanks

def stream s-load.

def var lc-line as char no-undo.
def var lc-address as char no-undo.

dos silent "ipconfig > c:\output.txt".

input stream s-load from "c:\output.txt" .

repeat:
    import stream s-load unformatted lc-line.

    if trim(lc-line) begins "IP Address" then
    do:
        assign lc-address = substring(lc-line, index(lc-line, ":") + 2).
        message lc-address view-as alert-box.
    end.

end.

input stream s-load close.

os-delete "c:\output.txt".

Posted by egarcia on 15-Sep-2009 16:12

Hello,

I just wanted to mention that the code with DOS SILENT and INPUT statement can also be done using INPUT THROUGH.

You can replace the statements with:

INPUT STREAM s-load THROUGH "ipconfig".

With the previous approach of using a temporary file, the temporary file needs be unique if multiple users can use it concurrently.

Using INPUT THROUGH, there is no temporary file created.

Regards.

Posted by kevin_saunders on 16-Sep-2009 02:24

You should have searched on OEHive.org..

http://www.oehive.org/node/458 has an example of using the Windows API to get the IP address.. Code is lot longer, but cleaner, IMO.

Posted by Admin on 18-Sep-2009 14:02

ojfoggin schrieb:

REPEAT:

     IMPORT STREAM s-load UNFORMATTED lc-line.

     IF TRIM ( lc-line ) BEGINS "IP Address" THEN

     DO:

          ASSIGN lc-address = SUBSTRING ( lc-line , INDEX ( lc-line , ":" ) + 2 ).

          LEAVE.

     END.

END.

INPUT STREAM s-load CLOSE.


Be careful with the result of parsing command line output. On a German 64bit Windows 7 system you won't find a hit:

Drahtlos-LAN-Adapter Drahtlosnetzwerkverbindung:

   Verbindungsspezifisches DNS-Suffix: intern

   Verbindungslokale IPv6-Adresse  . : fe80::c0bd:b479:d503:eee%11

   IPv4-Adresse  . . . . . . . . . . : 192.168.42.160

   Subnetzmaske  . . . . . . . . . . : 255.255.255.0

   Standardgateway . . . . . . . . . : 192.168.42.1

I think even on an english Windows 7 machine the string you'd have to look for would be IPv4-Address.

Is this here an option: http://www.oehive.org/node/458

Posted by fixitchris on 28-Sep-2009 10:14

I can't believe how elegant .NET is inside OE!

Posted by Admin on 29-Sep-2009 08:03

I can't believe how elegant .NET is inside OE!

So I guess Progress should do more to tell people about it!

GUI for .NET offers so much more than just Office 2007 stlye.

Posted by fixitchris on 29-Sep-2009 10:42

Sorry to go offtopic but have you successfully used the Reflection namespace or p/invoke inside OE?

Posted by Admin on 29-Sep-2009 10:45

Sorry to go offtopic but have you successfully used the Reflection namespace or p/invoke inside OE?

Both of them. But reflection will only work with .NET classes, not ABL classes. There should be a reflection sample from me somewhere on this forum.

Posted by fixitchris on 29-Sep-2009 11:27

Found it...  The syntax reminds me of using Powershell against .NET ...

This thread is closed