JNative dll回调函数使用实例

JNative(http://sourceforge.net/projects/jnative/)是供java直接调用c++dll的工具包,这样java程序员仅仅知道c++dll中的函数名和参数类型就可以直接调用dll 了,省去了使用JNI的繁琐。

下面是一个使用c++dll回调函数的例子:

dll名字JNativeTest.dll

  C++代码:

  JNativeTest.h:

 

Java代码 复制代码
  1. #ifndef JNativeTes_H   
  2. #define JNativeTes_H   
  3.   
  4. /** The following ifdef block is the standard way of creating macros which make exporting  
  5. * from a DLL simpler. All files within this DLL are compiled with the JNATIVETEST_EXPORTS  
  6. * symbol defined on the command line. this symbol should not be defined on any project  
  7. * that uses this DLL. This way any other project whose source files include this file see  
  8. * JNATIVETEST_API functions as being imported from a DLL, wheras this DLL sees symbols  
  9. * defined with this macro as being exported.  
  10. **/  
  11. #ifdef JNATIVETEST_EXPORTS   
  12. #define JNATIVETEST_API extern "C" __declspec(dllexport)   
  13. #else  
  14. #define JNATIVETEST_API __declspec(dllimport)   
  15. #endif   
  16.   
  17. typedef void (*pfCallbackTest)(char * msg, char* data, int type); /*回调函数声明*/  
  18.   
  19. // This class is exported from the JNativeTest.dll   
  20.   
  21. JNATIVETEST_API char* fnJNativeTest(char* msg);   
  22.   
  23. JNATIVETEST_API void RegCallback(pfCallbackTest ev);   
  24.   
  25. #endif  
#ifndef JNativeTes_H
#define JNativeTes_H

/** The following ifdef block is the standard way of creating macros which make exporting
* from a DLL simpler. All files within this DLL are compiled with the JNATIVETEST_EXPORTS
* symbol defined on the command line. this symbol should not be defined on any project
* that uses this DLL. This way any other project whose source files include this file see
* JNATIVETEST_API functions as being imported from a DLL, wheras this DLL sees symbols
* defined with this macro as being exported.
**/
#ifdef JNATIVETEST_EXPORTS
#define JNATIVETEST_API extern "C" __declspec(dllexport)
#else
#define JNATIVETEST_API __declspec(dllimport)
#endif

typedef void (*pfCallbackTest)(char * msg, char* data, int type); /*回调函数声明*/

// This class is exported from the JNativeTest.dll

JNATIVETEST_API char* fnJNativeTest(char* msg);

JNATIVETEST_API void RegCallback(pfCallbackTest ev);

#endif
 

  JNativeTest.cpp

 

Java代码 复制代码
  1. // JNativeTest.cpp : Defines the entry point for the DLL application.   
  2.   
  3. #include <windows.h>   
  4. #include "JNativeTest.h"  
  5.   
  6. BOOL APIENTRY DllMain( HANDLE hModule,   
  7.                        DWORD ul_reason_for_call,   
  8.                        LPVOID lpReserved   
  9.       )   
  10. {   
  11.     switch (ul_reason_for_call)   
  12. {   
  13.     case DLL_PROCESS_ATTACH:   
  14.     case DLL_THREAD_ATTACH:   
  15.     case DLL_THREAD_DETACH:   
  16.     case DLL_PROCESS_DETACH:   
  17.     break;   
  18. }   
  19.     return TRUE;   
  20. }   
  21.   
  22. pfCallbackTest fCallbackTest = NULL;   
  23. /*注册回调*/  
  24. void RegCallback(pfCallbackTest ev)   
  25. {   
  26.      fCallbackTest = ev;   
  27. }   
  28.   
  29. // This is an example of an exported function.   
  30. char* fnJNativeTest(char* msg)   
  31. {   
  32. if (fCallbackTest != NULL)   
  33.    fCallbackTest(msg, "Test"6); /*调用回调函数*/  
  34.    return msg;   
  35. }  
// JNativeTest.cpp : Defines the entry point for the DLL application.

#include <windows.h>
#include "JNativeTest.h"

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    switch (ul_reason_for_call)
{
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
    break;
}
    return TRUE;
}

