現在、アンマネージC++アプリケーションからC#DLLから関数を呼び出そうとしています。ネイティブC++から逆PInvoke
ウェブ上で時間を検索した後、私はいくつかの選択肢があることがわかりました。
私はCOM、DllExport
を使用するか、代理人と逆PInvokeを使用できます。最後は私に最も魅力的だったので、SOを検索した後、私はhereになった。
この記事では逆PInvokeを使用する方法を示していますが、使用する前にC#コードを最初にインポートする必要があるようです。
まずC#アプリケーションを実行せずに、C++を使用してC#のDLL関数を呼び出す必要があります。
逆PInvokeはそれを行う方法ではないかもしれませんが、低レベルのものについては非常に経験がないので、これを行う方法に関するヒントやヒントは素晴らしいでしょう。
リンクのコードは、ちょうどあなた自身のCLRホストをスピンアップし、何を実行し、
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);
}
}
最も単純なUnmanagedExportsです:https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports –
何がやりたいことはC++からC#のDLLをホストしている場合は、[見ていませんでしたここに] [1]? [1]:http://stackoverflow.com/questions/3799907/using-c-sharp-dll-in-project-c – plinth