Last active
February 15, 2023 20:52
-
-
Save pavly-gerges/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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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