我现在有个打印机的DLL,里面的方法和返回值都知道,但我通过JNI调用的时候,返回一直不对,求大家帮帮忙哦
代码如下
Java代码:public class Test { static{
System.loadLibrary("TestDll");
}
//切纸
private static native int cutPaper(int model,int distance);
//将值发送到缓冲区
private static native int sendTextOut(String text,int width,int hight,int widthTimes
,int heightTimes,int type,int style);
//将缓冲区数据打印
private static native int textPrint();
//向前走纸
private static native int feelLine();
//打开连接
private static native int open(String ip,int nComBaudrate,int nComDataBits, int nComStopBits,
int nComParity, int nParam );
//关闭连接
private static native int close();
private static native int queryStatus(String ip,int stauts);
生成的头文件代码/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Test */#ifndef _Included_Test
#define _Included_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Test
* Method: cutPaper
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_Test_cutPaper
(JNIEnv *, jclass, jint, jint);/*
* Class: Test
* Method: sendTextOut
* Signature: (Ljava/lang/String;IIIIII)I
*/
JNIEXPORT jint JNICALL Java_Test_sendTextOut
(JNIEnv *, jclass, jstring, jint, jint, jint, jint, jint, jint);/*
* Class: Test
* Method: textPrint
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Test_textPrint
(JNIEnv *, jclass);/*
* Class: Test
* Method: feelLine
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Test_feelLine
(JNIEnv *, jclass);/*
* Class: Test
* Method: open
* Signature: (Ljava/lang/String;IIIII)I
*/
JNIEXPORT jint JNICALL Java_Test_open
(JNIEnv *, jclass, jstring, jint, jint, jint, jint, jint);/*
* Class: Test
* Method: close
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Test_close
(JNIEnv *, jclass);/*
* Class: Test
* Method: queryStatus
* Signature: (Ljava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_Test_queryStatus
(JNIEnv *, jclass, jstring, jint);#ifdef __cplusplus
}
#endif
#endif
我的实现#include <jni.h>
#include <jni_md.h>
#include "stdafx.h"
#include "Test.h"BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
} HMODULE hModule = LoadLibrary("POSDLL.dll"); HANDLE hand;
/*
*
*切纸
*
*/
JNIEXPORT jint JNICALL Java_Test_cutPaper
(JNIEnv *env, jclass obj, jint model, jint distance){typedef int (__stdcall *POS_CutPaper)(int nMode,int nDistance); POS_CutPaper cut=(POS_CutPaper)GetProcAddress(hModule,"POS_CutPaper"); return cut(model,distance);}/*
* Class: Test
* Method: sendTextOut
* Signature: (Ljava/lang/String;IIIIII)I
* 发送数据到缓冲区
*/
JNIEXPORT jint JNICALL Java_Test_sendTextOut
(JNIEnv *env, jclass obj, jstring str, jint x, jint y, jint w, jint h, jint type, jint style){typedef int (__stdcall *POS_PL_TextOut)( char *pszString,int nOrgx,int nOrgy,int nWidthTimes,int nHeightTimes,
int nFontType,
int nFontStyle); POS_PL_TextOut plTextOut=(POS_PL_TextOut)GetProcAddress(hModule,"POS_PL_TextOut");
// char* text = (char*)(env)->GetStringUTFChars(env, str); return plTextOut((LPSTR)(LPCTSTR)str,x,y,w,h,type,style);}/*
* Class: Test
* Method: textPrint
* Signature: ()I
* 打印
*/
JNIEXPORT jint JNICALL Java_Test_textPrint
(JNIEnv *, jclass){
typedef int (__stdcall *POS_PL_Print)(void); POS_PL_Print print=(POS_PL_Print)GetProcAddress(hModule,"POS_PL_Print"); return print();
}/*
* Class: Test
* Method: feelLine
* Signature: ()I
* 向前走纸
*/
JNIEXPORT jint JNICALL Java_Test_feelLine
(JNIEnv *, jclass){ typedef int (__stdcall *POS_FeedLine)(void); POS_FeedLine feedline=(POS_FeedLine)GetProcAddress(hModule,"POS_FeedLine"); return feedline();}JNIEXPORT jint JNICALL Java_Test_open
(JNIEnv *env, jclass, jstring ip, jint m, jint n, jint x, jint y, jint type){ typedef HANDLE (__stdcall *POS_Open)( LPCTSTR lpName,int nComBaudrate,
int nComDataBits, int nComStopBits,
int nComParity, int nParam );
POS_Open open=(POS_Open)GetProcAddress(hModule,"POS_Open"); if(open == NULL){
return 1111;
}
// char* ipAdress = (char*)(env)->GetStringUTFChars(env, ip);
hand = open((LPCTSTR)ip,m,n,x,y,type);
if(hand == NULL){
return 0;
}
return 1;
}/*
* Class: Test
* Method: close
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Test_close
(JNIEnv *, jclass){
typedef int (__stdcall *POS_Close)(void); POS_Close close=(POS_Close)GetProcAddress(hModule,"POS_Close"); return close();
}JNIEXPORT jint JNICALL Java_Test_queryStatus(JNIEnv *, jclass, jstring ip, jint status){
typedef int (__stdcall *POS_NETQueryStatus)(char *ipAddress,char *pszStatus); POS_NETQueryStatus query=(POS_NETQueryStatus)GetProcAddress(hModule,"POS_NETQueryStatus"); char * ipAdress=(LPSTR)(LPCTSTR)ip; char Status = 0; return query(ipAdress,&Status);}
那个POSDLL就是打印机提供的申明下,我对C++不熟悉,所以希望大家能详细点。
代码如下
Java代码:public class Test { static{
System.loadLibrary("TestDll");
}
//切纸
private static native int cutPaper(int model,int distance);
//将值发送到缓冲区
private static native int sendTextOut(String text,int width,int hight,int widthTimes
,int heightTimes,int type,int style);
//将缓冲区数据打印
private static native int textPrint();
//向前走纸
private static native int feelLine();
//打开连接
private static native int open(String ip,int nComBaudrate,int nComDataBits, int nComStopBits,
int nComParity, int nParam );
//关闭连接
private static native int close();
private static native int queryStatus(String ip,int stauts);
生成的头文件代码/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Test */#ifndef _Included_Test
#define _Included_Test
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Test
* Method: cutPaper
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_Test_cutPaper
(JNIEnv *, jclass, jint, jint);/*
* Class: Test
* Method: sendTextOut
* Signature: (Ljava/lang/String;IIIIII)I
*/
JNIEXPORT jint JNICALL Java_Test_sendTextOut
(JNIEnv *, jclass, jstring, jint, jint, jint, jint, jint, jint);/*
* Class: Test
* Method: textPrint
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Test_textPrint
(JNIEnv *, jclass);/*
* Class: Test
* Method: feelLine
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Test_feelLine
(JNIEnv *, jclass);/*
* Class: Test
* Method: open
* Signature: (Ljava/lang/String;IIIII)I
*/
JNIEXPORT jint JNICALL Java_Test_open
(JNIEnv *, jclass, jstring, jint, jint, jint, jint, jint);/*
* Class: Test
* Method: close
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Test_close
(JNIEnv *, jclass);/*
* Class: Test
* Method: queryStatus
* Signature: (Ljava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_Test_queryStatus
(JNIEnv *, jclass, jstring, jint);#ifdef __cplusplus
}
#endif
#endif
我的实现#include <jni.h>
#include <jni_md.h>
#include "stdafx.h"
#include "Test.h"BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
} HMODULE hModule = LoadLibrary("POSDLL.dll"); HANDLE hand;
/*
*
*切纸
*
*/
JNIEXPORT jint JNICALL Java_Test_cutPaper
(JNIEnv *env, jclass obj, jint model, jint distance){typedef int (__stdcall *POS_CutPaper)(int nMode,int nDistance); POS_CutPaper cut=(POS_CutPaper)GetProcAddress(hModule,"POS_CutPaper"); return cut(model,distance);}/*
* Class: Test
* Method: sendTextOut
* Signature: (Ljava/lang/String;IIIIII)I
* 发送数据到缓冲区
*/
JNIEXPORT jint JNICALL Java_Test_sendTextOut
(JNIEnv *env, jclass obj, jstring str, jint x, jint y, jint w, jint h, jint type, jint style){typedef int (__stdcall *POS_PL_TextOut)( char *pszString,int nOrgx,int nOrgy,int nWidthTimes,int nHeightTimes,
int nFontType,
int nFontStyle); POS_PL_TextOut plTextOut=(POS_PL_TextOut)GetProcAddress(hModule,"POS_PL_TextOut");
// char* text = (char*)(env)->GetStringUTFChars(env, str); return plTextOut((LPSTR)(LPCTSTR)str,x,y,w,h,type,style);}/*
* Class: Test
* Method: textPrint
* Signature: ()I
* 打印
*/
JNIEXPORT jint JNICALL Java_Test_textPrint
(JNIEnv *, jclass){
typedef int (__stdcall *POS_PL_Print)(void); POS_PL_Print print=(POS_PL_Print)GetProcAddress(hModule,"POS_PL_Print"); return print();
}/*
* Class: Test
* Method: feelLine
* Signature: ()I
* 向前走纸
*/
JNIEXPORT jint JNICALL Java_Test_feelLine
(JNIEnv *, jclass){ typedef int (__stdcall *POS_FeedLine)(void); POS_FeedLine feedline=(POS_FeedLine)GetProcAddress(hModule,"POS_FeedLine"); return feedline();}JNIEXPORT jint JNICALL Java_Test_open
(JNIEnv *env, jclass, jstring ip, jint m, jint n, jint x, jint y, jint type){ typedef HANDLE (__stdcall *POS_Open)( LPCTSTR lpName,int nComBaudrate,
int nComDataBits, int nComStopBits,
int nComParity, int nParam );
POS_Open open=(POS_Open)GetProcAddress(hModule,"POS_Open"); if(open == NULL){
return 1111;
}
// char* ipAdress = (char*)(env)->GetStringUTFChars(env, ip);
hand = open((LPCTSTR)ip,m,n,x,y,type);
if(hand == NULL){
return 0;
}
return 1;
}/*
* Class: Test
* Method: close
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Test_close
(JNIEnv *, jclass){
typedef int (__stdcall *POS_Close)(void); POS_Close close=(POS_Close)GetProcAddress(hModule,"POS_Close"); return close();
}JNIEXPORT jint JNICALL Java_Test_queryStatus(JNIEnv *, jclass, jstring ip, jint status){
typedef int (__stdcall *POS_NETQueryStatus)(char *ipAddress,char *pszStatus); POS_NETQueryStatus query=(POS_NETQueryStatus)GetProcAddress(hModule,"POS_NETQueryStatus"); char * ipAdress=(LPSTR)(LPCTSTR)ip; char Status = 0; return query(ipAdress,&Status);}
那个POSDLL就是打印机提供的申明下,我对C++不熟悉,所以希望大家能详细点。
open方法返回值是1,那是不是说明返回的hand是有效的撒。。
不好意思,整个过程应该是先open打开连接,然后向缓冲区发数据,然后打印,之后关闭,但现在
open返回的hand不是空的,可发送数据,打印等方法返回都是函数执行失败的标记,打印机只提供了DLL,无头文件和源文件,无奈啊~~郁闷的
http://topic.csdn.net/u/20090403/10/8cad65f2-9e21-4eac-92df-caa42b0219f7.html
http://sech.javaeye.com/blog/164183
http://www.itpub.net/106333.html