Monday, August 02, 2010

What is JaWin and how to use it?

JaWin
The Java/Win32 integration project (Jawin) is a free, open source architecture for interoperation between Java and components exposed through Microsoft's Component Object Model (COM) or through Win32 Dynamic Link Libraries (DLLs).
For more information visit http://jawinproject.sourceforge.net/

How to use Jawin - A practical example
================================

a). If you have .tlb (COM Type Library) file available for the COM object you want to use then skip Step1 and proceed to Step3.
b). If you already have .Net component ready which you want to use from Java code then proceed to Step2:
c). If you want to develop a .NET component to be used via JAWIN then proceed to Step1:


Step1: Developing a .Net component (class library)

(a). Create an interface and before its declaration put .net attribute as given below
      [Guid("3823a63d-5891-3b4f-A460-DB0FB847075A")]
  See the code snippet below for mor einformation:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Org.Jawin.ManageWinProcess
{
       [Guid("3823a63d-5891-3b4f-A460-DB0FB847075A")]
       public interface IWinProcessOperations
       {
       Boolean IsProcessActive(String sProcessName);
       Boolean KillRunningProcess(String sProcessName);
}

b) Now Implement the interface created above and in the implementation class before the name of class declaration, type the following attributes
[Guid("25c2f5a2-1afe-36ce-BE27-84E040F5E19A")]
[ProgId("Jawin.WinProcessOps")] // You would be using this prog id while creating an instance of this class in JAVA.


See the code snippet below for more information:


using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace Org.Jawin.ManageWinProcess
{

         [Guid("25c2f5a2-1afe-36ce-BE27-84E040F5E19A")]
         [ProgId("Jawin.WinProcessOps")]
         public class Operations:IWinProcessOperations
         {
             public Operations(){ }
             public Boolean isProcessActive(String ProcessName)
             {
                  String machine = ".";
                  Boolean processRunning = false;
                  // Get the current processes
                   System.Diagnostics.Process[] runningProcesses = System.Diagnostics.Process.GetProcesses(machine);
                 // Find those that match the specified regular expression
                  Regex processFilter = new Regex(ProcessName, RegexOptions.IgnoreCase);
                  foreach (System.Diagnostics.Process current in runningProcesses)
                   {
                    // Check for a match.
                     if (processFilter.IsMatch(current.ProcessName))
                       {
                           processRunning = true;
                           break;
                        }
                   }


               return processRunning;
             }


public Boolean KillRunningProcess(String ProcessName)
{
String machine = ".";
Boolean processKilled = false;
// Get the current processes
System.Diagnostics.Process[] runningProcesses = System.Diagnostics.Process.GetProcesses(machine);
// Find those that match the specified regular expression
Regex processFilter = new Regex(ProcessName, RegexOptions.IgnoreCase);

foreach (System.Diagnostics.Process current in runningProcesses)
{
  // Check for a match.
  if (processFilter.IsMatch(current.ProcessName))
  {
   current.Kill();
   processKilled = true;
   break;
  }
}
return processKilled;
}


public bool KillAndStartProcess(String ProcessName)
{
String machine = ".";
Boolean processKilled = false;
// Get the current processes
System.Diagnostics.Process[] runningProcesses = System.Diagnostics.Process.GetProcesses(machine);
// Find those that match the specified regular expression
Regex processFilter = new Regex(ProcessName, RegexOptions.IgnoreCase);
foreach (System.Diagnostics.Process current in runningProcesses)
 {
  // Check for a match.
  if (processFilter.IsMatch(current.ProcessName))
  {
   current.Kill();
   break;
  }
 }
 System.Diagnostics.Process.Start(ProcessName);
 return true;
}}}



Building .NET Library
In order to build the .Net Library code you have just written from the Visual Studio IDE, configure the settings as shown below:



Important: If this setting is not configured, build would generate a DLL but the TLB file generated using regasm on .net command prompt would create .tlb file which would not expose any method in JaWin browser. So this setting is crucial.




Step 2: Creating .tlb file for the .net component

You can create a tlb file for the .net component using Assembly Registration Tool in the Visual Studio command prompt.
Syntax : Regasm /tlb “dll path with dll name”

e.g : regasm /tlb “c:\winprocess.dll” (suppose name of our dll is winprocess.dll and it is available at c:\)

The above command would generate the corresponding .tlb file.


Step 3: How to use JAWIN BROWSER and create JAWIN stub for Java for COM

Download JaWin and extract its files in a directory. Suppose JaWin is available in c:\JaWin folder. Open command prompt and reach the ‘typeBrowser’ folder in Jawin directory.

C:\Jawin\jawin-2.0-alpha1\typebrowser

And then type
java -Xmx256M -jar jawinBrowser.jar

and hit Enter. [Note: Xmx256M in the above command allocates 256 MB RAM to this process]. It would then start the Applet as shown below:



 Go to option ‘Project’ -> ‘New Project’ and type project name and then click on ‘New’ button as shown below:


Reach to your TLB file and when you are done, the contents of your tlb would be displayed as shown below:



Expand your interface (IwinProcessOperations in this case) and then select ‘Code Generation’->’Generate Selected Code’ and it would show the corresponding JAVA code under ‘Code’ tab as shown below:


Save this code as a JAVA class. Now you can import this class and use its methods in your code.
Note: Don’t try to modify this class file.


HOW to use JAWIN stub in java application

a). Include jaWin.jar in your build path and

b). Point to the path of jaWin.dll in run configuration by adding the following string under VM arguments(if running the code in eclipse or RAD)
-Djava.library.path="c:\JawinPath"

c). If you have .NET dll, put it in your project folder and register it using regasm command. If you are using pure COM dll then use regsvr32 command to register the COM component.

d). If you are using .net dll which was exposed as a COM then you would need to copy the .net dll in JDK\bin directory otherwise your program would not be able to find the object. This step is nor required if you are using pure COM. For more information visit http://jawinproject.sourceforge.net/jawinarchitecture.html


c). Import/add the Java class (JaWin stub) file created above in your application and then use it. The below snippets shows how to use this stub.


public static void main(String[] args)

{
boolean bResult;
IWinProcessOperations ops=null; //Jawin stub for the COM component
  try
  {
   //I have used Jawin.WinProcessOps because this was mentioned as a Prog id in the //class in .net.
   // You should use the prog id of your COM accordingly.
   ops = new IWinProcessOperations("Jawin.WinProcessOps");
   }


  catch (COMException e1)
   {
    e1.printStackTrace();
    }
  try
  {
   bResult=ops.KillRunningProcess("WINWORD");
  System.out.println("Process is active " + bResult);
  }
  catch (COMException e)
  {
   e.printStackTrace();
  }
}