JNI调用C++函数, 该函数编译错误, 求解!

delayxl 2013-02-27 01:43:40
=============================
DataCardDll.CPP代码如下
=============================

#include "CallDll.h"
#include "stdio.h"
#include "windows.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef int (*Type_SetPara)(int, int, int*, int,
unsigned short, unsigned short, int, int,
int);

typedef int (*Type_DataIn)(float *, float *, unsigned char *,
int, int);



JNIEXPORT jint JNICALL Java_CallDll_callInfoCollect
(JNIEnv *env, jobject _obj, jint _sampleRate, jint _adChannelNo, jintArray _adpgaChannels,
jint _dioModal, jchar _pwm1, jchar _pwm2, jint _comparatorEnable, jint _extTriggor,
jfloatArray _voltageIn1, jfloatArray _voltageIn2, jbooleanArray _d1,
jint _sampleNo, jint _deviceNo)
{
HINSTANCE hDll = LoadLibrary("RealDataCardDll.dll");
if(NULL == hDll)
{
printf("Cann't find real Dll");
}

Type_SetPara MPS_SetPara = (Type_SetPara)GetProcAddress(hDll, "SetPara");
if(NULL == MPS_SetPara)
{
printf("Cann't find <SetPara> function");
}

Type_DataIn MPS_DataIn = (Type_DataIn)GetProcAddress(hDll, "DataIn");
if(NULL == MPS_DataIn)
{
printf("Cann't find <DataIn> function");
}




int SampleRate = _sampleRate;
int ADChannelNo = _adChannelNo;

int* ADPGAChannels = env->GetIntArrayElements(_adpgaChannels, NULL);
int DIOModal = _dioModal;
unsigned short PWM1 = _pwm1;
unsigned short PWM2 = _pwm2;
int ComparatorEnable = _comparatorEnable;
int ExtTriggor = _extTriggor;
float* VoltageIn1 = env->GetFloatArrayElements(_voltageIn1, NULL);
float* VoltageIn2 = env->GetFloatArrayElements(_voltageIn2, NULL);
unsigned char* D1 = env->GetBooleanArrayElements(_d1, NULL);
int SampleNo = _sampleNo;
int DeviceNo = _deviceNo;

int result = 0;
int failAmount = 0;

result = MPS_SetPara(SampleRate, ADChannelNo, ADPGAChannels, DIOModal, PWM1, PWM2,
ComparatorEnable, ExtTriggor, DeviceNo);

....

}

=============================
CallDll.h 代码如下
=============================
#include <jni.h>
#ifndef _Included_CallDll
#define _Included_CallDll
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jint JNICALL Java_CallDll_callInfoCollect
(JNIEnv *, jobject, jint, jint, jintArray, jint, jchar, jchar, jint, jint, jfloatArray, jfloatArray, jbooleanArray, jint, jint);

#ifdef __cplusplus
}
#endif
#endif


=============================
按在VC++ 6.0 下按F7进行编译出错, 该句出错:
int* ADPGAChannels = env->GetIntArrayElements(_adpgaChannels, NULL);

编译窗口显示如下错误:
-------------------Configuration: DataCardDll - Win32 Debug--------------------
Compiling...
DataCardDll.cpp
D:\jcode\CPP\DataCardDll\DataCardDll.cpp(48) : error C2440: 'initializing' : cannot convert from 'long *' to 'int *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

DataCardDll.dll - 1 error(s), 0 warning(s)
=============================


小弟搞了两天了也没有头绪, 还请达人指教! 万分感谢!
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
delayxl 2013-02-28
  • 打赏
  • 举报
回复
感谢楼上的两位大侠. 我昨晚又敲了一遍代码, 终于发现了原因. 是两个比较低级的错误. 错误1, cpp中忘记敲最后的这部分了: #ifdef __cplusplus } #endif 错误2, 改完错误1之后, 再把出错那句强制类型转换成(int *)就可以了. int *ADPGAChannels = (int *)env->GetIntArrayElements(_adpgaChannels, NULL); 之前也曾试过强制类型转换, 但是由于错误1的存在, 编译提示最后的大括号那里语法错误, 我没有经验, 又把强制类型转换删除,恢复了原状. 但是当我把出错的那句注释掉后再编译居然能编译通过, 所以就认定是env->GetIntArrayElements这句的错误, 没有再考虑最后的大括号那里. 我还曾一度怀疑是遇到JNI的bug了. 现在看来真是幼稚呀. 哎, 本来上学时C++就没学好, 到现在又有大概十年未摸, 突然小用一下, 结果就贻笑大方了.呵呵. 无论如何再次感谢tcjiaan 和dracularking 两位大侠的出手相助!
dracularking 2013-02-27
  • 打赏
  • 举报
回复
env->GetIntArrayElements(_adpgaChannels, NULL); 虽然说是get IntArray,不过还是查看一下返回究竟什么类型
东邪独孤 2013-02-27
  • 打赏
  • 举报
回复
env->GetIntArrayElements返回的是不是long*?如果是,long*类型怎么能赋给int*的变量?
  • 打赏
  • 举报
回复
'initializing' : cannot convert from 'long *' to 'int *'说的很明白了吧,initializing不能由long型指针转换为int型指针。看看类型是否弄错了
delayxl 2013-02-27
  • 打赏
  • 举报
回复
引用 1 楼 fangmingshijie 的回复:
'initializing' : cannot convert from 'long *' to 'int *'说的很明白了吧,initializing不能由long型指针转换为int型指针。看看类型是否弄错了
------------------------------------------------------------ 查了类型没弄错. 头文件中该参数是 jintArray类型的, CPP中使用int* ADPGAChannels, 应该没错.

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