简体   繁体   中英

JNI:how to call c++ function from Java Native Method

i am working on some java application in which i am using jni. Well here now i want to call pure c++ function from JNI method . well i am doing this as shown as below code. here dll is created but when i am trying run i get the error java.lang.UnsatisfiedLinkError: Can't find dependent libraries . i am creating my dll in visual studio. so tell me what i am doing wrong in calling c++ function.

here is code of my .cpp file

#include "SensorDetect.h"
#include <stdio.h>
#include <windows.h>
// Include specific Tools header
#include "Tools.h"
   // Include IO_XRayUSB_MD_VC80 header
#include "IO_XRayUSB.h"

// Include IO_XRayUSB_MD_VC80 library
#pragma message ("Using : IO_XRayUSB_MD_VC80.lib")
#pragma comment(lib, "IO_XRayUSB_MD_VC80.lib")

//// Custom user Callback function which is called by IO_XrayUsb when a device is plugged or unplugged
void _stdcall DevicePlugUnplugCallback(XRay_CALLBACK * pCallBackInfo, int nCallBackCount)
{
    if (pCallBackInfo && (nCallBackCount > 0))
    {
        for (int nDeviceIndex = 0; nDeviceIndex < nCallBackCount; nDeviceIndex ++)
        {
            switch(pCallBackInfo[nDeviceIndex].nState)
            {
                case XRAY_USB_PLUGGED_DEVICE :
                    printf("\n=>Callback Device: %s has been Plugged...\n\n", pCallBackInfo[nDeviceIndex].pcDeviceName);
                    break;
                case XRAY_USB_UNPLUGGED_DEVICE :
                    printf("\n=>Callback Device: %s has been Unplugged...\n\n", pCallBackInfo[nDeviceIndex].pcDeviceName);
                    break;
                default:
                    break;
            }
        }
    }
}


extern "C"
JNIEXPORT void JNICALL Java_sensordetect_SensorDetect_getDevice(JNIEnv * env, jclass cl)
{

     const int nNbMaxDevices = 10;
    char pDeviceList[nNbMaxDevices][260];
    int nDeviceCount = 10;
    XRay_HANDLE hDevice;
    XRay_SENSOR_INFO SensorInfo;


    //int nTriggerMode = GetChoice(1, "Choose Trigger Mode:\t0:XVIS_detection    1:TWI_request_detection");
    char pcBuffer[100];
    int nKey;

        nKey=0;

            int nTriggerMode=nKey;

    try
    {
        // XRay_RegisterCallBackPlugDevice to be notified of device plug/unplug
**//This function call creates problem in loading dll. error:Can't find dependent libraries**
     BOOL bSuccess = XRay_RegisterCallBackPlugDevice(DevicePlugUnplugCallback);    


        //for (int nIndex = 0; nIndex < 1; nIndex ++)
            printf("\tFound device : %s\n", pDeviceList[0]);

    }
    catch (...) // catch own CMyException
        {
            //e.ShowReason();
        }

    }

Either IO_XRayUSB_MD_VC80.dll or one of is dependents is not in one of the directories listed in the PATH environment variable, or does not exist in the same directory where your application is running.

To get a list of DLLs on which a DLL depends you can use dumpbin.exe :

dumpbin.exe /DEPENDENTS C:\\path\\to\\IO_XRayUSB_MD_VC80.dll

确保在启动程序时指定-Djava.library.path=C:\\path\\to\\DLLs选项,和/或在Windows PATH包含DLL的目录。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM