add Mono internal call where a string is passed by reference

守給你的承諾、 提交于 2021-02-08 09:38:13

问题


I'm trying to create a method using mono where a string is passed by reference, here is the test code I have:

C++:

static bool p_TestMethod(int num, MonoString ** response) {

    auto b = mono_string_new(mono_domain_get(), "Test repsonse");
    response = &b;

    return true;
}
//...
mono_add_internal_call("SharpCode.TestClass::Testmethod", p_TestMethod);

C#:

    [MethodImpl(MethodImplOptions.InternalCall)]
    public static extern bool Testmethod(int num, out string response);

    public bool RunTheTest()
    {
        string x;
        Testmethod(0, out x);
        Console.WriteLine("Test: {0}", x);

        return true;
    }

But no response is printed (only Test: )

How do I properly pass a string by reference using Mono?


回答1:


Fixed by doing it like this:

*response = mono_string_new(mono_domain_get(), "Test repsonse"); 

as suggested by delnan



来源:https://stackoverflow.com/questions/22428411/add-mono-internal-call-where-a-string-is-passed-by-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!