Solved

Delphi to C++ (Few Lines)

Posted on 2007-03-20
16
399 Views
Last Modified: 2011-09-20
Hello,

Can anyone quickly help converting these few lines of Delphi to C++

type
  TSections = array [0..0] of TImageSectionHeader;

var
PSections: ^TSections;

So in C++, TImageSectionHeader is IMAGE_SECTION_HEADER, and would be like this
PSections* TSections; and maybe the type gets changed into a struct? Gah im so confused, help? :P

Cheers.
0
Comment
Question by:-Karamja-
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18757152
I don't know enough Delphi to know what this means :

    array [0..0]

an array of size 0 ???

In any case, I think that it might be something like this in C++ :

    typedef TImageSectionHeader* TSections

    TSections *PSections;

Again, I can't be sure since I don't know enough Delphi.
0
 
LVL 86

Expert Comment

by:jkr
ID: 18758729
Actually, if I remember my PASCAL times correctly, that should be

typedef IMAGE_SECTION_HEADER TSections;

TSections* pSections;

If you want to create a pointer to a IMAGE_SECTION_HEADER structure.
0
 
LVL 2

Author Comment

by:-Karamja-
ID: 18759121
Yes, just playing around with it and
typedef IMAGE_SECTION_HEADER TSections;

Seems to be close, but what about the array part?
0
 
LVL 86

Expert Comment

by:jkr
ID: 18759359
In C/C++ arrays and pointers are (well almost) interchangeable, so that should not play a role here. E.g.

int array[10];

*array = 0;

int n = *array

is the same as

array[0] = 0;

int n = array[0];
0
 
LVL 2

Author Comment

by:-Karamja-
ID: 18759688
Ok thanks for your reply guys,

I cant do anymore testing until tomorrow, I will get back to you then.
0
 
LVL 86

Expert Comment

by:jkr
ID: 18759836
Oh, until tomorrow, you'll probably find some essays as comments that deal with the philosophic depth of the array/pointer similarity and borderline cases where that might be dangerous ;o)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18760205
jkr, do you know what this means in Delphi ?

    array [0..0]

I assumed that it was a 0-length array, and thus defined it as a pointer only.
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18762606
array[0..0] means array of any length (but you need range checking off so no range check error is thrown in run-time)

basically with delphi you would use array of ... if you want dynamic variable length array, array[0..0] is used with windows API.

ziolko.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 53

Expert Comment

by:Infinity08
ID: 18762745
ziolko, for this :

    type
      TSections = array [0..0] of TImageSectionHeader;

would the equivalent in C++ be :

    typedef TImageSectionHeader* TSections

or :

    typedef TImageSectionHeader TSections;

???
0
 
LVL 21

Expert Comment

by:ziolko
ID: 18762773
i'm not that good in c++ but in delphi i've got:

  PDS_SELECTION_LIST = ^DS_SELECTION_LIST;
  DS_SELECTION_LIST = record
    cItems: ULONG;
    cFetchedAttributes: ULONG;
    aDsSelection: array[0..0] of DS_SELECTION;
  end;

and according to MDSN it's equivalent to:

typedef struct _DS_SELECTION_LIST {  ULONG cItems;  ULONG cFetchedAttributes;  DS_SELECTION aDsSelection[ANYSIZE_ARRAY];
} DS_SELECTION_LIST, *PDS_SELECTION_LIST;


maybe this will help you somehow

ziolko.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18762836
That fits my understanding of the Delphi code.

That means that my original hunch :

    typedef TImageSectionHeader* TSections

    TSections *PSections;

seems correct.
So, TSections is a dynamically-sized array of TImageSectionHeader
And PSections is a pointer to such an array.


Can anyone confirm that ?
0
 
LVL 2

Author Comment

by:-Karamja-
ID: 18762866
But isnt,

TImageSectionHeader* same as PTImageSectionHeader
or
IMAGE_SECTION_HEAEDER* = PIMAGE_SECTION_HEADER

And im just looking for IMAGE_SECTION_HEADER with my var as a pointer to it, Anyway,
I will do some testing in a minute and see what happens.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18762991
>> IMAGE_SECTION_HEAEDER* = PIMAGE_SECTION_HEADER

Well, in C++, that would be :

    typedef IMAGE_SECTION_HEAEDER* PIMAGE_SECTION_HEADER

which is the same as (just different names) :

    typedef TImageSectionHeader* TSections



And then used as :

    PIMAGE_SECTION_HEADER *PSections;

which is the same as (just different names) :

    TSections *PSections;



Am I missing something ?
0
 
LVL 2

Author Comment

by:-Karamja-
ID: 18763055
Lets just go back to the Delphi code,

type
  TSections = array [0..0] of TImageSectionHeader;

var
PSections: ^TSections;

^ is the same as * in C++, and TImageSectionHeader is the same as IMAGE_SECTION_HEADER.
So what im trying to get, is a pointer of var that is a type of IMAGE_SECTION_HEADER.
What your saying is, pointer of var that is that is a type of PIMAGE_SECTION_HEADER.

Remembering that PIMAGE_SECTION_HEADER is just IMAGE_SECTION_HEADER*

This so confusing :( maybe I am missing something too?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18763264
Yes, but TSections is not defined as a single TImageSectionHeader, but as an array of TImageSectionHeader (of undefined length).

In C++, and array can be defined like this :

    TImageSectionHeader  array[10];

this defines an array of 10 TImageSectionHeader. The problem here is that we need to know how big the array is gonna be.
Since Delphi uses a dynamic array (of undefined length), we most likely need to do the same thing in C++.

A dynamic array in C++ is done like this :

    TImageSectionHeader  *array;

now, we can allocate memory for that array once we know how much elements we want (say we want 10) :

    array = new TImageSectionHeader[10];
    // use the array
    delete [] array;

That's why I (with my limited knowledge of Delphi) used this typedef :

    typedef TImageSectionHeader* TSections

ie. TSections is defined as a dynamic array (of undefined length) of TImageSectionHeader
0
 
LVL 2

Author Comment

by:-Karamja-
ID: 18763328
Ahh, that seems to fit.
Sorry for all the confusion, I should be able to make tests soon and will get back to everyone then.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now