How to call native C# code from a C library

GingerMcMurrayGingerMcMurray ✭✭USMember ✭✭

I have a C library that needs pieces of it implemented in Xamarin C#. How can I call C# methods from inside the library?

If I'm using Objective C for iOS I can set up bridge methods. Android's Java has JNI. I assume that WinMo has something similar.

Every search I've tried explains how to call C from C#, but I need to go the opposite direction.

Thanks,
Ginger

Best Answer

Answers

  • ClintStLaurentClintStLaurent ✭✭✭✭✭ USUniversity ✭✭✭✭✭

    I have a C library that needs pieces of it implemented in Xamarin C#. How can I call C# methods from inside the library?

    Are you saying you want the library to be able to call outward to the Xamarin app that is its parent?
    If so... Redesign. It don't work that way. Libraries don't know anything of their parents.

  • GingerMcMurrayGingerMcMurray ✭✭ USMember ✭✭

    @ClintStLaurent said:

    I have a C library that needs pieces of it implemented in Xamarin C#. How can I call C# methods from inside the library?

    Are you saying you want the library to be able to call outward to the Xamarin app that is its parent?
    If so... Redesign. It don't work that way. Libraries don't know anything of their parents.

    Perhaps a simpler question would be to leave libraries out of it entirely and ask "how do I let my C code call my C# methods?"

    Doing it with libraries is completely possible. I'm doing it right now using JNI for Android and Objective C bridges.

    For example, in Android JNI:

    jmethodID getMethodId(const char methodName, const char *signature)
    {
    jmethodID mid = (
    jEnv)->GetMethodID(jEnv, jClass, methodName, signature);
    if (mid == 0){
    fs_UtilDebugPrint("couldn't find method %s %s in obj",
    methodName, signature);
    }

    return mid;
    }

    bool doTheThing(const char Text)
    {
    jmethodID mid = getMethodId("doTheThing", "(Ljava/lang/String;)Z");
    if (0 == mid)
    return FALSE;
    jstring jtext = (
    jEnv)->NewStringUTF(jEnv, Text);
    jboolean result = (*jEnv)->CallBooleanMethod(jEnv, jObj, mid, jtext);
    bool returnValue = (bool) result;

    (*jEnv)->DeleteLocalRef(jEnv, jtext);

    return returnValue;
    }

  • ConwayConway ✭✭ USMember ✭✭

    We use to do a similar thing where we used underlying mono_xxx type methods at the C level to find classes and methods at the C# level, but Xamarin broke that mechanism along the way. When we asked them what happened, they asked why we were doing that. So instead of trying to find out what broke, we switched to passing pointers to static C# class methods (using delegates) down to our C library during initialization. Then the C code uses the function pointers to call up into C# code.

  • GingerMcMurrayGingerMcMurray ✭✭ USMember ✭✭

    @Conway said:
    We use to do a similar thing where we used underlying mono_xxx type methods at the C level to find classes and methods at the C# level, but Xamarin broke that mechanism along the way. When we asked them what happened, they asked why we were doing that. So instead of trying to find out what broke, we switched to passing pointers to static C# class methods (using delegates) down to our C library during initialization. Then the C code uses the function pointers to call up into C# code.

    Can you point me to an example? I'm trying build out a POC to show management that using Xamarin instead of three separate native apps is viable.

  • GingerMcMurrayGingerMcMurray ✭✭ USMember ✭✭

    Awesome, thanks!

Sign In or Register to comment.