473,854 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
TMenuImageStruc t = record
BitMap : TBitMap;
end;

TFileStruct = Record
Info : TMenuDataStruct ;
Image : TMenuImageStruc t;
end;

TMenuArray = Array of TfileStruct;
Nov 16 '05 #1
2 12204
Hi Mel

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

......
using System.IO;
using System.Reflecti on;
using System.Runtime. InteropServices ;
.......

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

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

BinaryReader FS = new BinaryReader(Fi le.Open(FileNam e, FileMode.Open)) ;
byte[] Buffer = new byte[StructSize];
do
{
Buffer = FS.ReadBytes(St ructSize);
ByteToStruct(Bu ffer, ref Struct);

// do something here with your structure

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

// convert byte array ( record ) to structure
private void ByteToStruct(by te[] Buffer, ref TMenuDataStruct Struct)
{
IntPtr pCurrentPositio n;
GCHandle Handle = GCHandle.Alloc( Buffer, GCHandleType.Pi nned );
pCurrentPositio n = Handle.AddrOfPi nnedObject();
Struct = Convert.ChangeT ype(Marshal.Ptr ToStructure(pCu rrentPosition,
TMenuDataStruct ), TMenuDataStruct );
Handle.Free();
}

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

Jarek
"Mel WEaver" <Me**@removespa mcox.net> wrote in message
news:eX******** ******@TK2MSFTN GP12.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;
TMenuImageStruc t = record
BitMap : TBitMap;
end;

TFileStruct = Record
Info : TMenuDataStruct ;
Image : TMenuImageStruc t;
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******** ******@TK2MSFTN GP09.phx.gbl...
Hi Mel

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

.....
using System.IO;
using System.Reflecti on;
using System.Runtime. InteropServices ;
......

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

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

BinaryReader FS = new BinaryReader(Fi le.Open(FileNam e, FileMode.Open)) ;
byte[] Buffer = new byte[StructSize];
do
{
Buffer = FS.ReadBytes(St ructSize);
ByteToStruct(Bu ffer, ref Struct);

// do something here with your structure

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

// convert byte array ( record ) to structure
private void ByteToStruct(by te[] Buffer, ref TMenuDataStruct Struct)
{
IntPtr pCurrentPositio n;
GCHandle Handle = GCHandle.Alloc( Buffer, GCHandleType.Pi nned );
pCurrentPositio n = Handle.AddrOfPi nnedObject();
Struct = Convert.ChangeT ype(Marshal.Ptr ToStructure(pCu rrentPosition,
TMenuDataStruct ), TMenuDataStruct );
Handle.Free();
}

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

Jarek
"Mel WEaver" <Me**@removespa mcox.net> wrote in message
news:eX******** ******@TK2MSFTN GP12.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;
TMenuImageStruc t = record
BitMap : TBitMap;
end;

TFileStruct = Record
Info : TMenuDataStruct ;
Image : TMenuImageStruc t;
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
2472
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 nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now I have two questions - one is historical, and the other practical. 1.) Surely malloc (and...
2
6395
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) public static extern int EnumUserGroups(UserGroupCallBack callBack, object obj);
2
2441
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 convert to as like as int.parse(obj), bool.parse(obj)..... Is there any good methods force C# can accept objects implicitly convert type object to the specific type without editing all codes.
8
4552
by: Carlos | last post by:
I have a strinf with comma separated field. Is is possible to convert it to a Structure ? Thanks
2
2669
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 either. Could someone please take a look and see if or what I'm doing wrong? Many thanks,
8
6678
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 for (int i = 0; i < arr.Length; i++) {
4
5716
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
1517
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 Borland C++ Borland C# Visual C C Sharp
2
2190
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 would really appreciate it if you could make it as simple as possible. Code convert from VBScript to Sharp C#:
0
9901
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11025
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10371
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4562
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3187
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.