Using dll in Java with JNI

Dear Friends.
I have a dll which was developed in Visual Basic. If we use that in the VB code we can carry out certain methods.
My concern is: I want to use this dll in my java method. what all the methods that can be used in the VB should also be used in java method.
I know that this can be done using JNI, but please help me for getting starting, I am a bit new to this technology.
thanks so much.
Cheers,
Lakshman

Hi friends.
I have checked up with all the forums in this site and more. the registered dll file is sucessfully loaded in the my java file. i complied it and got my .h file...
until this point, i got everything correct. in all the articles i read, they have given an implementation in C/C++ fo the methods declared by including the header file.
but as per my requirement, i dont have my own implementations for any methods. I already have some methods in the dll which i want to call from the java program.
please give me your views on this..
thanks.
Lakshman Pilaka.

Similar Messages

  • How can i pass string from C++ DLL to Java via JNI?

    Hi everybody. I made a DLL with Borland C++. I must pass a string from this dll to Java via JNI.Namely i define a string variable in C++ DLL and i send this variable's value to Java via JNI.
    I can pass integers but i couldnt Strings. . How can i do this? is there any sample?

    Hi,
    So your function should be private static native String get_text();
    (It's often a good idea to make native methods private since when you change signatures you generally have to change java wrapper methods only).
    I know nothing about C++ strings but I'm pretty sure that you can convert it to char*, so
    do :
    char* szMyString = myString.toChar*();
    Then return from native with JNU_NewStringPlatform(env, szMyString)
    (see my 1st answer for JNU_NewStringPlatform() description).
    --Marc (http://jnative.sf.net)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Using dlls in java

    can u help me in creating a java application that incorporates a dll created in visual basic or visual c++?

    hey, throw your dukes ;-)
    here's sample code for JACOB that shows you how easy it is:
    import java.math.BigDecimal;
    import java.util.*;
    //jacob.jar:
    import com.jacob.com.*;
    import com.jacob.activeX.*;
    * Test usage of Visual Basic DLLs in Java with JACOB (JAva-COM Bridge). Requires a DLL in the Java
    * Lib Path with classes "TestProject.TestClass" and "TestProject.TestClass2" and the corresponding
    * functions/subs.
    * <p><b>Links:</b>
    * <ul>
    * <li><a href="http://danadler.com/jacob/">The JACOB Project: A JAva-COM Bridge</a>
    * <li><a href="http://groups.yahoo.com/group/jacob-project/">Yahoo group: jacob-project</a>
    * <li><a href="http://www.officecomputertraining.com/vbtutorial/tutpages/page32.asp">VB Dates and Time</a>
    * </ul>
    public class JacobTest {
         * Test application for JACOB.
         * @param args Not required.
        public static void main(String[] args) {
            System.out.println("Library path: "+System.getProperty("java.library.path"));
            ActiveXComponent activex = null;
            String dllClassName = "TestProject.TestClass";
            long time = System.currentTimeMillis();
            try {
                activex = new ActiveXComponent(dllClassName); //ProgId
                Object o = activex.getObject();
                Variant variant = null;
                System.out.println("\n*** Test function that returns a String:");
                variant = Dispatch.call(o, "getString", "Hello");
                System.out.println("getString('Hello'): "+variant.getString());
                List ml = new ArrayList();
                ml.add("mytest");
                System.out.println("\n*** Test function that returns a String:");
                variant = Dispatch.call(o, "getString", ml.get(0));
                System.out.println("getString('"+ml.get(0)+"'): "+variant.getString());
                System.out.println("\n*** Test function that returns an integer:");
                variant = Dispatch.call(o, "getInteger", new Integer(7));
                System.out.println("getInteger(7): "+variant.getShort());
                System.out.println("\n*** Test function that returns a double:");
                variant = Dispatch.call(o, "getDouble", new Double("12.34"));
                System.out.println("getDouble(12.34): "+variant.getDouble());
                System.out.println("\n*** Test function that returns a boolean:");
                variant = Dispatch.call(o, "getBoolean", new Boolean(true));
                System.out.println("getBoolean(true): "+variant.getBoolean());
                System.out.println("\n*** Test function that returns a date:");
                variant = Dispatch.call(o, "getDate");
                System.out.println("getDate(): "+variant.getDate());
                System.out.println("Java: "+new Date(toMilliseconds(variant.getDate())));
                System.out.println("\n*** Test function that returns a time:");
                variant = Dispatch.call(o, "getTime");
                System.out.println("getTime(): "+variant.getDate());
                System.out.println("Java: "+new Date(toMilliseconds(variant.getDate())));
                System.out.println("\n*** Test function that returns a timestamp:");
                variant = Dispatch.call(o, "getTimestamp");
                System.out.println("getTimestamp(): "+variant.getDate());
                System.out.println("Java: "+new Date(toMilliseconds(variant.getDate())));
                System.out.println("\n*** Test function that returns a timestamp:");
                variant = Dispatch.call(o, "getTimestamp");
                System.out.println("getTimestamp(): "+variant.getDate());
                System.out.println("Java: "+   new Date(toMilliseconds(variant.getDate())));
                System.out.println("\n*** Test function with optional parameter:");
                variant = Dispatch.call(o, "callOptionalParas", "eins", null, "drei");
                System.out.println("callOptionalParas(): "+variant.getString());
                System.out.println("\n*** Test function that returns a 2 dimensional array:");
                variant = Dispatch.call(o, "getArray", "*!*");
                SafeArray array = variant.toSafeArray(false);
                System.out.println("getArray('zwo'): "+array.getString(0,2));
                System.out.println("\n*** Test function with more than 8 parameters:");
                Object[] paras = new String[] {"1", "2", "3", "4", "five", "six", "seven", "eight", "nine", "ten"};
                variant = Dispatch.callN(o, "callNParas", paras);
                System.out.println("callNParas(..."+paras[9]+"): "+variant.getString());
                System.out.println("\n*** Test function that returns another COM object:");
                variant = Dispatch.call(o, "getTestClass2", "Test Object 1");
                Object class2 = variant.getDispatchRef();
                variant = Dispatch.call(class2, "getValue", "myPara!");
                System.out.println("getTestClass2().getValue('myPara'): "+variant);
    //            System.out.println("\n*** Open a Microsoft Word document:");
    //            ActiveXComponent oWord = new ActiveXComponent("Word.Application");
    //            oWord.setProperty("Visible", new Variant(true));
    //            Object oDocuments = oWord.getProperty("Documents").toDispatch();
    //            Object oDocument = Dispatch.call(oDocuments, "Open", "E:/temp/TestDoc.doc").toDispatch();
    //            Object oSelection = oWord.getProperty("Selection").toDispatch();
                System.out.println("\n*** Test function with new COM object parameter:");
                ActiveXComponent com2 = new ActiveXComponent("TestProject.TestClass2"); //ProgId
    //            Dispatch d2 = (Dispatch) com2.getObject();
    //            Variant v = new Variant(d2);
    //            Dispatch vp = v.toDispatch();
                variant = Dispatch.call(o, "callWithComPara", com2);
                System.out.println("callWithComPara(): "+variant.getString());
                System.out.println("\nDuration [ms]: "+(System.currentTimeMillis() - time));
            } catch (Throwable t) {
                System.err.println("ERROR with ActiceX component '"+dllClassName+"': "+t);
        }//main()
         * Convert a Visual Basic time double value (e.g. from functions Date(), Time(), Now()) to a Java long value (milliseconds).
         * Visual Basic time values are based to 30.12.1899, Java time values are based to 1.1.1970 (= 0 milliseconds).
         * The difference is added to the Visual Basic value to get the corresponding Java value. The Visual Basic double value reads:
         * <day count delta since 30.12.1899>.<1 day percentage fraction>, e.g. "38100.6453" means: 38100 days since 30.12.1899
         * plus (24 hours * 0.6453). Example usage: <code>Date javaDate = new Date(toMilliseconds(vbDate));</code>.
         * @param vbTimestamp Time value from Visual Basic.
         * @return Number of milliseconds since 1.1.1970.
        static public long toMilliseconds(double vbTimestamp) {
            long result = 0;
            final long vbZeroTime = -2209165200000L; //30.12.1899 00:00:00 in milliseconds
            final long oneDay = 86400000; //1 day in milliseconds (24 * 60 * 60 * 1000)
            BigDecimal bd = new BigDecimal(""+vbTimestamp);
            long dayCount = bd.longValue(); //value left of point; number of days since 30.12.1899
            BigDecimal tp = bd.subtract(new BigDecimal(""+dayCount)); //fractional part is factor for 1 day (e.g. 0.5 => 12 hours)
            long date = vbZeroTime + (dayCount * oneDay); //compute java date
            //avoid problems with daylight savings time by setting time to 00:00:00 :
            GregorianCalendar dateCal = new GregorianCalendar();
            dateCal.setTimeInMillis(date);
            dateCal.set(Calendar.HOUR, 0);
            dateCal.set(Calendar.HOUR_OF_DAY, 0);
            dateCal.set(Calendar.MINUTE, 0);
            dateCal.set(Calendar.SECOND, 0);
            dateCal.set(Calendar.MILLISECOND, 0);
            date = dateCal.getTimeInMillis();
            long time = (long) (oneDay * tp.doubleValue()); //compute java time
            result = date + time;
            return result;
        }//toMilliseconds()
    }//JacobTest()

  • How to access data structures in C dll from java thru JNI?

    We have been given API's( collection of C Functions) from some vendor.
    SDK from vendor consist of:
    Libpga.DLL, Libpga.h,Libpga.lib, Along with that sample program Receiver.h (i don't know its written in C or C++), I guess .C stnads for C files?
    Considering that I don't know C or C++ (Except that I can understand what that program is doing) & i have experience in VB6 and Java, In order to build interface based on this API, I have two option left, Use these dll either from VB or Java.
    As far as I know, calling this DLL in VB requires all the data structures & methods to be declared in VB, I guess which is not the case with Java (? I'm not sure)
    I experiemnted calling these function from Java through JNI, and I successfully did by writting wrapper dll. My question is whether I have to declare all the constants & data structures defined in libpga.h file in java, in order to use them in my java program??
    Any suggesstion would be greatly appreciated,
    Vini

    1. There are generators around that claim to generate suitable wrappers, given some dll input. I suggest you search google. Try JACE, jni, wrapper, generator, .... Also, serach back through this forum, where there have been suggestions made.
    2. In general, you will need to supply wrappers, and if you want to use data from the "C side" in java, then you will need java objects that hold the data.

  • Using DLL functions created with LabVIEW 6i in CVI 4.0.1

    I want use SQL functions from LabVIEW 6i in LabWindows/CVI 4.0.1. Building DLL with this LabVIEW-functions was succesfull, but after using DLL in CVI project and run, error message was generated: FATAL ERROR : LABVIEW.LIB was not called from a LabVIEW process. Can you help me? In attachment are VIs and build script for this DLL.
    Attachments:
    DLL.ZIP ‏24 KB

    The SQL toolkit appears to be an ActiveX program. Why not call the objects with activeX in CVI directly?
    There are several hits for labview.lib on ni's site. Goto http://search.ni.com/?col=alldocs&layout=TechResources&ql=a
    and search for labview.lib.

  • Using the command "java" with DOS

    Hey, until yesterday, I was able to simply use the commands "java, or javac" to run, or compile java in DOS. But now for some reason I can't use these commands. I have to use the full addresses of the java and javac.exe files. How can I fix this? Do I have to be in the \bin folder to use these with the command name only? It's a pain to store my files in the bin folder. Any help would be appreciated.

    See here: http://java.sun.com/j2se/1.4/install-windows.html#Environment

  • Problem in Packet.dll file calling with JNI

    I called wpcap.dll and packet.dll using JNI & while using native method of packet.dll, I am getting the error:
    The Procedure entry point PacketGetReadEvent could not be loaded in the dynamic link library packet.dll.
    Please help.
    Thanks in advance.

    what are you using for that ?
    JCOB or ezjcom ?I am developing Packet Capture Program in Java, for
    network monitoring. For that, I am using these
    dynamic link library.Thanks for information but how I would know ?

  • Pass an array of integers from C to Java with JNI

    Hello,
    I have a C function that returns a struct me. The fields of this struct are all of type integer. I, with the fields of this structure create an array of integers in C language I would like to pass this array of integers c, in Java using JNI.
    How can I make this?

    I don't see how you compiled that.
    It certainly will not compile for me.
    jintArray position=(jintArray)(*env)->NewIntArray(env,2);That is the invocation idiom for C code.
    jint f[2];You cannot have a variable declaration in the middle of a method (block) in C code. That is only allowed in C++.
    C code and C++ code is not the same.
    The following is C++ code. Note that it does NOT include getBinaryPoint() since that method could be the source of the problem.
    jintArray position = env->NewIntArray(2);
    if(position==NULL)     return NULL;
    jint f[2];
    f[0]=3;
    f[1]=4;
    env->SetIntArrayRegion(position,0,2,f);
    return position;

  • How to return a char [][] from c++ to java with JNI

    hi , i am new to JNI and i am having some troubles with a special task :
    in fact i am supposed to treat some data stored in a char[100][100] value and then return the result to java , the result is the sam value bur modified i mean a char [100][100] variable too...
    i saw that we can rely on
    JNIEXPORT jobjectArray JNICALL Java_JNITest_newArray (JNIEnv *env, jclass, jint size )
    jobjectArray joa = ... );  // what in my case ?;
    jsize len1 = (jsize)env->GetArrayLength(joa);
    for (int i=0; i<l en1; i++)
    jintArray colonne = (jintArray)env->GetObjectArrayElement(joa, i);
    jsize len2 = (jsize)env->GetArrayLength(colonne);
    jint *element = env->GetIntArrayElements(colonne, 0);
    for(int j=0; j<len2; j++)
           jint res = element[j];
                 // process some treatment here
            // how now to return the result to java : joa modified !!
    [/code]
    may be some errors present in this piece of code ...but i rely on your help to have the solution!! please i am really desperated !                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    You know that any JNI function for an array handles or creats only one-dimensional array. But if you know Java array structure then with these functions you can create Java arrays of any dimensions.
    A Java multi-dimensional array is a set of nested one-dimensional arrays (consider dimensions from left to right). I give you only schema of this process. Let create the array of your type (for example, char [2][3]):
    1) create one-dimensional object array "[C"[/b] of size two for the first dimension (and assign it to some variable of type jobjectArray) ;
    2) create two three-dimentional char arrays and assign these local references to each element of the array created in prev. step;
    3) fill each array created in (2) with your data (characters);
    4) return to Java code the reference created in the step (1) and do not delete local references created in steps (1) and (2).

  • How to use VS2005 dlls with jni?

    Hello,
    I have a java application using jni and some dlls.
    Previously this dlls were created using VS2003. Now I want to use VS2005 and I
    encountered some problems.
    Everything is working on my station were I have installed VS2005 but when I try to run the
    application on other station (a test station) is not working.
    I get the following error:
    java.lang.UnsatisfiedLinkError: C:\App\Test.dll: This application has failed to start because
    the application configuration is incorrect. Reinstalling the application may fix this problem
         at java.lang.ClassLoader$NativeLibrary.load(Native Method)
         at java.lang.ClassLoader.loadLibrary0(Unknown Source)
         ......First I tried to search all the dependences and copied all missing dlls from my work station to the test
    station in the application directory. Still not working.
    when dlls were made with VS2003 I had only 3 dependences: mfc71d, msvcp71d, msvcr71d. With this dlls in
    the application directory everything was fine.
    Second I tried to install .Net framework(2.0) and vcredist (VS2005_sp1) but still nothing.
    What I'm doing wrong? Is somebody which is using dlls made in VS2005 with jni?
    Thank you
    ,Dragos

    IIRC, that message means the manifest file in the DLL points to assembly that your computer doesn't have, or it has no manifest file.
    You either need to:
    -recompile using static linking so that it has no dependencies
    -stick the correct manifest in
    -include the entire SxS (site by side) assembly folders with your application (only the ones you're dependent on). These are found in something similar to:
    C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\redist\x86

  • Executing DLLs from Java using JNI vs Runtime.exec()

    I am trying to understand the best way to execute a dll using java. The dll I am testing with takes a few input parameter. If I use JNI with System.loadLibrary("dll"); what do I need to do to pass the arguements in as well? Can I just add the arguements in the java code like private int xyz = "value"; and expect that the dll will accept them or is there special definitions I have to set up in the dll to make it work?
    Alternatively I was looking at using Runtime to execute the dll by using Runtime.exec("dll param1 param2",env,filePath); Does anyone know if there are drawback to doing it this way vs. using JNI or is this just as efficient? Any help would be appreciated.

    You seem to be confused...
    "execute a dll using java"
    Unless I'm mistaken, a dll is not executable. A dll is a library of code. This code has (hopefully) some well-defined entry points (declared in a header file somewhere) which you must call in a C/C++ file. The arguments you pass to the dll will come from java through JNI.
    As far as your understanding of this entire process, it is obviously confused beyond the scope of a simple reply. I recommend you read the examples and ALL of the documentation available here:
    http://java.sun.com/docs/books/tutorial/native1.1/index.html
    When you get the/an example running (or just set up to run) then post your code and comments here for more help.
    Ian

  • Problem with JNI and Parallel Port dll

    Hi. I'm doing some testes with JNI. Firs i followed the netbeans tutorial for doing a C programa that prints somthing for java.. like.. "Hello java from C".
    So i tried to load a dll from my dll. Like, I have this dll to use the parallel port on windows Xp. So i created a dll to access it an comunicates eoth java.
    I did everything just fine.
    When I start my Java app, the first thing it does is to load this parallel port dll and configure the functions of it.
    After that .. I get this error
    EXCEPTION_FLT_STACK_CHECK (0xc0000092) at pc=0x0093d269, pid=2284, tid=3000
    Can someone explain why ?
    (Sorry if i wasn't clear enough, english is not my native language, so ask if you don't understand something.. )

    hi ,
    I met the same problem this morning, and searched the www.google.com in order to solve it, as a result, your article was shown on my screen. :)
    Till now I have read some technical information and solved my problems. Maybe the solution be useful to you:
    ==============================
    error message : (Environment : Tomcat 5, Windows 2003, Mysql5)
    2006-3-29 11:53:48 org.apache.catalina.core.StandardWrapper unload
    message: Waiting for 2 instance(s) to be deallocated
    ==============================
    cause: the number of connection to database exceeded.another word,too many connections.
    ==============================
    solution: close the connection when it becomes useless for your program. :)
    ==============================
    ps. Sorry for my weak English . hehe ....

  • How to Use DLL Function in Java Applet

    Hi all,
    I have been assigned a task to develop java applet. The problem is, I need to use DLL functions in my applet to read records. Could you pls anyone guide me how to interface DLL and applet to read records with sample code?
    I'm using JDK 1.5.0_06 and Windows XP OS. thanx..
    best rgds,
    jpdbay

    You will need to use Java Native Interface JNI. Ther are many posts on the subject, please search, and this is the documentation:
    http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/jniTOC.html

  • Integerating objective c with java through JNI in MAC

    Hi,
    I hv some code in objective c and i hv to call some methods from java code. So i want to use JNI interface for that.
    Could any body give me refrence on how to go about it. I know little bit about integerating c code and java thorugh jni on windows.
    Thanks in advace.
    Regards,
    Prashant

    you need to create dll using vc++ or Borland compiler
    i will try to help you out.
    //mac.java
    class mac
        public native void getMac();
        private native String getLine(String prompt);
        static {
         System.loadLibrary("myMac");//This is myMac.DLL
         /*if generated by borland
         System.loadLibrary("mymac");//This is mymac.dll
         public static void main(String[] args)
        mac mc=new mac();
        String input = mc.getLine("Enter Some Thing ");
        System.out.println("You Entered " + input);
        System.out.println(mc.getMac());
    }prompt> javac mac.java
    prompt> javah -jni mac
    Borland compiler
    /*mymac.c*/
    #include <jni.h>
    #include "mac.h"
    #include <stdio.h>
    JNIEXPORT void JNICALL
    Java_mac_getMac(JNIEnv *env, jobject obj)
        printf("I am in  getmac dir in mymac.dll (Borland) \n");
        return;
    JNIEXPORT jstring JNICALL
    Java_mac_getLine(JNIEnv *env, jobject obj, jstring enter)
        char buf[128];
        const char *str = (*env)->GetStringUTFChars(env, enter, 0);
        printf("%s", str);
        (*env)->ReleaseStringUTFChars(env, enter, str);
        scanf("%s", buf);
        return (*env)->NewStringUTF(env, buf);
    }To generate DLL
    create bcc32.cfg file in %BORLAND_INSTALL%/BCC5/bin Folder
    In the bcc32.cfg file add following code
    -I"%BORLAND_INSTALL%\BCC55\include;%JAVA_HOME%\include;%JAVA_HOME%\include\win32"
    -L"%BORLAND_INSTALL%\BCC55\lib;%BORLAND_INSTALL%\BCC55\Lib\PSDK;"
    i.e the path for borland compiler and java.
    now goto prompt and say
    prompt>bcc32 -tWM -tWD mymac.c
    This will create mymac.lib File
    Now say
    prompt>implib -c mymac.lib mymac.dll
    That's it!!
    there you are with dll
    Now you can run the mac.class file by using java
    VC++
    Click File->New->Win32Dynamic-Link Library
    Give name and Select
    A simple DLL project
    You will have
    myMac.CPP file
    // myMac.cpp : Defines the entry point for the DLL application.
    #include "stdafx.h"
    #include "mac.h"
    #include "jni.h" //can copy or give full path
    #include <math.h>
    BOOL APIENTRY DllMain( HANDLE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
        return TRUE;
    JNIEXPORT void JNICALL
    Java_mac_getMac(JNIEnv *env, jobject obj)
        printf("You are in getMac (VC++) DLL\n");
    JNIEXPORT jstring JNICALL
    Java_mac_getLine(JNIEnv *env, jobject obj, jstring enter)
        char buf[128];
        const char *str = env->GetStringUTFChars(enter, 0);
        printf("\n%s", str);
        env->ReleaseStringUTFChars(enter, str);
        scanf("%s", buf);
        return env->NewStringUTF(buf);
    }Hope this is useful
    regards
    kanad Deshpande

  • Using DLL  with JSP

    I have a dll component writing with C++.
    I want to use this DLL in a JSP Pages.
    Is it possible?
    How could i do?
    I don't want to use JNI because I don't know JAVA.
    thanks.
    Bye.

    I have a dll component writing with C++.
    I want to use this DLL in a JSP Pages.
    Is it possible?Yes.
    How could i do?With JNI.
    I don't want to use JNI because I don't know JAVA.Then you are screwed. Maybe you should use ASP.

