Chall中的Marshall奇怪的C ++结构

时间:2016-10-19 19:41:33

标签: c# c++ marshalling

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

2 个答案:

答案 0 :(得分:2)

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;

}

答案 1 :(得分:-2)

尝试使用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;
}