0

I have a dll (with source) LibUVCCam.dll that I want to implement using Java JNA. I've followed the msvcrt tutorial and it works.

Using dependency walker, I dumped the names of the functions and get things like ?listDevices@UVCCameraLibrary@@SAXPEAPEADAEAH@Z and I want to implement listDevices(). How do I define that in:

public interface JNALoader extends Library
{
  JNALoader INSTANCE = (JNALoader) Native.loadLibrary(("LibUVCCam"), JNALoader.class);
  ???? listDevices() // ???? = I don't know what to do here.
}

Here is the function from the source of the library:

/*
* static function
* List connected devices
* @cameraNames : (out) name list of connected cameras
* @nDevices : (out) the number of connected cameras
*/
void UVCCameraLibrary::listDevices(char **cameraNames , int &nDevices)
{
...
}

The full source for the project is https://github.com/sky19938470/uvcamera-windows-project/tree/master/LibUVCCam.

John Smith
  • 3,493
  • 3
  • 25
  • 52
  • The library source is C++, not Java. Also that "????" looks suspiciously like you have some problem with a different character encoding somewhere. Does the library have a separate API you can use? That might be easier. – rossum Jul 03 '20 at 16:42
  • I did just answer this question on the JNA Mailing list with some details. First, the syntax (`UVCCameraLibrary::`) is C++. You can't use that in JNA. Look in the header files for `extern "C"` and a function prototype defined there. Second, we need more info from the API on what is expected for the `cameraNames` variable. The function shows it's a pointer to a memory location, but gives no information on whether you or the DLL is responsible for allocating and freeing the memory for the names. – Daniel Widdis Jul 03 '20 at 18:43
  • @DanielWiddis - I did see your reply and did reply and there is no extern "C" in the header files, but I needed to go further on the structure for extern "C" as I'm not a C++ programmer. The link to the code I'm trying to implement is https://github.com/sky19938470/uvcamera-windows-project/tree/master/LibUVCCam. I'm trying to use the dll from this project. If I had an example wrapper for this dll for listDevices, I think I could figure out the rest of the calls I want to use. – John Smith Jul 05 '20 at 16:06
  • @rossum - the ???? is just a place holder for "I don't know" I modified my OP. – John Smith Jul 05 '20 at 16:07
  • 1
    JNA doesn't seem to provide a way to call C++ code from Java. You'll need to write a C wrapper. Link to old post: https://stackoverflow.com/questions/2241685/java-native-access-doesnt-do-c-right -- I'm not sure if this is still true. – user13784117 Jul 05 '20 at 16:22
  • @user13784117 is correct. Most cross-platform code including `libffi` that JNA leverages, uses the C API. You have to write (and compile) a C++ program, using `extern "C"` to export your own functions to make them available for JNA, and calling the native code from inside your wrapper. No other portable options. – Daniel Widdis Jul 05 '20 at 20:07

0 Answers0