473,216 Members | 1,920 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,216 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 12081
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.