Conversion from C++ to Delphi
spacer
Autor Nachricht
randydom
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Sa 07.01.12 15:03 
Could someone helps me in porting this C++ function into Delphi?

sys is a class named TSystem.

SharedMem is a class named TVirtualMemory

ausblenden C# Sourcecode markieren
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
int Process::FixApi(dword FileHandler,image_import_descriptor* Imports,dword dllbase){
image_import_by_name** names;
dword* pointers;
if (Imports->original_first_thunk!=0){
names=(image_import_by_name**)Imports->original_first_thunk;
}else{
names=(image_import_by_name**)Imports->first_thunk;
};
names=(image_import_by_name**)((dword)names+FileHandler);
pointers=(dword*)(Imports->first_thunk + FileHandler);
if (Imports->first_thunk==0)return 0;
for (int i=0;i<200;i++){
if (names[i]==0)break;
if(!((dword)(names[i]->name+FileHandler) & 0x80000000)){
dword s=(dword)names[i]->name;
dword ptr=sys->GetAPI((char*)(s+FileHandler),dllbase);
dword n=this->SharedMem->get_virtual_pointer(ptr);
if (n!=0){
ptr=n;
}
memcpy(&pointers[i],&ptr,4);
}
};
};
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
Werbung ausblenden? Dann registriere Dich kostenlos. Weitere Gründe für eine Registrierung.


Werbung ausblenden? Dann registriere Dich kostenlos. Weitere Gründe für eine Registrierung.
Delphi-Laie
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starhalf offtopic starofftopic star
Beiträge: 501
Erhaltene Danke: 29


Delphi 2-4
BeitragVerfasst: Sa 07.01.12 16:14 
Wie wäre es denn auf Deutsch? Schließlich ist das hier ein deutschsprachiges Forum!
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 15833
Erhaltene Danke: 737

XP, W7 x64 (Chrome, IE9, FF), Debian, (OSX 10.7)
RAD XE 2, Java (NB), C++, C# (VS 2010), JS/HTML, PHP, Lazarus
BeitragVerfasst: Sa 07.01.12 18:29 
user profile iconDelphi-Laie hat folgendes geschrieben Zum zitierten Posting springen:
Wie wäre es denn auf Deutsch? Schließlich ist das hier ein deutschsprachiges Forum!
Not everybody in the world does speak German, so I don't see the problem if someone asks in English. Nearly every developer will understand it. :nixweiss:

user profile iconrandydom hat folgendes geschrieben Zum zitierten Posting springen:
Could someone helps me in porting this C++ function into Delphi?
It would be helpful to know what you need the code for. Do you need a 1:1 translation or do you need a code that does the job?

Without the context I don't see what the code is supposed to do exactly.

Für diesen Beitrag haben gedankt: Dude566
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
randydom Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Sa 07.01.12 18:41 
sorry Delphi-Laie because i don't speak German ( sorry again ).

thank you jaenicke .

i did this attempt :

ausblenden volle Höhe Delphi-Quelltext markieren
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
function FixApi(FileHandler: dword;
Imports: Pimage_import_descriptor; dllbase: dword): Integer;
var
names:^image_import_by_name;
pointers:Pdword;
i:integer;
s,ptr,n:dword;
begin
if Imports^.original_first_thunk<>0 then
names:=Pointer(Imports^.original_first_thunk)
else
names:=Pointer(Imports^.first_thunk);

names:=Pointer(dword(names)+FileHandler);
pointers:=Pdword(Imports^.first_thunk+FileHandler);
if (Imports^.first_thunk=0)then result:=0;

