I've embedded some C# code into an iphone objective-c app using monotouch. I'm able to call into the C# code and get a return value, but I am not able to invoke a C function from the C# code using PInvoke. I am attempting to get a trivial example working. It is a simple calculator. The objective c code calls into the C# code to add two integers. The C# code should then call back into the objective-c code and give it the updated value. Below is the C# code that handles the calculation:
public class MyClass
{
static public void Add(int a, int b)
{
updateResult(a + b);
}
[DllImport("__Internal", EntryPoint="updateResult")]
static extern public void updateResult(int result);
}
Here is my code that initializes mono and handles the add method:
@implementation Mono
- (id)init {
self = [super init];
if (self) {
NSBundle *main = [NSBundle mainBundle];
NSString *path = [main bundlePath];
const char *c_path = [path UTF8String];
[main autorelease];
[path autorelease];
chdir (c_path);
setenv ("MONO_PATH", c_path, 1);
setenv ("MONO_XMLSERIALIZER_THS", "no", 1);
setenv ("DYLD_BIND_AT_LAUNCH", "1", 1);
setenv ("MONO_REFLECTION_SERIALIZER", "yes", 1);
#if defined (__arm__)
mono_aot_register_module (mono_aot_module_mscorlib_info);
mono_aot_register_module (mono_aot_module_Math_info);
mono_jit_set_aot_only (TRUE);
#endif
mono_jit_init("MonoTouch");
MonoAssembly *assembly = mono_assembly_open("Math.dll", NULL);
MonoImage *img = mono_assembly_get_image(assembly);
MonoClass *cls = mono_class_from_name(img, "Math", "MyClass");
MonoMethodDesc *methodDesc = mono_method_desc_new("Math.MyClass:Add", TRUE);
_addMethod = mono_method_desc_search_in_class(methodDesc, cls);
}
return self;
}
- (void)addA:(int)a plusB:(int)b {
void *params[] = { &a, &b };
mono_runtime_invoke(_addMethod, NULL, params, NULL);
}
@end
and here is the definition of the updateResults method:
extern void updateResult(int result) {
printf("Got Result");
}
When the updateResults is called from the C# side of things I get the following exception:
Unhandled Exception: System.EntryPointNotFoundException: updateResult
at (wrapper managed-to-native) Math.MyClass:updateResult (int)
at Math.MyClass.Add (Int32 a, Int32 b) [0x00000] in <filename unknown>:0
I can see the symbol in the binary with the following command:
$ nm Calc | grep updateResult
00002b2e t _updateResult
Setting the MONO_LOG_LEVEL environment variable to debug I get the following output when attempting the PInvoke. It looks like it finds the method and then can't find it:
Mono: DllImport attempting to load: '_Internal'.Mono: Searching for 'updateResult'.Mono: Probing 'updateResult'.Mono: Probing 'updateResult'.Mono: Probing 'updateResultA'.Mono: Probing 'updateResultA'.Mono: DllImport attempting to load: '_Internal'.Mono: Searching for 'updateResult'.Mono: Probing 'updateResult'.Mono: Probing 'updateResult'.Mono: Probing 'updateResultA'.Mono: Probing 'updateResultA'.Mono: AOT FOUND AOT compiled code for (wrapper managed-to-native) Math.MyClass:updateResult (int) 0x3bc8 - 0x3d90 0x3dcb
Mono: DllImport attempting to load: '__Internal'.Mono: Searching for 'updateResult'.Mono: Probing 'updateResult'.Mono: Probing 'updateResult'.Mono: Probing 'updateResultA'.Mono: Probing 'updateResultA'.Unable to resolve pinvoke method 'Math.MyClass:updateResult (int)' Re-run with MONO_LOG_LEVEL=debug for more information.
I've spent quite some time trying to figure this out. From what I've read it seems like this should be pretty trivial, but I can't get it to work. Any help would be much appreciated.
Would it be better to use Selectors?
That is the default path to follow as suggested by the makers of MonoTouch.