如何将C++指针分配给C#函数
How to Assign a C++ Pointer to a C# Function
我正在创建一个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#中更加有用。
相关文章:
- 当我尝试通过构造函数分配 char 数组时出现错误
- c++如何为类成员函数分配内存
- 如何将析构函数分配给指针
- 当我们在C++中创建类的对象时,为成员函数分配的内存在哪里?
- 将来自类的特定实例的函数分配给变量以保持状态
- 为什么我不能使用已删除或私有析构函数分配类的数组?
- 如何将 gmock 函数分配给特定的函数指针
- 将成员函数分配给实例成员的属性
- 当设置对象等于另一个函数的返回值时,为什么要调用移动构造函数/分配
- 尝试将返回无符号值的函数分配给指向无符号的指针时,继续获取 SIGSEGV
- 当 lambda 函数必须访问其他成员时,如何将该函数分配给结构成员
- 将 lambda 函数分配给静态成员变量 (c++)
- 如何安全地释放构造函数分配的资源
- 如何将函数分配给函数指针?
- 是否可以在基类中删除复制和移动构造函数/分配运算符
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 为什么从工厂函数分配向量并在循环中迭代它会导致段错误
- 如何在隐式复制构造函数/分配运算符中压制成员
- 在将函数分配给函数指针时,为什么可以多次使用取消任务运算符
- C 将成员对象函数分配给类成员功能