for i:=0 to 200 do
begin
{
// i didn't get it
if (names[i]==0)break;
if(!((dword)(names[i]->name+FileHandler) & 0x80000000)){
dword s=(dword)names[i]->name;
}

ptr:=sys.GetAPI(PChar(s+FileHandler),dllbase);
n:=SharedMem.get_virtual_pointer(ptr);
if n<>0 then
ptr:=n;

CopyMemory(@pointers[i],@ptr,4);

end;
end;



Zitat:
Without the context I don't see what the code is supposed to do exactly


The code is stripped from a machine emulator.


many thanks
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
Delphi-Laie
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starhalf offtopic starofftopic star
Beiträge: 501
Erhaltene Danke: 29


Delphi 2-4
BeitragVerfasst: Sa 07.01.12 18:42 
Dann sollte man aber wenigstens sich kurz dazu äußern, warum man zu der Gastfreundlichkeit, sich in der Sprache des Gastgebers äußern zu können, nicht imstande ist. Soviel Anstand sollte schon sein.

Übrigens, Ankunftsgruß, Bitte und Danke gibt es auch im Englischen, was ich oben auch vermisse.

Ich verstehe Englisch durchaus, werde aber einen Teufel tun, mich auch noch innerhalb meines Sprachraumes in eine Fremdsprache verbiegen zu lassen.
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 15833
Erhaltene Danke: 737

XP, W7 x64 (Chrome, IE9, FF), Debian, (OSX 10.7)
RAD XE 2, Java (NB), C++, C# (VS 2010), JS/HTML, PHP, Lazarus
BeitragVerfasst: Sa 07.01.12 19:08 
user profile iconrandydom hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext markieren
1:
2:
3:
4:
5:
6:
7:
8:
for i:=0 to 200 do
begin
{
// i didn't get it
if (names[i]==0)break;
if(!((dword)(names[i]->name+FileHandler) & 0x80000000)){
dword s=(dword)names[i]->name;
}
Well, in Delphi you don't have the possibility to use the pointer "names" as an array implicitly. The easiest way is just to increase the pointer after each loop. This way you just jump to the next entry and that's all you need.

If you do so you can just use names^ instead of names[i]. Then we have this expression: names[i]->name+FileHandler
What is done in C++ is: take the first character of name as ordinal value ans add it to FileHandler. So in Delphi (dependend on your definition of image_import_by_name use the first index for the name array, 0 or 1):
ausblenden Delphi-Quelltext markieren
1:
Ord(names^.name[1]) + FileHandler

The bitwise and operator & in C++ is in Delphi just and. You can compare the result against 0 as you want to know whether the expression is false in C++ and false is 0.

This should be all I think. ;-)
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starhalf offtopic starofftopic star
Chefentwickler
Beiträge: 18451
Erhaltene Danke: 641

Win 7
C# (VS 2010)
BeitragVerfasst: Sa 07.01.12 19:22 
user profile iconDelphi-Laie hat folgendes geschrieben Zum zitierten Posting springen:
Ich verstehe Englisch durchaus, werde aber einen Teufel tun, mich auch noch innerhalb meines Sprachraumes in eine Fremdsprache verbiegen zu lassen.
Es zwingt Dich niemand, auf Englisch zu schreiben.

In diesem Forum sind englischsprachige User ausdrücklich willkommen. Wenn Du mit solchen Postings nicht klar kommst, ignoriere sie bitte einfach. Eine Diskussion darüber ist in diesem Thread fehl am Platze, ich bitte daher drum, beim Thema zu bleiben.



@randydom: Feel free to continue posting in english. That's absolutely no problem for us! :)

_________________
Physics is a bitch.

Für diesen Beitrag haben gedankt: Dude566, glotzer, jaenicke, Mitmischer 1703, Narses, Oliver Marx, platzwart, Xion
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
randydom Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Sa 07.01.12 19:31 
thank you jaenicke ;

and this how can i read it , because normally its result is a boolean value .

ausblenden C# Sourcecode markieren
1:
2:
3:
if(!((dword)(names[i]->name+FileHandler) & 0x80000000)){
dword s=(dword)names[i]->name;
}


@Christian S : thank you .

@Delphi-Laie : again i'm really sorry .
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 15833
Erhaltene Danke: 737

XP, W7 x64 (Chrome, IE9, FF), Debian, (OSX 10.7)
RAD XE 2, Java (NB), C++, C# (VS 2010), JS/HTML, PHP, Lazarus
BeitragVerfasst: Sa 07.01.12 20:03 
user profile iconrandydom hat folgendes geschrieben Zum zitierten Posting springen:
and this how can i read it , because normally its result is a boolean value .

ausblenden C#-Quelltext markieren
1:
2:
3:
if(!((dword)(names[i]->name+FileHandler) & 0x80000000)){
dword s=(dword)names[i]->name;
}
As I said / meant :mrgreen: ;-)
ausblenden Delphi-Quelltext markieren
1:
2:
if (Ord(names^.name[0]) + FileHandler) and $80000000 = 0 then
s := Ord(names^.name[0]);
// EDIT:
And again: Use 0 or 1 as index according to your definition of name.
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Moderator
Beiträge: 2836
Erhaltene Danke: 182

Win 2000, Win XP
Delphi 7, Turbo Delphi Exp.
BeitragVerfasst: Sa 07.01.12 20:16 
user profile iconjaenicke hat folgendes geschrieben Zum zitierten Posting springen:
As I said / meant :mrgreen: ;-)
ausblenden Delphi-Quelltext markieren
1:
2:
if (Ord(names^.name[0]) + FileHandler) and $80000000 = 0 then
s := Ord(names^.name[0]);
Are you sure this (using Ord) would actually compile? Coincidentally, I tried the exact same thing yesterday and it did not: I had to use a cast to an actual type (in this case DWORD or Cardinal {which are the same}) for it to work.

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
Ich code EdgeMonkey -~==~- #ee-lounge in Freenode
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 15833
Erhaltene Danke: 737

XP, W7 x64 (Chrome, IE9, FF), Debian, (OSX 10.7)
RAD XE 2, Java (NB), C++, C# (VS 2010), JS/HTML, PHP, Lazarus
BeitragVerfasst: Sa 07.01.12 20:27 
Yes, I am absolutely sure. ;-)
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
mandras
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 90
Erhaltene Danke: 9


D6 Prof, XE2 Prof
BeitragVerfasst: So 08.01.12 11:44 
ausblenden Delphi-Quelltext markieren
1:
if(!((dword)(names[i]->name+FileHandler) & 0x80000000))


does the following:
take entry [i] of names, then there look in "field" name at the 4 bytes at position <filehandler>,
use them as DWORD (32Bit-integer) and test if highest bit is set.
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
randydom Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 10.01.12 14:40 
many thanks for you all , but i still need some little help with this :


