如何将C++指针分配给C#函数
本文关键字:函数 分配 指针 C++ | 更新日期: 2023-09-27 17:59:09
我正在创建一个C#DLL,该DLL将用作PLSQL Developer IDE的插件,该IDE是用C++设计的。
我的C#DLL需要接受一个C++指针,然后将该指针分配给稍后调用的函数或方法
IDE提供了构建这些插件的规范文档,但它只提供了C++和Delphi的示例。规范文档提供了我在其中包含的更多信息屏幕截图。
提供的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#到目前为止:
我应该注意,我正在使用Robert Gieseckes Unmanaged Exports NuGet Package来导出函数。如有必要,我可以更改
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#方法?
出于实际目的,编译后的方法不能在运行时更改。因此,更改IDE_Connected()
的功能(来自您的代码示例)是不可能的。
但是,您可以将方法声明为delegates
(请参见此处),并为每个方法创建一个静态实例。试试这个:
public unsafe class MyClass
{
delegate void IDE_MenuState(int ID, int Index, bool Enabled);
delegate bool IDE_Connected();
delegate void IDE_GetConnectionInfo(char** Username, char** Password, char** Database);
delegate void IDE_GetBrowserInfo(char** ObjectType, char** ObjectOwner, char** ObjectName);
static IDE_MenuState method_IDE_MenuState;
static IDE_Connected method_IDE_Connected;
static IDE_GetConnectionInfo method_IDE_GetConnectionInfo;
static IDE_GetBrowserInfo method_IDE_GetBrowserInfo;
public static void RegisterCallback(int Index, IntPtr Addr)
{
switch (Index)
{
case 10:
method_IDE_MenuState = (IDE_MenuState)Marshal.GetDelegateForFunctionPointer(Addr, typeof(IDE_MenuState));
break;
case 11:
method_IDE_Connected = (IDE_Connected)Marshal.GetDelegateForFunctionPointer(Addr, typeof(IDE_Connected));
break;
case 12:
method_IDE_GetConnectionInfo = (IDE_GetConnectionInfo)Marshal.GetDelegateForFunctionPointer(Addr, typeof(IDE_GetConnectionInfo));
break;
case 13:
method_IDE_GetBrowserInfo = (IDE_GetBrowserInfo)Marshal.GetDelegateForFunctionPointer(Addr, typeof(IDE_GetBrowserInfo));
break;
}
}
public static void IDE_MenuState(int ID, int Index, bool Enabled)
{
if (method_IDE_MenuState == null)
{
throw new MissingMethodException("IDE_MenuState has not been assigned pointer yet.");
}
method_IDE_MenuState(ID, Index, Enabled);
}
public static bool IDE_Connected()
{
if (method_IDE_Connected == null)
{
throw new MissingMethodException("IDE_Connected has not been assigned pointer yet.");
}
return method_IDE_Connected();
}
public static void IDE_GetConnectionInfo(char** Username, char** Password, char** Database)
{
if (method_IDE_GetConnectionInfo == null)
{
throw new MissingMethodException("IDE_GetConnectionInfo has not been assigned pointer yet.");
}
method_IDE_GetConnectionInfo(Username, Password, Database);
}
public static void IDE_GetBrowserInfo(char** ObjectType, char** ObjectOwner, char** ObjectName)
{
if (method_IDE_GetBrowserInfo == null)
{
throw new MissingMethodException("IDE_GetBrowserInfo has not been assigned pointer yet.");
}
method_IDE_GetBrowserInfo(ObjectType, ObjectOwner, ObjectName);
}
}
注意:我还没有对此进行测试,但尝试将IDE_GetConnectionInfo
和IDE_GetBrowserInfo
的方法签名更改为使用out char[]
或更好的out string
,而不是char**
。这将使您的API在C#中更加有用。