Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi CPP nach Delphi Übersetzung (https://www.delphipraxis.net/147564-cpp-nach-delphi-uebersetzung.html)

Gehstock 11. Feb 2010 14:04


CPP nach Delphi Übersetzung
 
Delphi-Quellcode:
BOOL GetProcessOf( char exename[], PROCESSENTRY32 *process )
{
   process->dwSize = sizeof( PROCESSENTRY32 );

   HANDLE handle = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

   if ( Process32First( handle, process ) )
      while ( Process32Next( handle, process ) )
         if ( strcmpi( process->szExeFile, exename ) == 0 )
         {
            CloseHandle( handle );
            return TRUE;
         }

   CloseHandle( handle );
   return FALSE;
}

DWORD GetModuleBaseAddress( DWORD dwProcID, char* szModule )
{
   MODULEENTRY32 xModule;

   HANDLE hSnap = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwProcID );

   xModule.dwSize = sizeof( MODULEENTRY32 );

   if( Module32First( hSnap, &xModule ) )
      while( Module32Next( hSnap, &xModule ) )
         if( strcmp( xModule.szModule, szModule ) == 0 )
         {
            CloseHandle( hSnap );
            return (DWORD)*xModule.modBaseAddr;
         }

   CloseHandle( hSnap );
   return 0;
}
hab ich versucht mit

Delphi-Quellcode:
Function GetProcessOf(process: PROCESSENTRY32;exename: Array of Char): Boolean;
var
  HANDLE: THANDLE;
begin
  Result := FALSE;
  process.dwSize := sizeof(PROCESSENTRY32);
  HANDLE := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if (Process32First(HANDLE, process)) then
    while (Process32Next(HANDLE, process)) do
      if (strcomp(process.szExeFile, @exename) = 0) then
      Begin
        CloseHandle(HANDLE);
        Result := TRUE;
      End;
  CloseHandle(HANDLE);
  Result := FALSE;
End;

Function GetModuleBaseAddress(dwProcID: Dword;szModule: Array of Char): Dword;
var
  xModule: MODULEENTRY32;
  hSnap: THANDLE;
begin
  hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcID);
  xModule.dwSize := sizeof(MODULEENTRY32);
  if (Module32First(hSnap, &xModule)) then
    while (Module32Next(hSnap, &xModule)) do
      if (strcomp(xModule.szModule, @szModule) = 0) then
      Begin
        CloseHandle(hSnap);
        Result := Dword (xModule.modBaseAddr);
      End;
  CloseHandle(hSnap);
  Result := 0;
end;
zu übersetzen

aufgerufen werden soll das ganze So
Delphi-Quellcode:
procedure TForm2.Button1Click(Sender: TObject);
var
pe32 : PROCESSENTRY32;
dwEngineBase,dwClientBase,dwBytes,dwProcID : Dword;
hProcess : THandle;
begin
 while not ( GetProcessOf('hl2.exe', &pe32)) Sleep(100);
     
dwProcID := pe32.th32ProcessID;
hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcID);
if (hProcess = INVALID_HANDLE_VALUE)then Exit;//zeige Fehler

// This seems to crash on Vista, which I'm running atm. works on XP.
dwEngineBase := GetModuleBaseAddress( dwProcID, 'engine.dll' );
dwClientBase := GetModuleBaseAddress( dwProcID, 'client.dll' );
// Putting manual addresses right now instead.
//dwEngineBase := $20000000;
//dwClientBase := $24000000;
dwBytes := 0;
end;
kann sich das Bitte mal jemand anschauen was da nicht rundläuft bei den beiden Funktionen

Jaynder 11. Feb 2010 14:22

Re: CPP nach Delphi Übersetzung
 
- cancel - war auf dem falschen Dampfer

hsg 11. Feb 2010 14:25

Re: CPP nach Delphi Übersetzung
 
Das erste was mir auffällt, ist das die Parameterlisten nicht übereinstimmen:
Dein GetProcessOF hat als ersten Parameter die Proc-Struktur, dann den Exenamen. Aufrufen tust du es mit genau vertauschten Parametern. Beides ist Delphi-Sourcecode, also müssen auch die Parameter in der gleichen Reihenfolge stehen.

Gehstock 11. Feb 2010 14:40

Re: CPP nach Delphi Übersetzung
 
