我正在創建一個C#DLL,該DLL將用作PLSQL Developer IDE(用C++設計)的插件。如何將C++指針指定給C#函數
我的C#DLL需要接受一個C++指針,然後將該指針分配給函數或方法,稍後再調用。
IDE爲構建這些插件提供了一個規範文檔,但它僅提供了C++和Delphi的示例。規格文檔提供我包含在這個 screenshot.
提供的C++範例的更多信息:
void (*IDE_MenuState)(int ID, int Index, BOOL Enabled);
BOOL (*IDE_Connected)();
void (*IDE_GetConnectionInfo)(char **Username, char **Password, char **Database);
void (*IDE_GetBrowserInfo)(char **ObjectType, char **ObjectOwner, char **ObjectName);
void RegisterCallback(int Index, void *Addr)
{
switch (Index)
{
case 10 :
(void *)IDE_MenuState = Addr;
break;
case 11 :
(void *)IDE_Connected = Addr;
break;
case 12 :
(void *)IDE_GetConnectionInfo = Addr;
break;
case 13 :
(void *)IDE_GetBrowserInfo = Addr;
break;
}
}
C#我到目前爲止有:
我要指出,我使用羅伯特Gieseckes Unmanaged Exports NuGet包出口功能。如有必要,我可以改變這一點。
public bool IDE_Connected()
{
return false;
}
public void IDE_MenuState(int ID, int Index, bool Enabled)
{
}
[DllExport("add", CallingConvention = CallingConvention.Cdecl, ExportName= "RegisterCallback")]
public static void RegisterCallback(int Index, IntPtr Addr)
{
if (Index == 10)
{
// Assign IntPtr Addr to IDE_MenuState()
// Please help :)
}
if (Index == 11)
{
// Assign IntPtr Addr to IDE_Connected()
// Please help :)
}
}
我怎樣才能在C++指針參數分配給我的C#的方法呢?
你在c#中嘗試過'void *'類型嗎? –
我可以將Addr參數從intPtr更改爲void *。那麼我該怎麼做才能將void * Addr分配給一個方法?謝謝。 –
你可以試試'Marshal.GetDelegateForFunctionPointer()' –