C#相当于C const char ** [英] C# equivalent to C const char**

查看:155
本文介绍了C#相当于C const char **的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中实现一个Mongoose(http://code.google.com/p/mongoose/)绑定​​。有一些例子,但它们不适用于当前版本。



这是我当前的函数调用:

  [DllImport _mongoose,CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr mg_start(int zero,Nullable,string options); 

(工作)C等价物将是:

  const char * options [] = {
document_root,/ var / www,
listening_ports,80,443s,
NULL
};
struct mg_context * ctx = mg_start(& my_func,NULL,options);

其中mg_start定义为:

  struct mg_context * mg_start(mg_callback_t callback,void * user_data,
const char ** options);

您可以在这里找到整个C示例:
https://svn.apache.org/repos/asf/incubator /celix/trunk/remote_services/remote_service_admin_http/private/include/mongoose.h



如何传递 const char * options [] to c#?



谢谢

解决方案>

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

没有尝试过这个,但我认为这会让你在那里。如果要在C调用中使用unicode,则可能需要使ArraySubType为LPWStr。让它LPStr给你ANSI。



你在做这个函数指针吗?那就是真正的挑战。不是宣称和编排,而是从指针的寿命问题。如果mg_start持有委托的非托管版本,您可能会发现,尽管文档说明了这个thunk在你身上收集垃圾。我们已经看到这种情况经常发生,我们已经在可能的情况下重新构建了基础胶水,以便不使用那种风格的代码。



一般来说,如果API是一个具有大量回调的聊天API,你将被回调所淹没。您最好尝试创建一个C ++ / CLI库,以更少的聊天方式实现API,具有明确的管理/非管理边界。


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 current function call:

[DllImport("_mongoose",CallingConvention=CallingConvention.Cdecl)] private static extern IntPtr mg_start(int zero, Nullable, string options);

The (working) C equivalent would be:

const char *options[] = {
     "document_root", "/var/www",
     "listening_ports", "80,443s",
     NULL
   };
struct mg_context *ctx = mg_start(&my_func, NULL, options);

where mg_start is defined as:

struct mg_context *mg_start(mg_callback_t callback, void *user_data,
                            const char **options);

You can find the whole C example here: https://svn.apache.org/repos/asf/incubator/celix/trunk/remote_services/remote_service_admin_http/private/include/mongoose.h

How do I transfer the const char *options[] to c#?

Thank you

解决方案

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.

这篇关于C#相当于C const char **的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