Skip to content

Instantly share code, notes, and snippets.

@Scrappers-glitch
Last active February 15, 2023 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Scrappers-glitch/8f79133d8dd386193227db288db1cc4b to your computer and use it in GitHub Desktop.
Save Scrappers-glitch/8f79133d8dd386193227db288db1cc4b to your computer and use it in GitHub Desktop.
An example showing how to assign a function table to a C struct in one go
#include <stdio.h>
#include <stdlib.h>
/** jni.h **********************************************************************************/
struct JNINativeInterface_ {
void *reserved0;
void *reserved1;
void *reserved2;
void *reserved3;
int (*GetVersion)(int* version);
};
/**
* Construct a memory reference type
*/
typedef struct JNINativeInterface_* JNIEnv;
/**
* Implementation
*/
int GetVersion(int* version) {
return 0b10 * (*version);
}
JNIEnv* env = NULL;
/** jni.h **********************************************************************************/
/** jni.c **********************************************************************************/
/**
* Initializes the JNI interface function table.
*
* @param env a pointer to the JNIEnv address to be allocated.
*/
static inline void init_jni(JNIEnv** env) {
struct JNINativeInterface_ jni_interface = {
NULL,
NULL,
NULL,
NULL,
&GetVersion
};
*env = (JNIEnv*) calloc(1, sizeof(char));
**env = &jni_interface;
}
int main() {
init_jni(&env);
/** jni.c **********************************************************************************/
/** user code**********************************************************************************/
int java = 8;
int jni_version = (*env)->GetVersion(&java);
printf("%i\n", jni_version);
free(env);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment