Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert Delphi record structure to C Sharp

26 views
Skip to first unread message

Mel WEaver

unread,
Jun 15, 2004, 10:14:20 PM6/15/04
to
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;


Jaroslav Suchacek

unread,
Jun 16, 2004, 2:03:21 AM6/16/04
to
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(pCurrentPosition,
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:eXFTge0U...@TK2MSFTNGP12.phx.gbl...


---
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


Mel Weaver

unread,
Jun 16, 2004, 11:26:02 AM6/16/04
to
Thank You
Mel


"Jaroslav Suchacek" <jaroslav...@atlas.cz> wrote in message
news:e8Ckge2U...@TK2MSFTNGP09.phx.gbl...

0 new messages