Created
January 27, 2023 22:40
-
-
Save pavly-gerges/042a79698743f73e6c9a9678090fc145 to your computer and use it in GitHub Desktop.
An example for how java native interface defines its headers and the implementation of java native apis
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> | |
/** | |
* Headers | |
*/ | |
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); | |
} | |
int main() { | |
struct JNINativeInterface_* jni_interface = (struct JNINativeInterface_*) calloc(1, sizeof(struct JNINativeInterface_)); | |
JNIEnv* env = &jni_interface; | |
(*env)->GetVersion = &GetVersion; | |
int java = 8; | |
int jni_version = (*env)->GetVersion(&java); | |
printf("%i\n", jni_version); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment