我正在开发一些使用 jni 的 java 应用程序。那么现在我想从 JNI 方法调用纯 C++ 函数。好吧,我正在这样做,如下面的代码所示。这里创建了 dll,但是当我尝试运行时,我收到错误 java.lang.UnsatisfiedLinkError: Can't finddependent library。我正在 Visual Studio 中创建我的 dll。所以告诉我我在调用 c++ 函数时做错了什么。
这是我的 .cpp 文件的代码
#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();
}
}