Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Die Delphi-IDE (https://www.delphipraxis.net/62-die-delphi-ide/)
-   -   Conversion from Cpp to Delphi (https://www.delphipraxis.net/165372-conversion-cpp-delphi.html)

sdean 27. Dez 2011 03:08

Conversion from Cpp to Delphi
 
Can someone helps me in converting these two CPP functions into Delphi :
Code:
 bool StrSearch(byte* mem,char* text,DWORD size){
       byte* pchL=mem;
       DWORD offset=(DWORD)pchL;
       DWORD length=offset+size;
       int s=strlen(text);
       while(offset<length){
          if (!strnicmp((PCSTR)pchL, (PCSTR)text, s)){
             return true;
          }
          offset++;
          pch++;
       }
       return false;
    }
Code:
int PEFILE::GetPESectionIndex(char* SectionName){
       int j;
       int n=image_nt_headers->FileHeader.NumberOfSections;
       for(j=0;j<n;j++){
          if(!strcmp((const char*)image_section_header[j]->Name,SectionName)) return j;
       }
       return -1;
    }

many thanks

sdean 27. Dez 2011 14:59

AW: Conversion from Cpp to Delphi
 
for the 1st function is did this :

Delphi-Quellcode:
function StrSearch(mem: pbyte;text: pchar; size: DWORD):Boolean;
    var
    pchL: pbyte;
    offset: DWORD;
    length: DWORD;
    iMax: integer;
    begin
      pchL:=mem;
      offset:=DWORD(pchL);
      length:=offset+size;
      iMax:=lstrlen(text);
      while(offset<length) do
      begin
              if StrLIComp(PChar(pchL),PChar(text),iMax)=0  then
              begin
                 result:= true;
                 Break;
               end;
               inc(offset);
              inc(pchL);
            end;
           result:= false;
    end;
is that correct ?

implementation 27. Dez 2011 15:32

AW: Conversion from Cpp to Delphi
 
Zitat:

Zitat von sdean (Beitrag 1143328)
is that correct ?

Not at all:

Delphi-Quellcode:
function StrSearch(mem: pbyte;text: pchar; size: DWORD):Boolean;
var
  pchL: pbyte;
  offset: DWORD;
  length: DWORD;
  iMax: integer;
begin
  pchL := mem;
  offset := DWORD(pchL);
  length := offset + size;
  iMax := lstrlen(text);
  while offset < length do
  begin
    if StrLIComp(PChar(pchL),PChar(text),iMax)=0 then
    begin
      result:= true; // Here you set Result := True
      Break;        // .. and then jump out of the loop ...
    end;
    inc(offset);
    inc(pchL);
  end;
  // ... to here!
  result:= false; // and set Result := False
end;
So your function will always return false.
Replace "break" by "exit" and it should work.

implementation 27. Dez 2011 15:39

AW: Conversion from Cpp to Delphi
 
Zitat:

Zitat von sdean (Beitrag 1143241)
Code:
int PEFILE::GetPESectionIndex(char* SectionName){
       int j;
       int n=image_nt_headers->FileHeader.NumberOfSections;
       for(j=0;j<n;j++){
          if(!strcmp((const char*)image_section_header[j]->Name,SectionName)) return j;
       }
       return -1;
    }

I'll try it:
Delphi-Quellcode:
function TPEFile.GetPESectionIndex(SectionName: PChar): Integer;
var j,n: Integer;
begin
  n := image_nt_headers.FileHeader.NumberOfSections;
  for j := 0 to n-1 do
    if StrLIComp(PChar(image_section_header[j].Name),SectionName)=0 then
    begin
      Result := j;
      exit;
    end;
  Result := -1;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:56 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz