ITeye博客

JNative dll回调函数使用实例

zengguo1988 · 2010-05-04 15:45 · 编程语言 · C · JNI · C++ · C# · thread · Markdown

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. /** The following ifdef block is the standard way of creating macros which make exporting
  4. * from a DLL simpler. All files within this DLL are compiled with the JNATIVETEST_EXPORTS
  5. * symbol defined on the command line. this symbol should not be defined on any project
  6. * that uses this DLL. This way any other project whose source files include this file see
  7. * JNATIVETEST_API functions as being imported from a DLL, wheras this DLL sees symbols
  8. * defined with this macro as being exported.
  9. **/
  10. #ifdef JNATIVETEST_EXPORTS
  11. #define JNATIVETEST_API extern "C" __declspec(dllexport)
  12. #else
  13. #define JNATIVETEST_API __declspec(dllimport)
  14. #endif
  15. typedef void (*pfCallbackTest)(char * msg, char* data, int type); /*回调函数声明*/
  16. // This class is exported from the JNativeTest.dll
  17. JNATIVETEST_API char* fnJNativeTest(char* msg);
  18. JNATIVETEST_API void RegCallback(pfCallbackTest ev);
  19. #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. #include <windows.h>
  3. #include "JNativeTest.h"
  4. BOOL APIENTRY DllMain( HANDLE hModule,
  5. DWORD ul_reason_for_call,
  6. LPVOID lpReserved
  7. )
  8. {
  9. switch (ul_reason_for_call)
  10. {
  11. case DLL_PROCESS_ATTACH:
  12. case DLL_THREAD_ATTACH:
  13. case DLL_THREAD_DETACH:
  14. case DLL_PROCESS_DETACH:
  15. break;
  16. }
  17. return TRUE;
  18. }
  19. pfCallbackTest fCallbackTest = NULL;
  20. /*注册回调*/
  21. void RegCallback(pfCallbackTest ev)
  22. {
  23. fCallbackTest = ev;
  24. }
  25. // This is an example of an exported function.
  26. char* fnJNativeTest(char* msg)
  27. {
  28. if (fCallbackTest != NULL)
  29. fCallbackTest(msg, "Test", 6); /*调用回调函数*/
  30. return msg;
  31. }
// 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. * @date 2009-3-17
  7. * @author charis
  8. * @Copyright (c) qqtech All rights reserved. http://www.qqtech.com
  9. */
  10. public class TestCallback implements Callback/*实现此接口*/ {
  11. public TestCallback() {
  12. }
  13. /**
  14. * Method callback
  15. * java对应于dll中的回调函数
  16. * @param values an long[] 参数数组,非整型数代表的都是地址
  17. * @return an int
  18. * @version 3/17/2008
  19. */
  20. public int callback(long[] values) {
  21. if (values == null) {
  22. System.err.println("callback ret " + 3);
  23. return 3;
  24. }
  25. if (values.length == 3) {
  26. String name = "";
  27. try {
  28. Pointer _pointer = Pointer.createPointerToNativeMemory((int) values[0], 100); /*获取字符串通过指针*/
  29. Pointer _pointer1 = Pointer.createPointerToNativeMemory((int) values[1], 100);
  30. name = _pointer.getAsString() + "/" + _pointer1.getAsString();
  31. } catch (Exception e1) {
  32. // TODO Auto-generated catch block
  33. e1.printStackTrace();
  34. }
  35. System.err.println("lParam " + name + "/" + values[2]);
  36. return 1;
  37. } else {
  38. System.err.println("Bad number of arguments ! 3 expected "+values.length+" found");
  39. System.err.println("callback ret " + 2);
  40. return 2;
  41. }
  42. }
  43. /**
  44. * java回调函数地址
  45. */
  46. public int getCallbackAddress() throws NativeException {
  47. return JNative.createCallback(3/*回调函数参数数目*/, this);
  48. }
  49. }
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. * @date 2009-3-10
  5. * @author charis
  6. * @Copyright (c) qqtech All rights reserved. http://www.qqtech.com
  7. */
  8. public class TestMain{
  9. public TestMain(){
  10. }
  11. public static void main(String[] args) {
  12. // TODO Auto-generated method stub
  13. try {
  14. JNative termcallback = new JNative("JNativeTest.dll", "RegCallback" /*函数名*/ );
  15. TestCallback _callback = new TestCallback();
  16. termcallback.setRetVal(Type.VOID);
  17. termcallback.setParameter(0, _callback.getCallbackAddress()); /*回调函数地址作为参数传递到dll*/
  18. /**
  19. * 调用getCallbackAddress后要调用该行代码
  20. */
  21. JNative.releaseCallback(_callback);
  22. termcallback.invoke(); /*注册回调函数*/
  23. JNative term = new JNative("JNativeTest.dll", "fnJNativeTest"/*函数名*/);
  24. term.setRetVal(Type.STRING); /*返回值类型*/
  25. term.setParameter(0, "hello word!"); /*左起第一个参数,索引为0*/
  26. term.invoke();
  27. String value = term.getRetVal();
  28. System.out.println(value);
  29. term.setParameter(0, "hello word second!");
  30. term.invoke();
  31. value = term.getRetVal();
  32. System.out.println(value);
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }
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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。