C# equivalent to C const char**

后端 未结 5 1203
鱼传尺愫
鱼传尺愫 2021-01-22 10:42

I want to implement a Mongoose (http://code.google.com/p/mongoose/) Binding in C#. There are some examples, but they don\'t work with the current version.

This is my cu

5条回答
  •  再見小時候
    2021-01-22 11:10

    DllImport("_mongoose",CallingConvention=CallingConvention.Cdecl)]
    private static extern IntPtr mg_start(IntPtr callback, IntPtr userData,
        [In][MarshalAsAttribute(UnmanagedType.LPArray,
                                ArraySubType=UnmanagedType.LPStr)] string[] options);
    

    Haven't tried this, but I think this gets you there. You might need to make the ArraySubType be LPWStr if you want unicode in the C call. Making it LPStr gives you ANSI.

    Are you doing the function pointers? That's where the real challenge is. Not so much from declaring and marshaling but rather from a pointer lifespan issue. If mg_start holds onto the unmanaged version of the delegate, you may find that the thunk gets garbage collected on you, in spite of what the documentation says. We've seen this happen so often that we've, where possible, rearchitected the underlying glue to not use that style of code.

    Generally speaking, if the API is a chatty API with tons of callbacks, you're going to be hosed by the callbacks. You're better off trying to create a C++/CLI library that implements the API in a far less chatty way with clear managed/unmanaged boundaries.

提交回复
热议问题