C++中的struct在C#中如何写?比较特殊

calen 2004-09-29 10:19:27
着下面的两个struct比较特殊,该如何写成C#模式?


typedef struct {
BOOL bEnableProxy;
DWORD dwProxyPort;
char szProxyName[128]; //此处如何写成C#模式?
BOOL bEnableIPRestrict;
DWORD dwIPRestrictNum;
char (*pszIPRestrictList)[128]; //此处如何写成C#模式?
DWORD dwFlags;
} TDataBrokerOptions;

typedef struct {
TDataBrokerStatusCallback pfStatus;
TDataBrokerAVCallback pfAV;
DWORD dwStatusContext;
DWORD dwAVContext;
char* pzServerType; //此处如何写成C#模式?
DWORD dwAudioCodec;
DWORD dwVideoCodec;
DWORD dwProtocolType;
char* zVSize; //此处如何写成C#模式?
} TDataBrokerInputOptions, *PTDataBrokerInputOptions; //此处如何写成C#模式?

...全文
663 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
calen 2004-09-29
  • 打赏
  • 举报
回复
谢谢 Brunhild() ,能否帮我把我的那两个struct翻译一下?

比较少接触C++,所以比较菜,见笑了

再次感谢
Brunhild 2004-09-29
  • 打赏
  • 举报
回复
大概是这样:
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)]
public struct TDataBrokerInputOptions
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
public byte[] szProxyName ;

[MarshalAs(UnmanagedType.LPStr)]
public string pszIPRestrictList;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)]
public struct TDataBrokerOptions
{
[MarshalAs(UnmanagedType.LPStr)]
public string pzServerType;

public IntPtr zVSize;
}
calen 2004-09-29
  • 打赏
  • 举报
回复
sorry

请问如下的三处如何翻译成C#?

char (*pszIPRestrictList)[128]; //此处如何写成C#模式?
char szProxyName[128]; //此处如何写成C#模式?
} TDataBrokerInputOptions, *PTDataBrokerInputOptions; //此处如何写成C#模式?
calen 2004-09-29
  • 打赏
  • 举报
回复
谢谢 xrascal(横刀夺爱)


请问如下的两处如何翻译成C#?

char szProxyName[128]; //此处如何写成C#模式?
} TDataBrokerInputOptions, *PTDataBrokerInputOptions; //此处如何写成C#模式?
暗石绿 2004-09-29
  • 打赏
  • 举报
回复
Wtypes.h 中的非托管类型 非托管C 语言类型 托管类名 说明
HANDLE void* System.IntPtr 32 位
BYTE unsigned char System.Byte 8 位
SHORT short System.Int16 16 位
WORD unsigned short System.UInt16 16 位
INT int System.Int32 32 位
UINT unsigned int System.UInt32 32 位
LONG long System.Int32 32 位
BOOL long System.Int32 32 位
DWORD unsigned long System.UInt32 32 位
ULONG unsigned long System.UInt32 32 位
CHAR char System.Char 用 ANSI 修饰。
LPSTR char* System.String 或 System.StringBuilder 用 ANSI 修饰。
LPCSTR Const char* System.String 或 System.StringBuilder 用 ANSI 修饰。
LPWSTR wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。
LPCWSTR Const wchar_t* System.String 或 System.StringBuilder 用 Unicode 修饰。
FLOAT Float System.Single 32 位
DOUBLE Double System.Double 64 位
calen 2004-09-29
  • 打赏
  • 举报
回复
谢谢大家,大概解决问题,如果有问题,还请大家继续帮忙

calen 2004-09-29
  • 打赏
  • 举报
回复
编号: 4 发送者 Brunhild 发送时间 2004-9-29 13:45:50 删除 回复
内容 [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)]
public struct TDataBrokerOptions
{
public TDataBrokerStatusCallback pfStatus;
public TDataBrokerAVCallback pfAV;
public uint dwStatusContext;
public uint dwAVContext;
[MarshalAs(UnmanagedType.LPStr)]
public string pzServerType;
public uint dwAudioCodec;
public uint dwVideoCodec;
public uint dwProtocolType;
public IntPtr zVSize;
}
另外
typedef struct
{
...
} TDataBrokerInputOptions, *PTDataBrokerInputOptions;
应该是定义一个结构类型和这个结构的指针,C#中只需要定义结构就可以了,不必定义指针

编号: 3 发送者 Brunhild 发送时间 2004-9-29 13:45:24 删除 回复
内容 我的C++也是一般,试试翻译吧。
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)]
public struct TDataBrokerInputOptions
{
public bool bEnableProxy;
public uint dwProxyPort;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=128)]
public byte[] szProxyName ;
public bool bEnableIPRestrict;
public uint dwIPRestrictNum;
[MarshalAs(UnmanagedType.LPStr)]
public string pszIPRestrictList;
public uint dwFlags;
}




谢谢Brunhild()

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