相关疑难解决方法(0)

C# 委托中的元帅 va_list

我正在尝试从 C# 开始这项工作:

C头:

typedef void (LogFunc) (const char *format, va_list args);

bool Init(uint32 version, LogFunc *log)
Run Code Online (Sandbox Code Playgroud)

C# 实现:

static class NativeMethods
{
    [DllImport("My.dll", SetLastError = true)]
    internal static extern bool Init(uint version, LogFunc log);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl, SetLastError = true)]
    internal delegate void LogFunc(string format, string[] args);
}

class Program
{
    public static void Main(string[] args)
    {
         NativeMethods.Init(5, LogMessage);
         Console.ReadLine();
    }

    private static void LogMessage(string format, string[] args)
    {
         Console.WriteLine("Format: {0}, args: {1}", format, DisplayArgs(args));
    }
}
Run Code Online (Sandbox Code Playgroud)

这里发生的是NativeMethods.Init调用回调LogMessage …

c# c++ pinvoke marshalling

5
推荐指数
2
解决办法
3921
查看次数

标签 统计

c# ×1

c++ ×1

marshalling ×1

pinvoke ×1