Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

convert c to dephi

RipperRipper Member Posts: 85
hi there ...

i need some help.
i got code in c, and need to convert it into delphi. hope anyone can help. here is the c code :

#include
#include
#include "psapi.h"

void PrintProcessNameAndID( DWORD processID )
{
char szProcessName[MAX_PATH] = "unknown";

// Get a handle to the process.

HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );

// Get the process name.

if ( hProcess )
{
HMODULE hMod;
DWORD cbNeeded;

if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName) );
}
}

// Print the process name and identifier.

printf( "%s (Process ID: %u)
", szProcessName, processID );

CloseHandle( hProcess );
}

void main( )
{
// Get the list of process identifiers.

DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;

if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;

// Calculate how many process identifiers were returned.

cProcesses = cbNeeded / sizeof(DWORD);

// Print the name and process identifier for each process.

for ( i = 0; i < cProcesses; i++ )
PrintProcessNameAndID( aProcesses[i] );
}

search on your pc for the file psapi.pas ... the procedure it calls are in there.

hope anyone can help ...

regards
ripper.

Comments

  • zibadianzibadian Member Posts: 6,349
    My C is not very good, but here is an untested code in Delphi.
    [code]
    program Project1;
    {$APPTYPE CONSOLE}

    procedure PrintProcessNameAndID(processID: THandle);
    var
    szProcessName: string;
    hProcess: THandle;
    hMod: THandle;
    cbNeeded: Cardinal;
    begin
    hProcess := OpenProcess(PROCESS_VM_READ, false, processID);
    // Get the process name.
    if hProcess<>0 then begin
    if EnumProcessModules(hProcess, @hMod, sizeof(hMod), cbNeeded) then begin
    GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName));
    end;
    end;
    // Print the process name and identifier.

    writeln(Format('%s (Process ID: %d)',[szProcessName, processID]);
    CloseHandle(hProcess);
    end;

    var
    aProcesses[0..1023]: array[0..1023 of Cardinal;
    i, cbNeeded, cProcesses: Cardinal;
    begin
    // Get the list of process identifiers.
    if EnumProcesses(@aProcesses, sizeof(aProcesses), cbNeeded) then
    Exit;
    // Calculate how many process identifiers were returned.
    cProcesses = cbNeeded div sizeof(Cardinal);
    // Print the name and process identifier for each process.
    for i:=0 to cProcesses-1 do
    PrintProcessNameAndID(aProcesses[i]);
    end;
    [/code]
    This code is a console application, but can easily be converted to a GUI program. You then need to move the program body to a procedure and change the writeln to something more appropriate.
  • Shawn CarterShawn Carter Member Posts: 0

    | http://forcoder.org | free ebooks and video tutorials about | Ruby, Delphi, R, PHP, Perl, Visual Basic, Go, Java, Assembly, C#, JavaScript, Scratch, Visual Basic .NET, PL/SQL, Python, Objective-C, MATLAB, Swift, C++, C Bash, SAS, Julia, Transact-SQL, Prolog, Clojure, Rust, Alice, Erlang, Dart, VBScript, Lua, Awk, Logo, ABAP, ML, Scala, Scheme, COBOL, Fortran, Kotlin, Lisp, LabVIEW, FoxPro, Hack, F#, Ada, Apex, D, Crystal |

Sign In or Register to comment.