Wrapping C++ objects in C

Introduction Using C functions from C++ is very easy but going the other way isn’t. However, it can be done with a little ingenuity. Really, this isn’t as crazy as it sounds. I’ll use a simple adder for the object. It takes an integer as a starting value and has two functions for adding and getting the current value. This could be expanded later into a complex object for mathematical operations but that’s really not necessary right now....

August 18, 2017 · John

JNI is (Not) Your Friend

Introduction So far we’ve scratched the surface of using JNI when we looked at wrapping a C library and Calling Java from C. Now we’re going to look into some more complex uses. Java Class Since we’re working with JNI, we’ll need a Java class to use what we’re going to expose from C. DemoFuncs.java class DemoFuncs { static { System.loadLibrary("demo_lib"); } public enum Days { MON, TUE, WED, THU, FRI, SAT, SUN, } public static native int[] demo_array_return(); public static native int[] demo_array_copy_inc(int[] in); public static native Days demo_enum_val(); public static native Days demo_enum_field_val(); public static native void demo_exception_1(); public static native void demo_exception_2(); public static native void demo_exception_3(); public static native boolean demo_exception_4(); public static native Name demo_name(String name); } Note: This class includes an enum because one of the later examples uses them....

July 9, 2017 · John

Calling Java From C

Introduction Obviously, JNI lets you call Java functions and use Java classes in C. Typical a Java app is the one calling into C. Now Let’s say you don’t have a Java app that kicks off the process but you want your C app to still use some Java code. Okay, I know what you’re thinking, “why”? Well, there are companies out there that provide SDKs for their products only in Java....

June 17, 2017 · John

Wrapping a C library in Java

Introduction It can’t be argued that Java is popular and successful. It is consistently the number one language on TIOBE’s popularity list, above C which comes in as number two. This ranking is based on popularity and doesn’t mean Java is more used than C but that doesn’t change the fact that there is a lot of Java code out there. Unlike other languages interop with Java is not what I’d call easy....

June 6, 2017 · John

Interop Objective-C Objects In C Using ARC

Introduction Using C functions from Objective-C is very easy but going the other way isn’t so easy. Especially with ARC which can destroy the object out from under you because C code is outside of ARC. With ARC Objective-C objects are no longer allowed in C structs for this very reason. Here is a situation I ran into where I needed to use Objective-C objects from within C. At work we have a library that was ported to iOS....

December 18, 2015 · John