pfCallbackTest fCallbackTest = NULL;
/*注册回调*/
void RegCallback(pfCallbackTest ev)
{
     fCallbackTest = ev;
}

// This is an example of an exported function.
char* fnJNativeTest(char* msg)
{
if (fCallbackTest != NULL)
   fCallbackTest(msg, "Test", 6); /*调用回调函数*/
   return msg;
}
 

  java代码:

  TestCallback.java

Java代码 复制代码
  1. import org.xvolks.jnative.JNative;   
  2. import org.xvolks.jnative.exceptions.NativeException;   
  3. import org.xvolks.jnative.pointers.*;   
  4. import org.xvolks.jnative.util.Callback;   
  5.   
  6. /**  
  7. * @date 2009-3-17  
  8. * @author charis  
  9. * @Copyright (c) qqtech All rights reserved. http://www.qqtech.com  
  10. */  
  11. public class TestCallback implements Callback/*实现此接口*/ {   
  12.   
  13. public TestCallback() {   
  14.      
  15. }   
  16. /**  
  17. * Method callback  
  18. * java对应于dll中的回调函数  
  19. * @param values an long[] 参数数组,非整型数代表的都是地址  
  20. * @return an int  
  21. * @version 3/17/2008  
  22. */  
  23. public int callback(long[] values) {   
  24.    if (values == null) {   
  25.     System.err.println("callback ret " + 3);   
  26.     return 3;   
  27.    }   
  28.    if (values.length == 3) {   
  29.     String name = "";   
  30.     try {   
  31.      Pointer _pointer = Pointer.createPointerToNativeMemory((int) values[0], 100); /*获取字符串通过指针*/  
  32.      Pointer _pointer1 = Pointer.createPointerToNativeMemory((int) values[1], 100);   
  33.        
  34.      name = _pointer.getAsString() + "/" + _pointer1.getAsString();   
  35.        
  36.     } catch (Exception e1) {   
  37.      // TODO Auto-generated catch block   
  38.      e1.printStackTrace();   
  39.     }   
  40.     System.err.println("lParam " + name + "/" + values[2]);   
  41.       
  42.       
  43.     return 1;   
  44.    } else {   
  45.     System.err.println("Bad number of arguments ! 3 expected "+values.length+" found");   
  46.     System.err.println("callback ret " + 2);   
  47.     return 2;   
  48.    }   
  49. }   
  50.   
  51. /**  
  52. * java回调函数地址  
  53. */  
  54. public int getCallbackAddress() throws NativeException {   
  55.    return JNative.createCallback(3/*回调函数参数数目*/this);   
  56. }   
  57.   
  58. }  
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.exceptions.NativeException;
import org.xvolks.jnative.pointers.*;
import org.xvolks.jnative.util.Callback;

/**
* @date 2009-3-17
* @author charis
* @Copyright (c) qqtech All rights reserved. http://www.qqtech.com
*/
public class TestCallback implements Callback/*实现此接口*/ {

public TestCallback() {
  
}
/**
* Method callback
* java对应于dll中的回调函数
* @param values an long[] 参数数组,非整型数代表的都是地址
* @return an int
* @version 3/17/2008
*/
public int callback(long[] values) {
   if (values == null) {
    System.err.println("callback ret " + 3);
    return 3;
   }
   if (values.length == 3) {
    String name = "";
    try {
     Pointer _pointer = Pointer.createPointerToNativeMemory((int) values[0], 100); /*获取字符串通过指针*/
     Pointer _pointer1 = Pointer.createPointerToNativeMemory((int) values[1], 100);
    
     name = _pointer.getAsString() + "/" + _pointer1.getAsString();
    
    } catch (Exception e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
    }
    System.err.println("lParam " + name + "/" + values[2]);
   
   
    return 1;
   } else {
    System.err.println("Bad number of arguments ! 3 expected "+values.length+" found");
    System.err.println("callback ret " + 2);
    return 2;
   }
}

/**
* java回调函数地址
*/
public int getCallbackAddress() throws NativeException {
   return JNative.createCallback(3/*回调函数参数数目*/, this);
}

}

 

  TestMain.java

 

