0

I am working with the android NDK trying to use an existing library to build an Application.

The following function is declared in the library...

BYTE __stdcall InitRelay(fp_setbaud _setbaud, fp_get _get, fp_put _put, fp_flush _flush, fp_delay _delay){

These are the declarations for the parameters passed to InitRelay...

typedef void (__stdcall *fp_setbaud)(WORD);
typedef short (__stdcall *fp_get)(WORD);
typedef void (__stdcall *fp_put)(BYTE);
typedef void (__stdcall *fp_flush)(void);
typedef void (__stdcall *fp_delay)(WORD);
 typedef short (__stdcall *fp_ProgressUpdate)(WORD);

I've been to this thread, but still am not sure what to do. The difference between what I am doing and what they are doing, is that I want to call a native function in C that requires a function pointer as a parameter.

I was considering writing a wrapper function in the Native code, does that sound right? My main problem is that I cannot change the native code i've been given, but I can add new functions. I know people will ask me to post my java code, so I will, but it is literally useless. It is just a method call with parameters that currently don't make sense to java.

 // InitRelay
    InitRelay( fp_setbaud _setbaud, fp_get _get, fp_put, fp_flush _flush, fp_delay _delay );
Community
  • 1
  • 1
JuiCe
  • 4,132
  • 16
  • 68
  • 119

1 Answers1

2

Yes, write native wrappers. You will need to do this anyway as jni has very particular naming requirements for the interface functions. Non-trivial data types will also require copy conversion from/to jni references.

Process-level static state in the library can also give you a lot of headaches, since android does not link the lifetime of a process to a user perceptible session lifetime. Android will also quite happily put multiple distinct sessions into the same process.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • Thank you, I actually have no experience in C, and have only been working android for about five weeks, is there anywhere you know of I can learn more about this situation? – JuiCe Jul 10 '12 at 14:40
  • Well, start by building the hello-jni example from the ndk samples, and look at the function naming and what it has to do to create a string that java can use. – Chris Stratton Jul 10 '12 at 14:46
  • Yeah, I've done it. I'm comfortable calling functions from the native code in my Android code. What I'm not comfortable is writing wrappers in C, but I guess that's a very broad topic. – JuiCe Jul 10 '12 at 14:50
  • Basically you need to create things that are called in a jni-compatible way, and call the native library in the way it expects. That is fairly straightforward on a detail level; the real challenge will be figuring out if an overlall plan of mapping one onto the other is workable, or if you may need to consider a different overall mapping. – Chris Stratton Jul 10 '12 at 14:53
  • Not asking you to write my code for me, but if you find the time to edit the post and add some sort of example code for the wrapper, I'd really appreciate it. – JuiCe Jul 11 '12 at 11:25