Java调用dll 闪退,java 调用DLL 读串口,不久就死了,请帮忙看看什么原因?

请 登录 后使用快捷导航

没有帐号?注册

阅 1458|回 1

最后登录2021-1-24

在线时间1 小时

威望0分

芯积分E金币好友

Java调用dll 闪退,java 调用DLL 读串口,不久就死了,请帮忙看看什么原因?_第1张图片

发表于2009-6-1 14:20

java 调用DLL 读串口,不久就死了,请帮忙看看什么原因?

[复制链接]

经过仔细分析问题出这dll这里,但始终找不到解决办法,请大家帮忙看看,谢谢!

dll代码如下:

JNIEXPORT jstring JNICALL Java_getGPS

(JNIEnv * env, jobject obj)

{

char * cIpAddress =compressWideChar((LPCWSTR)L"empty value!");

int baud     = 9600;//BaudTbl[1];

int parity   = 0;//ParityTbl[1];

int databit  = 8;//DataBitTbl[1];

int stopbit  = 0;//StopBitTbl[1];

//打开串口

if(m_hComm==INVALID_HANDLE_VALUE)

{

m_hComm=CreateFile(TEXT("COM3:"),GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,0,0);

if(m_hComm==INVALID_HANDLE_VALUE )

{

cIpAddress =compressWideChar((LPCWSTR)L"COM3 open error!");

ClosePort();

return WindowsTojstring(env, cIpAddress);

}

GetCommState(m_hComm,&dcb);

dcb.BaudRate=baud;//BaudRate;

dcb.ByteSize=databit;//DataBits;

dcb.Parity=parity;//Parity;

dcb.StopBits=stopbit;//StopBits;

dcb.fParity=FALSE;

dcb.fBinary=TRUE;

dcb.fDtrControl=0;

dcb.fRtsControl=0;

dcb.fOutX=dcb.fInX=dcb.fTXContinueOnXoff=0;

//设置状态参数

SetCommMask(m_hComm,EV_RXCHAR);

SetupComm(m_hComm,16384,16384); //old

//SetupComm(m_hComm,4096,4096); //new my add

if(!SetCommState(m_hComm,&dcb))

{

//AfxMessageBox(_T("无法按当前参数配置端口,请检查参数!"));

cIpAddress =compressWideChar((LPCWSTR)L"setup com3 error!");

PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);

ClosePort();

// bOpen = FALSE;

return WindowsTojstring(env, cIpAddress);

}

//设置超时参数

GetCommTimeouts(m_hComm,&CommTimeOuts);

CommTimeOuts.ReadIntervalTimeout=100;//old

CommTimeOuts.ReadTotalTimeoutMultiplier=1;//old

CommTimeOuts.ReadTotalTimeoutConstant=100;//old

CommTimeOuts.WriteTotalTimeoutMultiplier=0;

CommTimeOuts.WriteTotalTimeoutConstant=0;

if(!SetCommTimeouts(m_hComm,&CommTimeOuts))

{

cIpAddress =compressWideChar((LPCWSTR)L"setup Timeout error!");

PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);

ClosePort();

return WindowsTojstring(env, cIpAddress);

}

PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);

}

//读数据

//下边的代码转化中有出问题

while(m_hComm!=INVALID_HANDLE_VALUE)

{

BOOL fReadState;

DWORD dwLength;//,dwBytesRead;

char* buf=new char[256];//256

int length=256;//256

fReadState=ReadFile(m_hComm,buf,length,&dwLength,NULL);//从串口读数

if(fReadState)

{

cIpAddress = NULL;

cIpAddress = buf;

}

jstring str = WindowsTojstring(env, cIpAddress);//我估计问题出在这里 WindowsTojstring()是用于UNICODE转ANSI    delete []buf;

delete buf;

delete cIpAddress;

// ClosePort();

return str;

}

else

{

jstring str = WindowsTojstring(env, cIpAddress);

delete []buf;

delete buf;

delete cIpAddress;

// ClosePort();

return str;

}

}

ClosePort();

return WindowsTojstring(env, cIpAddress);

}

//UNICODE转ANSI

jstring WindowsTojstring( JNIEnv* env, char* str )

{

jstring rtn = 0;

int slen = strlen(str);

wchar_t* buffer = 0;

if( slen == 0 )

rtn = env->NewStringUTF( str ); //UTF ok since empty string

else

{

int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );//old

buffer = (wchar_t*)malloc( length*2 + 1 );//old

if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )

rtn = env->NewString( (jchar*)buffer, length );

}

if( buffer )

{

free( buffer );

delete buffer;

}

return rtn;

}

你可能感兴趣的:(Java调用dll,闪退)