By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,574 Members | 1,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,574 IT Pros & Developers. It's quick & easy.

Convert Delphi record structure to C Sharp

P: n/a
Hello,
I have the following delphi structure for a binary file. I'm looking
for idea how to read this file.

Mel
type
TMenuDataStruct = packed record
exename : string[150];
caption : string[25];
userid : string[20];
password : string[20];
sparam : string[255];
workdir : string[100];
IconSize : Integer;
IconPos : Integer;
NextDataPos : Integer;
end;
TMenuImageStruct = record
BitMap : TBitMap;
end;

TFileStruct = Record
Info : TMenuDataStruct;
Image : TMenuImageStruct;
end;

TMenuArray = Array of TfileStruct;
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Mel

//first convert your structure to C#, it will look like

......
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
.......

[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1 )]
struct TMenuDataStruct
{
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=150)]
public string exename;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=25)]
public string caption;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=20)]
public string userid ;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=20)]
public string password;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=255)]
public string sparam;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=100)]
public string workdir;
public int IconSize ;
public int IconPos ;
public int NextDataPos ;
}

// get file size
FileInfo oFileInfo = new FileInfo(FileName);
long FileSize = oFileInfo.Length;
int StructSize = Marshal.SizeOf(Struct);
// number of records
long Records = FileSize / StructSize;
long Record = 0;
TMenuDataStruct Struct;

BinaryReader FS = new BinaryReader(File.Open(FileName, FileMode.Open));
byte[] Buffer = new byte[StructSize];
do
{
Buffer = FS.ReadBytes(StructSize);
ByteToStruct(Buffer, ref Struct);

// do something here with your structure

Record += 1;
} while (Record < Records);
FS.Close();

// convert byte array ( record ) to structure
private void ByteToStruct(byte[] Buffer, ref TMenuDataStruct Struct)
{
IntPtr pCurrentPosition;
GCHandle Handle = GCHandle.Alloc(Buffer, GCHandleType.Pinned );
pCurrentPosition = Handle.AddrOfPinnedObject();
Struct = Convert.ChangeType(Marshal.PtrToStructure(pCurrent Position,
TMenuDataStruct ), TMenuDataStruct );
Handle.Free();
}

it is writen from head, so check for errors. Also when file has zero length
etc ....

Jarek
"Mel WEaver" <Me**@removespamcox.net> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
Hello,
I have the following delphi structure for a binary file. I'm looking
for idea how to read this file.

Mel
type
TMenuDataStruct = packed record
exename : string[150];
caption : string[25];
userid : string[20];
password : string[20];
sparam : string[255];
workdir : string[100];
IconSize : Integer;
IconPos : Integer;
NextDataPos : Integer;
end;
TMenuImageStruct = record
BitMap : TBitMap;
end;

TFileStruct = Record
Info : TMenuDataStruct;
Image : TMenuImageStruct;
end;

TMenuArray = Array of TfileStruct;

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004
Nov 16 '05 #2

P: n/a
Thank You
Mel
"Jaroslav Suchacek" <ja***************@atlas.cz> wrote in message
news:e8**************@TK2MSFTNGP09.phx.gbl...
Hi Mel

//first convert your structure to C#, it will look like

.....
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
......

[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1 )]
struct TMenuDataStruct
{
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=150)]
public string exename;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=25)]
public string caption;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=20)]
public string userid ;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=20)]
public string password;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=255)]
public string sparam;
[MarshalAs(UnmanagedType.ByValTStr , SizeConst=100)]
public string workdir;
public int IconSize ;
public int IconPos ;
public int NextDataPos ;
}

// get file size
FileInfo oFileInfo = new FileInfo(FileName);
long FileSize = oFileInfo.Length;
int StructSize = Marshal.SizeOf(Struct);
// number of records
long Records = FileSize / StructSize;
long Record = 0;
TMenuDataStruct Struct;

BinaryReader FS = new BinaryReader(File.Open(FileName, FileMode.Open));
byte[] Buffer = new byte[StructSize];
do
{
Buffer = FS.ReadBytes(StructSize);
ByteToStruct(Buffer, ref Struct);

// do something here with your structure

Record += 1;
} while (Record < Records);
FS.Close();

// convert byte array ( record ) to structure
private void ByteToStruct(byte[] Buffer, ref TMenuDataStruct Struct)
{
IntPtr pCurrentPosition;
GCHandle Handle = GCHandle.Alloc(Buffer, GCHandleType.Pinned );
pCurrentPosition = Handle.AddrOfPinnedObject();
Struct = Convert.ChangeType(Marshal.PtrToStructure(pCurrent Position,
TMenuDataStruct ), TMenuDataStruct );
Handle.Free();
}

it is writen from head, so check for errors. Also when file has zero length etc ....

Jarek
"Mel WEaver" <Me**@removespamcox.net> wrote in message
news:eX**************@TK2MSFTNGP12.phx.gbl...
Hello,
I have the following delphi structure for a binary file. I'm looking for idea how to read this file.

Mel
type
TMenuDataStruct = packed record
exename : string[150];
caption : string[25];
userid : string[20];
password : string[20];
sparam : string[255];
workdir : string[100];
IconSize : Integer;
IconPos : Integer;
NextDataPos : Integer;
end;
TMenuImageStruct = record
BitMap : TBitMap;
end;

TFileStruct = Record
Info : TMenuDataStruct;
Image : TMenuImageStruct;
end;

TMenuArray = Array of TfileStruct;

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.698 / Virus Database: 455 - Release Date: 02/06/2004

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.