读取vc生成的结构体文件,在线等

cnzhanghc 2008-12-11 09:56:23
在vc中定义的文件结构
typedef struct ingalog {
UINT id; //日志顺序ID号
FILETIME sj; //时间 存储用FileTimeToLocalFileTime函数转换后的
char czlx; //操作类型 定义为 程序监控(P),网址监控(I),安全登录(L) 之一
char cznr[58]; //操作内容 Keyid + (程序名称 or 域名)
DWORD czsm; //在附日志文件中的起始位置
long czsmlen; //在附日志文件中的占用长度
char dj; //等级
int cid; //CID
int uid; //UID
char sign[128]; //签名信息
} INGALOG;

请问在C#中如何来读取VC中用这个结构生成的文件
...全文
112 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnzhanghc 2008-12-11
  • 打赏
  • 举报
回复
谢 谢 zhyuanshan
xjh_net 2008-12-11
  • 打赏
  • 举报
回复
public struct ingalog {
public uint32 id; //日志顺序ID号
public DateTime sj; //时间 存储用FileTimeToLocalFileTime函数转换后的
public char czlx; //操作类型 定义为 程序监控(P),网址监控(I),安全登录(L) 之一
public char cznr[58]; //操作内容 Keyid + (程序名称 or 域名)
public uint32 czsm; //在附日志文件中的起始位置
public int32 czsmlen; //在附日志文件中的占用长度
public char dj; //等级
public int cid; //CID
public int uid; //UID
public char sign[128]; //签名信息
}
zhyuanshan 2008-12-11
  • 打赏
  • 举报
回复
用C#声明结构:
[StructLayout(LayoutKind.Sequential)]
public struct FILETIME
{
public UInt32 dwLowDateTime;
public UInt32 dwHighDateTime;
}
[StructLayout(LayoutKind.Sequential)]
public struct INGALOG
{
public UInt32 id; //日志顺序ID号
public FILETIME sj; //时间 存储用FileTimeToLocalFileTime函数转换后的
public byte czlx; //操作类型 定义为 程序监控(P),网址监控(I),安全登录(L) 之一
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 58)]
public byte[] cznr; //操作内容 Keyid + (程序名称 or 域名)
public UInt32 czsm; //在附日志文件中的起始位置
public int czsmlen; //在附日志文件中的占用长度
public byte dj; //等级
public int cid; //CID
public int uid; //UID
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] sign; //签名信息
}

拷贝文件到结构:
static void Main()
{
INGALOG ing = new INGALOG();
int len = Marshal.SizeOf(ing);
byte[] fileBytes = new byte[len];

StreamReader sr = new StreamReader("c:\\Ingalog.txt");
sr.Read(fileBytes, 0, len);
sr.Close();

IntPtr ingPtr = Marshal.AllocHGlobal(len);
Marshal.StructureToPtr(ing, ingPtr, false);
//拷贝结构
Marshal.Copy(ingPtr, fileBytes, 0, len);

Marshal.FreeHGlobal(IntPtr);
}
birdlonger 2008-12-11
  • 打赏
  • 举报
回复
mark >!
传闻可以在C#中定义相同内存结构的结构体,引用操作..本人没实验过!

110,530

社区成员

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

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

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