整理c#中奇怪的c++结构
Marshall weird C++ structure in C#
我一直在尝试在c#中整理这个结构,但是我在最后两行遇到了麻烦。
typedef struct _modenv_
{
long lMajor; /* major version of kernel */
long lMinor; /* minor version of kernel */
long lRelease; /* release version of kernel */
long lResultSize; /* sResult buffer size */
long (__stdcall *lPGSM_ExecuteKernel) (struct _modenv_ *PGEnv, char *sCommand, char *sResult, long lLength);
long (__stdcall *lPGSM_ExecuteCommand)(struct _modenv_ *PGEnv, char *sCommand, char *sResult, long lLength);
} PGMODENV;
我所做的就是:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PGMODENV
{
/* input data */
public long lMajor; /* major version of kernel */
public long lMinor; /* minor version of kernel */
public long lRelease; /* release version of kernel */
/* updated data */
public long lResultSize; /* sResult buffer size */
}
如何在c#中实现它们?
public struct PGMODENV
{
public int lMajor; // major version of kernel
public int lMinor; // minor version of kernel
public int lRelease; // release version of kernel
public int lResultSize; // sResult buffer size
//The original C++ function pointer contained an unconverted modifier:
//ORIGINAL LINE: int(__stdcall *lPGSM_ExecuteKernel)(struct _modenv_ *PGEnv, sbyte *sCommand, sbyte *sResult, int lLength);
public delegate int lPGSM_ExecuteKernelDelegate(PGMODENV PGEnv, ref string sCommand, ref string sResult, int lLength);
public lPGSM_ExecuteKernelDelegate lPGSM_ExecuteKernel;
//The original C++ function pointer contained an unconverted modifier:
//ORIGINAL LINE: int(__stdcall *lPGSM_ExecuteCommand)(struct _modenv_ *PGEnv, sbyte *sCommand, sbyte *sResult, int lLength);
public delegate int lPGSM_ExecuteCommandDelegate(PGMODENV PGEnv, ref string sCommand, ref string sResult, int lLength);
public lPGSM_ExecuteCommandDelegate lPGSM_ExecuteCommand;
}
尝试使用IntPtr
,因为它们是指向函数的指针。所以它看起来像这样:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct PGMODENV
{
/* input data */
public long lMajor; /* major version of kernel */
public long lMinor; /* minor version of kernel */
public long lRelease; /* release version of kernel */
/* updated data */
public long lResultSize; /* sResult buffer size */
public IntPtr lPGSM_ExecuteKernel;
public IntPtr lPGSM_ExecuteCommand;
}
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- vscode g++链路故障:体系结构x86_64的未定义符号
- C++概念:如何使用'concept'检查模板化结构的属性?