ja ich hatte da was unterdessen geändert und dann vertauscht zumindest die Erste funktion hab ich jetzt hinbekommen

Jaynder 11. Feb 2010 14:41

Re: CPP nach Delphi Übersetzung
 
dto.

himitsu 11. Feb 2010 14:53

Re: CPP nach Delphi Übersetzung
 
Delphi-Quellcode:
Function GetModuleBaseAddress(dwProcID: Dword; szModule: PChar): LongWord;
Function GetProcessOf(exename: PChar; var process: PROCESSENTRY32): LongBool;
PS: C++ kennt die delphieigenen dynamischen Arrays nicht, also kann "array of" niemals stimmen.

Dieses geht auch, da der String Intern teilweise wie ein PChar aussieht.
Delphi-Quellcode:
Function GetProcessOf(const exename: String; var process: PROCESSENTRY32): LongBool;
Bei GetModuleBaseAddress geht es aber nicht, da der "String" dort ein ausgabeparameter ist.
Und weil PChar praktisch nur eine Teilmenge des Strings ist.

Also String > PChar geht, aber PChar > String nicht, wenn man die Schnittstelle nicht änder will/darf.

Gehstock 11. Feb 2010 14:59

Re: CPP nach Delphi Übersetzung
 
Ok habs etwas umgestellt und jetzt scheint es zu klappen

Delphi-Quellcode:
function GetProcessID(Exename: string): DWord;
var
  hProcSnap: THandle;
  pe32: TProcessEntry32;
begin
  result := 0;
  hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  if hProcSnap <> INVALID_HANDLE_VALUE then
  begin
    pe32.dwSize := SizeOf(ProcessEntry32);
    if Process32First(hProcSnap, pe32) = true then
    begin
      while Process32Next(hProcSnap, pe32) = true do
      begin
        if pos(Exename, pe32.szExeFile) <> 0 then
          result := pe32.th32ProcessID;
      end;
    end;
    CloseHandle(hProcSnap);
  end;
end;

Function GetModuleBaseAddress(dwProcID: DWord; szModule: pChar): Cardinal;
var
  xModule: TMODULEENTRY32;
  hSnap: THandle;
begin
  hSnap := CreateToolHelp32SnapShot(TH32CS_SNAPMODULE, dwProcID);
  xModule.dwSize := SizeOf(MODULEENTRY32);
  Module32First(hSnap, xModule);
  repeat
    if LowerCase(xModule.szModule) = LowerCase(szModule) then
    begin
      result := Cardinal(xModule.modBaseAddr);
      break;
    end;
  until (not(Module32Next(hSnap, xModule)));
  CloseHandle(hSnap);
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  pe32: TProcessEntry32;
  dwClientBase, dwBytes, dwProcID: DWord;
  dwEngineBase : Cardinal;
  hProcess: THandle;
begin
  dwProcID := GetProcessID('xxx.exe');
  if dwProcID <> 0 then
  begin
    // Showmessage(inttostr(dwProcID));
    hProcess := OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcID);
    // Showmessage(inttostr(hProcess));
    if (hProcess = INVALID_HANDLE_VALUE) then
    begin
      Showmessage('Finde Handle nicht');
      exit;
    end;
    // This seems to crash on Vista, which I'm running atm. works on XP.
    dwEngineBase := GetModuleBaseAddress(dwProcID, 'xxxx.exe');
    Showmessage(inttostr(dwEngineBase));
    // dwClientBase := GetModuleBaseAddress( dwProcID, 'xxxxx.dll' );
    // Putting manual addresses right now instead.
    // dwEngineBase := $20000000;
    // dwClientBase := $24000000;
    dwBytes := 0;
  end
  else
  begin
    Showmessage('Finde Prozess nicht');
  end;
end;

himitsu 11. Feb 2010 15:05

Re: CPP nach Delphi Übersetzung
 
Zitat:

Delphi-Quellcode:
if pos(Exename, pe32.szExeFile) <> 0 then

strcmp ist aber
Delphi-Quellcode:
if Exename = pe32.szExeFile then
Zitat:

Delphi-Quellcode:
if LowerCase(xModule.szModule) = LowerCase(szModule) then

ist so bestimmt übersichtlicher
Delphi-Quellcode:
if AnsiSameText(xModule.szModule, szModule) then


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:27 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