Maybe you are looking for

  • JSP - Prevent auto creation of JavaBean

    Is there is a directive which i can use in JSP to prevent the <useBean> tag auto creating a new instance of a java bean if it cannot be found in the session? As i want to manage where the javaBean is created. Thanks Ian

  • Sort a non database item

    I have one datablock (ma_erz) in my forms based on the table test1. Datase-Items of table test1: :ma_erz.MAE_ID, :ma_erz.MAE_SYSART Non-Database-Items: :ma_erz.L_KM_USERNAME My problem is, that I could not sort the records of a non-database-item. Doe

  • ADFS 3.0 & ldpinitiated.aspx

    I realize that with ADFS 3.0, Microsoft has done away with a number of things since ADFS 2.1 including using http.sys as the basis for the ADFS "website" versus using IIS.  (http://blog.auth360.net/2013/09/13/first-impressions-ad-fs-and-windows-serve

  • Rollback not working with CMT or BMT ?

    Is the container (OC4J) not rolling back transactions ? Are you using CMT or BMT ? I had this problem with a stateless session bean using CMT and / or BMT. The problem was related to my datasource configuration. With OC4J you can have two types of da

  • Hi We got the below alert but its not reported in the alert.log

    The following SQL error occurred ORA-00054:resource busy had accquire with NOWAIT specified but its not reported in the alert log file Regards