Brandon
Brandon

Reputation: 23498

JNI reading from two DLLs with one class

I have two DLL's: a directx dll, which exports GetNativeBuffer, and an opengl dll, which does the same.

I use the following Java class to call GetNativeBuffer, to read an image from the loaded dll.

class DllLoader {

    private ByteBuffer Buffer = null;
    private BufferedImage Image = null;
    public boolean LibraryLoaded = false;

    private static native void GetNativeBuffer(IntBuffer Buffer);
    private int ByteSize = 0, Width = 0, Height = 0;

    public DllLoader(ByteBuffer Buffer, int ImageWidth, int ImageHeight) {
        this.Buffer = Buffer;
    }
}

Problem: If both DLL's are loaded by the program, how do I specify which one to read from? Do I have to make two separate classes? Do I have to rename the functions and have two native functions?

Upvotes: 2

Views: 266

Answers (1)

JoshDM
JoshDM

Reputation: 5062

You should make two classes, one for each DLL. If you are naming your java classes identically, it might be easier to separate them into different subpackages, for example:

package com.stackoverflow.jni.opengl;

/*
 * GENERATE HEADER FILE FROM GENERATED CLASS AS NEEDED VIA
 *   javah com.stackoverflow.jni.opengl.NativeClazz
 */
public class NativeClazz {

/**
 * Load C++ Library
 */
static {
    // Always fun to do this in a static block!
        System.loadLibrary("OpenGL");
}

private static native void GetNativeBuffer(IntBuffer Buffer);
}

and

package com.stackoverflow.jni.directx;

/*
 * GENERATE HEADER FILE FROM GENERATED CLASS AS NEEDED VIA
 *   javah com.stackoverflow.jni.directx.NativeClazz
 */
public class NativeClazz {

/**
 * Load C++ Library
 */
static {
    // Always fun to do this in a static block!
        System.loadLibrary("DirectX");
}

private static native void GetNativeBuffer(IntBuffer Buffer);
}

My personal preference is to keep any class containing JNI methods "utility-only" (private constructor), leaving them lean and mean (no internal variables unless necessary) and transfer data back and forth within beans via function call parameters.

Upvotes: 2

Related Questions