我目前正在尝试从非托管 C++ 应用程序的 C# DLL 调用函数。
在网上搜索了几个小时之后,我发现我有几个选择。
我可以使用 COM、DllExport或对委托使用反向 PInvoke。最后一个听起来对我最有吸引力,所以在搜索之后我最终来到了这里。
它指出文章展示了如何使用反向 PInvoke,但看起来 C# 代码必须先导入 C++ Dll,然后才能使用。
我需要能够使用 C++ 调用我的 C# Dll 函数,而无需先运行 C# 应用程序。
也许反向 PInvoke 不是这样做的方法,但是当涉及到低级别的东西时我非常缺乏经验,所以任何关于如何做到这一点的指针或提示都会很棒。
链接中的代码是
C#
using System.Runtime.InteropServices;
public class foo    
{    
    public delegate void callback(string str);
    public static void callee(string str)    
    {    
        System.Console.WriteLine("Managed: " +str);    
    }
    public static int Main()    
    {    
        caller("Hello World!", 10, new callback(foo.callee));    
        return 0;    
    }
    [DllImport("nat.dll",CallingConvention=CallingConvention.StdCall)]    
    public static extern void caller(string str, int count, callback call);    
}
C++
#include <stdio.h>    
#include <string.h>
typedef void (__stdcall *callback)(wchar_t * str);    
extern "C" __declspec(dllexport) void __stdcall caller(wchar_t * input, int count, callback call)    
{    
    for(int i = 0; i < count; i++)    
    {    
        call(input);    
    }    
}