Brandon
Brandon

Reputation: 23485

Pass Parameters to DLL's Main Java

How can I pass Parameters to Main via JNI?

Currently I load my DLL like:

class SharedLibrary {

    native void GetBuffer(ByteBuffer Buffer);;

    SharedLibrary(String[] exec_args) {
        String path = new File(exec_args[0]).getAbsolutePath();
        System.load(path);  //Load My DLL. I want to Pass this DLL some arguments.

        ByteBuffer Foo = ByteBuffer.allocateDirect(.....);
        GetBuffer(Foo);
    }
}

How can I pass the DLL arguments? I need to pass multiple arguments.

Upvotes: 0

Views: 1542

Answers (2)

Pavel Zdenek
Pavel Zdenek

Reputation: 7293

Well, if you need "multiple parameters", any existing "dll main" won't work for you. You are most probably referring to WinAPI DllMain and you probably think that this function is mandatory to any DLL, much the same way as every C executable is expected to have main() function. It is not. JNI in particular has JNI_OnLoad which doesn't take any parameters, but so DllMain doesn't have any user-definable "multiple parameters" per your requirement. If you need your own parameters, why can't you create an initialization method? Even the DllMain doc is recommending that. DllMain is very limited in what it can do. Make the JNI init method static, so that you can call it before instantiating the SharedLibrary object in Java. What's the problem with it? Tell something about the "multiple parameters" you need so much.

Upvotes: 1

mah
mah

Reputation: 39807

The purpose of loading a library into Java is to fulfill Java methods which are declared with the native attribute, such as native void methodname(_arguments go here_);. You can declare one or more native methods in a class, but all of them are expected to be defined (using JNI standards) in your DLL. From Java, you call them like any other method (by using whatever arguments are defined for the method).

If there are data elements you want the DLL's initialization entry point to receive, you need to make them static members (or methods) of some class and the DLL needs to know to access that class to get them. This, however, would be quite abnormal and is probably not the best way to perform whatever it is you're looking to do.

Upvotes: 2

Related Questions