ausblenden C# Sourcecode markieren
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
dword LoadAndVerifyPE(string filename) {
long unsigned int size = 0;
FileMapping * rawdata = OpenFile(filename.c_str());
if (rawdata == 0) {
return 0;
}
image_header * FileHeader;
dword FileHandler, PEHeaderPtr;
image_section_header * ImgSecH;
dword imagebase;
FileHandler = rawdata->BaseAddress;
if (!(*((short *) FileHandler) == 0x5a4d)) {
return 0;
}
PEHeaderPtr = ((dos_header *) FileHandler)->e_lfanew + FileHandler;
if (!(*((short *) PEHeaderPtr) == 0x4550)) {
return 0;
}
FileHeader= (image_header *) PEHeaderPtr;


i did this in Delphi :
ausblenden Delphi Sourcecode markieren
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
function LoadAndVerifyPE(filename: string):Integer;
var
rawdata: pFileMapping;
FileHeader: pimage_header;
FileHandler: dword;
data: pimage_section_header;
imagebase: dword;
sections: pimage_section_header;
aligned_ptr: dword;
src: dword;
dest: dword;
BaseAddress_:Pointer;
PEHeaderPtr:dword;
begin
size:=0;
Result:=0;
//***************************************** from here
rawdata:=OpenFile_(PChar(filename));//--> OpenFile_() function is successfully declared and used
if (rawdata=nil)Then
Result:= 0;
FileHandler:= rawdata.BaseAddress^;
//***************************************** to here all goes well

// but from here ...??????
if (FileHandler<>$5a4d) then result:=0;
PEHeaderPtr:=pDos_header(FileHandler)^.e_lfanew+FileHandler;
if PEHeaderPtr<>$4550 then result:=0;
FileHeader:=pimage_header(PEHeaderPtr);


What about this :

ausblenden Delphi Sourcecode markieren
1:
2:
3:
4:
if (FileHandler<>$5a4d) then result:=0;
PEHeaderPtr:=pDos_header(FileHandler)^.e_lfanew+FileHandler;
if PEHeaderPtr<>$4550 then result:=0;
FileHeader:=pimage_header(PEHeaderPtr);
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 15833
Erhaltene Danke: 737

XP, W7 x64 (Chrome, IE9, FF), Debian, (OSX 10.7)
RAD XE 2, Java (NB), C++, C# (VS 2010), JS/HTML, PHP, Lazarus
BeitragVerfasst: Di 10.01.12 15:03 
You have to call Exit in addition to setting result to zero, 'cause in Delphi you do not exit the function when assigning the result.

In newer versions of Delphi you can just write Exit(0); instead of result := 0;. Before about Delphi 2009 you have to assign 0 to result and call Exit afterwards.
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
randydom Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 10.01.12 15:16 
user profile iconjaenicke wrote Zum zitierten Posting springen:
You have to call Exit in addition to setting result to zero, 'cause in Delphi you do not exit the function when assigning the result.

In newer versions of Delphi you can just write Exit(0); instead of result := 0;. Before about Delphi 2009 you have to assign 0 to result and call Exit afterwards.

@ jaenicke thank you for this valuable information .

just a help about this :

ausblenden C# Sourcecode markieren
1:
2:
3:
4:
5:
6:
7:
8:
if (!(*((short *) FileHandler) == 0x5a4d)) {
return 0;
}
PEHeaderPtr = ((dos_header *) FileHandler)->e_lfanew + FileHandler;
if (!(*((short *) PEHeaderPtr) == 0x4550)) {
return 0;
}
FileHeader= (image_header *) PEHeaderPtr;
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 15833
Erhaltene Danke: 737

XP, W7 x64 (Chrome, IE9, FF), Debian, (OSX 10.7)
RAD XE 2, Java (NB), C++, C# (VS 2010), JS/HTML, PHP, Lazarus
BeitragVerfasst: Di 10.01.12 15:37 
user profile iconrandydom hat folgendes geschrieben Zum zitierten Posting springen:
just a help about this :
Your code looked quite good. I thought you just had to correct the translation of return. :?:
If it does not work, what does happen?
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
randydom Threadstarter
Hält's aus hier
Beiträge: 8



BeitragVerfasst: Di 10.01.12 15:51 
Ok jaenicke look at this

ausblenden Delphi Sourcecode markieren
1:
2:
3:
4:
if (FileHandler<>$5a4d) then result:=0;
PEHeaderPtr:=pDos_header(FileHandler)^.e_lfanew+FileHandler;
if PEHeaderPtr<>$4550 then result:=0;
FileHeader:=pimage_header(PEHeaderPtr);



i get an exception near :
ausblenden Delphi Sourcecode markieren
1:
PEHeaderPtr:=pDos_header(FileHandler)^.e_lfanew+FileHandler;


i think the problem is when assigning FileHandler(FileHandler=dword) to pDos_header and the result is PEHeaderPtr(PEHeaderPtr=dword).


Or if possible can you re-convert this code :

ausblenden C# Sourcecode markieren
1:
2:
3:
4:
5:
PEHeaderPtr = ((dos_header *) FileHandler)->e_lfanew + FileHandler;
if (!(*((short *) PEHeaderPtr) == 0x4550)) {
return 0;
}
FileHeader= (image_header *) PEHeaderPtr;
 
Antworten mit Zitat Beitrag melden
Private Nachricht sendenPosting in privater Nachricht zitieren
home home