Getting the computer’s IP Address via LotusScript in 5 lines of code (or, LS2J to the rescue)

Domino/Notes Version: 6.x and higher

At times, it seems that I have pushed LotusScript to its limits. There are a lot of things it can do but sometimes there are better solutions. Getting the current computers IP address is one of those things.

Getting the IP address is certainly possible through LotusScript. But, instead of a single call to a single routine through the Windows API like one might think, you really need to jump through several hoops to do it. The result is a lot of code and a dependence on the Winsock DLL. One example of this method is on the VBNet Visual Basic Developers Resource Centre.

I did not like that solution. I don’t know about you, but, I get nervous anytime I start referencing lots of Windows DLL files. It could get ugly in the future as Windows evolves. There are no guarantees and it probably means careful testing when you upgrade your OS. I like my integration with the Windows API to remain fairly simple.

I figured there must be a better solution out there. It turns out that there is and, as an added bonus, the result works on platforms other than Windows! The key is that you can accomplish this same task in essentially two lines of Java code. Below is a Java agent that prints the IP address to the Java console with the two important lines in bold.

Code sample

import lotus.domino.*;
import java.net.InetAddress;

public class JavaAgent extends AgentBase {
   public void NotesMain() {
      try {
         Session session = getSession();
         AgentContext agentContext = session.getAgentContext();
         InetAddress thisIp = InetAddress.getLocalHost();
         System.out.println("IP:"+thisIp.getHostAddress());

      } catch(Exception e) {
          e.printStackTrace();
      }
   }
}

That’s great if you are writing Java but if you need to work in the UI you are probably writing LotusScript. You could use the environment or even profile documents to facilitate the communication between Java and LotusScript but that is kludgy. A better approach is that through LS2J you can access these same Java classes from LotusScript. It takes only a few more lines to instantiate the correct Java object and invoke the method to get the IP address.

Code sample

Uselsx "*javacon"

Function getIPAddress() As String
   ‘***************************************************************
   ‘** getIPAddress
   ‘**
   ‘** parameters:
   ‘**    N/A
   ‘**
   ‘** returns:
   ‘**    String with IP address of current computer in format
   ‘**     
www.xxx.yyy.zzz
   ‘***************************************************************

   Dim jSession As JavaSession
   Dim jClass As JavaClass
   Dim jObject As JavaObject
   Dim jMethod As JavaMethod

   On Error Goto errorHandler

   Set jSession = New JavaSession()
   Set jClass = jSession.GetClass("java/net/InetAddress")
   Set jMethod = jClass.GetMethod("getLocalHost","()Ljava/net/InetAddress;")
   Set jObject = jMethod.Invoke()

   getIPAddress = jObject.getHostAddress()

   Exit Function

errorHandler:
   getIPAddress = ""

   Exit Function

End Function

It’s as simple as that; 5 lines of code. Using java.net.InetAddress you can also get host (computer) name. I’m sure there are dozens of other uses for this type of solution primarily if you think about Java’s strengths in areas like networking where LotusScript is generally weak.