Convert Delphi record structure to C Sharp

M

Mel WEaver

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

Jaroslav Suchacek

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 said:
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;
 
M

Mel Weaver

Thank You
Mel


Jaroslav Suchacek said:
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 said:
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;
 
Joined
May 16, 2006
Messages
1
Reaction score
0
TMenuDataStruct denotes a 'class' where a 'variable' was expected

Hi,

Have tried this example and it all looked like it was going too well. When I compile I get an error:

...LicenceRecord denotes a 'class' where a 'variable' was expected.

Please help me if you, i'm having a senior moment.

spj_uk
 
Joined
Apr 19, 2008
Messages
2
Reaction score
0
Convert Packed Record to C#

Type
PRecKey = ^RecKey;
RecKey = Packed Record
Key_Command : Array[0..2] of Char;
Key_Pan_Type : Char;
Key_Format : Char;
Key_E_Pin : Array[0..15] of Char;
Key_Terminal : Array[0..4] of Char;
Key_Key_Type : Char;
Key_OffSet : Array[0..3] of Char;
Key_Pan : Array[0..19] of Char;
Key_Pad_Char : Char;
Key_Pin_length : Array[0..1] of Char;
Key_Master_key : Array[0..1] of Char;
Key_Old_E_Pin : Array[0..15] of Char;
Key_KEK_WK : Array[0..47] of Char;
end;

Definition:
rbuf : array[0..1023] of char; // contain data
key : PRecKey;


Using in delphi:

key := PRecKey(@rbuf[0]);


There is way to turn it to C # .net 2005?????
Is possible?


Thanks....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top