Jacob + Visual Studio. Your custom .DLL to use in Java.
UPDATED: 02 January 2014
Tags:
io
In previous tutorial "Getting started with JACOB. An Example with Source Code" I gave you brief information about JACOB and how it works. In this tutorial we'll mainly focus on How you can create your own .DLL file for your customized system request. I created .cs (.dll) file to read system's encryption/decryption and signing certificates.
Technically speaking, Java guys take help of .Net guys around you. Don't waste time to learn another language. If you are geek then learn it. Lets roll...
Creating first .cs (.dll) file.
Step 1: Install Visual Studio on your Machine (Recommend 2010 or up).
Step 2: Create New Project > Class Library
Step 3: IDE will provide you one file called Class1.cs use or add new by Right click on Project > Add > New Item (Ctrl + Shift + A). Select Class and give filename. Here I named it math.cs
Step 5: Right click on Project and open up Properties > Build > Check Register for COM interop. Its important to follow all steps because one missing may lead to failed COM call from Java.
Step 6: You are done with .cs file now you need to create .dll file. Right click on Project and click on build. It'll create .dll and .tld file at <project>\bin\Debug
-->
Technically speaking, Java guys take help of .Net guys around you. Don't waste time to learn another language. If you are geek then learn it. Lets roll...
Creating first .cs (.dll) file.
Step 1: Install Visual Studio on your Machine (Recommend 2010 or up).
Step 2: Create New Project > Class Library
Step 3: IDE will provide you one file called Class1.cs use or add new by Right click on Project > Add > New Item (Ctrl + Shift + A). Select Class and give filename. Here I named it math.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; /** * @author vicky.thakor */ namespace jacobJavaQuery { public class math { public int add(int x, int y) { return x + y; } public int sub(int x, int y) { return x - y; } } }Step 4: Open up AssemblyInfo.cs file from project properties and set ComVisible value to true.
Step 5: Right click on Project and open up Properties > Build > Check Register for COM interop. Its important to follow all steps because one missing may lead to failed COM call from Java.
Step 6: You are done with .cs file now you need to create .dll file. Right click on Project and click on build. It'll create .dll and .tld file at <project>\bin\Debug
Create Setup file(.msi) for your .DLL
Step 1: Create New Project > Setup Project
Step 3: Right click on Middle Pane > Add > Assembly > Browse. Select .dll file from your debug folder, generated by 1st project (<project>\bin\Debug). It'll import two files jacobJavaQuery.dll and jacobJavaQuery.tlb
Step 4: Click on jacobJavaQuery.dll file and set Register Property to vsdraCOM.
Step 5: Right click on Project > View > Custom Actions
Step 6: Right click on Install and Commit folder one by one > Add Custom Action > Application Folder > jacobJavaQuery.dll
Step 7: Right click on Project and Build. Your output .msi file generated at <project>\bin\Debug\jacobJavaQuerySetup.msi
Note: You can set properties of your .msi like Icon, Install for all users or single, etc... You know what to do.
Calling .cs (.dll) functions from Java
We are done with creating COM visible .DLL file. Now we'll call add and sub function from Java + Jacob program.
package javaQuery.jacob; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.LibraryLoader; import com.jacob.com.Variant; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; /** * @author vicky.thakor * @date 28th December, 2013 * http://sourceforge.net/projects/jacob-project/ * First Program to understand how to use JACOB library */ public class JACOBCustomDLL { public static void main(String[] args) { /** * `System.getProperty("os.arch")` * It'll tell us on which platform Java Program is executing. Based on that we'll load respective DLL file. * Placed under same folder of program file(.java/.class). */ String libFile = System.getProperty("os.arch").equals("amd64") ? "jacob-1.17-x64.dll" : "jacob-1.17-x86.dll"; try { /* Read DLL file*/ InputStream inputStream = jacobExample.class.getResourceAsStream(libFile); /** * Step 1: Create temporary file under <%user.home%>\AppData\Local\Temp\jacob.dll * Step 2: Write contents of `inputStream` to that temporary file. */ File temporaryDll = File.createTempFile("jacob", ".dll"); FileOutputStream outputStream = new FileOutputStream(temporaryDll); byte[] array = new byte[8192]; for (int i = inputStream.read(array); i != -1; i = inputStream.read(array)) { outputStream.write(array, 0, i); } outputStream.close(); /** * `System.setProperty(LibraryLoader.JACOB_DLL_PATH, temporaryDll.getAbsolutePath());` * Set System property same like setting java home path in system. * * `LibraryLoader.loadJacobLibrary();` * Load JACOB library in current System. */ System.setProperty(LibraryLoader.JACOB_DLL_PATH, temporaryDll.getAbsolutePath()); LibraryLoader.loadJacobLibrary(); /** * Create ActiveXComponent using CLSID. You can also use program id here. * Next line(commented line/compProgramID) shows you how you can create ActiveXComponent using ProgramID. */ ActiveXComponent compProgramID = new ActiveXComponent("jacobJavaQuery.math"); System.out.println("The Library been loaded, and an activeX component been created"); int arg1 = 100; int arg2 = 50; /** * Call .cs(.dll) function/method as follow */ int result = Dispatch.call(compProgramID, "add", arg1, arg2).toInt(); System.out.println("Result: " + result); /* Temporary file will be removed after terminating-closing-ending the application-program */ temporaryDll.deleteOnExit(); } catch (Exception e) { e.printStackTrace(); } } }
Tags:
io
0 comments :