整理c#中奇怪的c++结构

本文关键字:c++ 结构 整理 | 更新日期: 2023-09-27 18:13:45

我一直在尝试在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#中实现它们?

整理c#中奇怪的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;
}