473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Convert Delphi record structure to C Sharp

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
2 12126
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
2
by: Huseyin Altun | last post by:
I have DLL written in Delphi 6. Its prototype is below: function EnumUserGroups(Proc: TEnumUserGroupProc; APtr: Pointer): LongInt; stdCall; I have declared it in c# like below: (VS 2003) ...
2
by: ABC | last post by:
I have a very large amount VB code convert into C Sharp. I just complete it use tools. But it still have some codes errors. There are object to datatype problems. I need to edit many files to...
8
by: Carlos | last post by:
I have a strinf with comma separated field. Is is possible to convert it to a Structure ? Thanks
2
by: jewalsh2k | last post by:
Hi, I have a delphi dll which I am trying to call from C# but something isn't working, I get an SEHException thrown by the dll but I don't get enough info to debug it. I can't change the dll...
8
by: mesutalturk | last post by:
How do i convert the following C# code to Delphi? public static uint GenerateSiteID(string SiteName) { uint id = 0; char arr = SiteName.ToCharArray(); //Convert the sitename to a Char Array...
4
by: acap | last post by:
my problem is converting C to java, pls help..... this the code.... #include <stdio.h> #include <string.h> #include <ctype.h> #define FIRST_NAME_LEN 31 #define SECOND_NAME_LEN 51
0
by: jsmith | last post by:
hi all i use delphi for win32 applications and C for microcontrollers. i going to migrate to C for windows (and linux soon), while developing from win32 to DotNet. there are several compilers...
2
by: LostnCode | last post by:
Hi, Can anyone help, I need to convert his code from vbscript to sharp C# for use with ASP.Net2.0? This is my first time using a forum. I don't know anything about either coding language so...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.