JNAerator - using interfaces generated by typedef

1.5k Views Asked by At

I'm trying to use a generated interface by JNAerator from a typedef instruction, but I can't find a way to do that:

The function in the .h file is:

MyClass::Initialize(LPCWSTR path);

The header file also includes the original typedef instruction:

typedef __nullterminated CONST WCHAR *LPCWSTR, *PCWSTR;

JNAerator generates:

Method:

public native int Initialize(LPCWSTR path);

The interface:

/// Undefined type
/// Undefined type
public static interface LPCWSTR {

};

And the classes:

/// C type : WCHAR*
    public Pointer<Short > LPCWSTR() {
        try {
            return (Pointer<Short >)BridJ.getNativeLibrary("dlls").getSymbolPointer("LPCWSTR").as(DefaultParameterizedType.paramType(Pointer.class, Short.class)).get();
        }catch (Throwable $ex$) {
            throw new RuntimeException($ex$);
        }
    }
    /// C type : WCHAR*
    public MainLibrary LPCWSTR(Pointer<Short > LPCWSTR) {
        try {
            {
                BridJ.getNativeLibrary("dlls").getSymbolPointer("LPCWSTR").as(DefaultParameterizedType.paramType(Pointer.class, Short.class)).set(LPCWSTR);
                return this;
            }
        }catch (Throwable $ex$) {
            throw new RuntimeException($ex$);
        }
    }

The problem is, I don't know how to instantiate an object LPCWSTR using Pointer and the LPCWSTR interface, using a String, so that I can pass it to the Initialize method.

How can I do this?

UPDATE:

I modified the .h file to use wchar_t*:

MyClass::Initialize(wchar_t* path)

JNAerator generated the method like this:

public native int Initialize(Pointer<Character > path);

So I called it like this:

MyClass factory = new MyClass();
Pointer<Character> path = org.bridj.Pointer.pointerToWideCString("dlls");
factory.Initialize(path);

The problem is that I get the following exception:

java.lang.UnsatisfiedLinkError: main.MyClass.Initialize(Lorg/bridj/Pointer;)I

at this line:

factory.Initialize(path);

What am I doing wrong?

2

There are 2 best solutions below

0
On

I was having this problem too. zOlive’s response sent me down the right path. I changed the JNAerator library option to match my DLL name (e.g. -library MyLib MyLib.dll MyLib.h). That fixed it for me. There is more info about the library option here.

1
On

The UnsatisfiedLinkError indicates that your native Initialize method was not bound by BridJ.

This binding is done by CPPObject's constructor (I assume you got a JNAerated MyClass class that inherits from it), and any failure to bind should result in error logs in your console (in any case, you can increase the logs verbosity with BRIDJ_VERBOSE=1 environment variable).

Also, please note that your MyClass should have a @Library("mylibrary") annotation to let it know where to find the appropriate library (.dll, .so or .dylib), or should be an inner class of a class with such an annotation. This is normally the case of JNAerated classes, but if it's not, please report a bug in NativeLibs4Java's tracker.