Java代码 复制代码
  1. import org.xvolks.jnative.JNative;   
  2. import org.xvolks.jnative.Type;   
  3.   
  4. /**  
  5. * @date 2009-3-10  
  6. * @author charis  
  7. * @Copyright (c) qqtech All rights reserved. http://www.qqtech.com  
  8. */  
  9. public class TestMain{   
  10.   
  11. public TestMain(){   
  12.      
  13. }   
  14.   
  15. public static void main(String[] args) {   
  16.    // TODO Auto-generated method stub   
  17.     try {   
  18.     JNative termcallback = new JNative("JNativeTest.dll""RegCallback" /*函数名*/ );   
  19.     TestCallback _callback = new TestCallback();   
  20.     termcallback.setRetVal(Type.VOID);   
  21.     termcallback.setParameter(0, _callback.getCallbackAddress()); /*回调函数地址作为参数传递到dll*/  
  22.     /**  
  23.     * 调用getCallbackAddress后要调用该行代码  
  24.     */  
  25.     JNative.releaseCallback(_callback);   
  26.     termcallback.invoke(); /*注册回调函数*/  
  27.          
  28.     JNative term = new JNative("JNativeTest.dll""fnJNativeTest"/*函数名*/);   
  29.     term.setRetVal(Type.STRING); /*返回值类型*/  
  30.     term.setParameter(0"hello word!"); /*左起第一个参数,索引为0*/  
  31.     term.invoke();   
  32.     String value = term.getRetVal();   
  33.     System.out.println(value);   
  34.       
  35.     term.setParameter(0"hello word second!");   
  36.     term.invoke();   
  37.     value = term.getRetVal();   
  38.     System.out.println(value);   
  39.       
  40.     } catch (Exception e) {   
  41.     e.printStackTrace();   
  42.    }   
  43.   }   
  44. }  
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;

/**
* @date 2009-3-10
* @author charis
* @Copyright (c) qqtech All rights reserved. http://www.qqtech.com
*/
public class TestMain{

public TestMain(){
  
}

public static void main(String[] args) {
   // TODO Auto-generated method stub
    try {
    JNative termcallback = new JNative("JNativeTest.dll", "RegCallback" /*函数名*/ );
    TestCallback _callback = new TestCallback();
    termcallback.setRetVal(Type.VOID);
    termcallback.setParameter(0, _callback.getCallbackAddress()); /*回调函数地址作为参数传递到dll*/
    /**
    * 调用getCallbackAddress后要调用该行代码
    */
    JNative.releaseCallback(_callback);
    termcallback.invoke(); /*注册回调函数*/
      
    JNative term = new JNative("JNativeTest.dll", "fnJNativeTest"/*函数名*/);
    term.setRetVal(Type.STRING); /*返回值类型*/
    term.setParameter(0, "hello word!"); /*左起第一个参数,索引为0*/
    term.invoke();
    String value = term.getRetVal();
    System.out.println(value);
   
    term.setParameter(0, "hello word second!");
    term.invoke();
    value = term.getRetVal();
    System.out.println(value);
   
    } catch (Exception e) {
    e.printStackTrace();
   }
  }
}
 

注意

当调用dll回调函数失败时,请检查参数数目和参数类型,并请c++程序员核对dll中回调函数的声明。

如此typedef void (WINAPI *pfCallbackTest)(char * msg, char* data, int type)声明将会引起调用失败

原文链接:http://hi.baidu.com/chinacharis/blog/item/87ef7f00b0b092067bec2cfb.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值