Jump to content
Tuts 4 You

C++ > Delphi help


Departure

Recommended Posts

Just wondering if anyone can help me convert a c++ snippet to delphi, I have attempted myself but my C++ skills are not that good...

Either one of these snippets does the job..


//In Globals
typedef void (__cdecl *lpSetConsoleVariable)(unsigned long console,char* szVal);
lpSetConsoleVariable SetConsoleVariable;//Usage
SetConsoleVariable(0x8003F0,"ShowFps 1");

Or this snippet


typedef int (__cdecl* RunConsoleCommand_t)(char* cmd);
RunConsoleCommand_t pRunConsoleCommand = (RunConsoleCommand_t)0x00485E10;//Usage
pRunConsoleCommand("ShowFPS 1");

Now for my attempts


function Pu****(command: PChar):boolean;cdecl;
var
dwAddress: Dword;
begin
dwAddress:= $00484bc0;
asm
mov eax,command
push eax
push $08003f0
call dwAddress
add esp,8
end;
result := true;
end;//Usage
Pu****(Pchar('ShowFPS 1'));

And


type
TRunConsoleCommand = function(cmd : pchar) : Integer; cdecl;
PRunConsoleCommand = ^TRunConsoleCommand;procedure RunConsoleCommand(Const command : String);
var
RCC : PRunConsoleCommand;
begin
New(RCC); //Allocate Mem
RCC:= pointer($00485E10);
RCC^(Pchar(command));
Dispose(RCC); //Free Mem
end;Also tryed doing it this way...
procedure RunConsoleCommand(Const command : String);
var
RCC : TRunConsoleCommand;
begin
RCC:= TRunConsoleCommand($00485E10);
RCC(Pchar(command));
end//Usage
RunConsoleCommand('ShowFPS 1');

and here is what it looks like at those address's


00485E0F CC INT3
00485E10 /$ 8B4424 04 MOV EAX,[ESP+4]
00485E14 |. 50 PUSH EAX
00485E15 |. 68 F0038000 PUSH 008003F0
00485E1A |. E8 A1EDFFFF CALL 00484BC0
00485E1F |. 83C4 08 ADD ESP,8
00485E22 \. C3 RET
00485E23 CC INT3

As you can see with my first attemp was to just do the exact same thing as what is seen in olly... Once going into the call here is what we have


00484BC0 /$ 8B4424 08 MOV EAX,[ESP+8]
00484BC4 |. 8B4C24 04 MOV ECX,[ESP+4]
00484BC8 |. 6A 00 PUSH 0
00484BCA |. 6A 00 PUSH 0
00484BCC |. 50 PUSH EAX
00484BCD |. 51 PUSH ECX
00484BCE |. E8 2DF8FFFF CALL 00484400
00484BD3 |. 83C4 10 ADD ESP,10

As you might have guessed by now, im looking at trainer and how they work(to learn from) and the only resources I found are in C++ but I have converted to Delphi except this C++ function, Can anyone please help me out on conversion?

Link to comment

The reason it's different when disassembled is because of the compiler (i'm guessing this).

also, SetConsoleVariable is just being predefined lol, so you're going to need to edit SetConsoleVariable itself.

Link to comment

Can you do an example?

by the way the assembly code is from the game itself, and not the C++ or delphi code

then he's just redefining the static import.

try this:

procedure lpSetConsoleVariable( console : Integer; szVal : PChar ); cdecl; external 'XXx.dll/exe';

Link to comment

actually this snippet is injected into the game process so its apart of its process, So i dont need external....

here is what he has


//In Globals
typedef void (__cdecl *lpSetConsoleVariable)(unsigned long console,char* szVal);
lpSetConsoleVariable SetConsoleVariable;//In Present or Endscene
void cBase::RenderFrame(LPDIRECT3DDEVICE9 pDevice)
{
SetConsoleVariable = (lpSetConsoleVariable)(0x0484BC0); if(GetAsyncKeyState(VK_INSERT)&1){
ptc =! ptc;
}
if(ptc){
SetConsoleVariable(0x8003F0,"ShowFps 1");
} else {
SetConsoleVariable(0x8003F0,"ShowFps 0");
}
}

Im not interested about the render frame.. Just more interested what this means in Delphi

First he does this...

SetConsoleVariable = (lpSetConsoleVariable)(0x0484BC0);

Then calls...

SetConsoleVariable(0x8003F0,"ShowFps 1");

to this....

typedef void (__cdecl *lpSetConsoleVariable)(unsigned long console,char* szVal);

lpSetConsoleVariable SetConsoleVariable;

But there is another way also, this way I could read it a little better and tryed to make delphi code based on this....

typedef int (__cdecl* RunConsoleCommand_t)(char* cmd);

RunConsoleCommand_t pRunConsoleCommand = (RunConsoleCommand_t)0x00485E10;

which is called like this...

pRunConsoleCommand("ShowFPS 1");

Now I have no idea how C++ works with pointers ect.. Im just taking for granted that pRunConsoleCommand is pointer and if i remember correctly "*" means pointer also in C++, Anyway could you translate this to delphi? I tryed but im not having much luck due to my poor C++ knowledge.

Link to comment

//In Globals
typedef void (__cdecl *lpSetConsoleVariable)(unsigned long console,char* szVal);
lpSetConsoleVariable SetConsoleVariable;

This is a type definition. He is defining SetConsoleVariable to be equal to a function setup like:

void __cdecl SetConsoleVariable( unsigned long console, char* szVal )
{
// stuff happens..
}

Im not interested about the render frame.. Just more interested what this means in Delphi

First he does this...

SetConsoleVariable = (lpSetConsoleVariable)(0x0484BC0);

Then calls...

SetConsoleVariable(0x8003F0,"ShowFps 1");

The first part is him casting a pointer to SetConsoleVariable to give it a function base. Because its prototyped as a function, you can call it as one if it aligns correctly with a pointer.

The second part is him calling the function he defined with the typedef, based on the pointer he casted it to.

I don't use Delphi so I can't help convert this for you, but hopefully that helps you some about the C++ side.

Link to comment
type
lpSetConsoleVariable = procedure( console: Integer; szVal: PChar ); cdecl;
PSetConsoleVariable = ^lpSetConsoleVariable; // Pointer To lpSetConsoleVariable
SetConsoleVariable = lpSetConsoleVariable;
Edited by rotem156
Link to comment

Combat arms...

I tried "SetConsoleVariable($00484BC0)($008003F0,Pchar('showpfs 1'))" no compile errors but still didn't work. :( Ill keep trying, if you want the source code to dll Im writing I can post it here is you like.

Link to comment

Hey, i think this will work:

type 
lpSetConsoleVariable = procedure( console: Integer; szVal: PChar ); cdecl; procedure Test;
var
SetConsoleVariable: lpSetConsoleVariable;
hInst: THandle;
begin
hInst := LoadLibrary( 'Module.dll/exe' );
@SetConsoleVariable := $00484BC0 / GetProcAddress( hInst,
end;
Link to comment

I have given up trying to convert this to delphi, Next idea is to convert make the C++ into an .obj file and include it with delphi

I tryed the following but I might have the C++ incorrect (using C++ Builder)

C++


#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
//In Globals
typedef void (__cdecl *lpSetConsoleVariable)(unsigned long console,char* szVal);
lpSetConsoleVariable SetConsoleVariable;extern void SetCommand( char* szValue)
{
SetConsoleVariable = (lpSetConsoleVariable)(0x0484BC0);
SetConsoleVariable(0x8003F0,szValue);
}int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}

Compiled it without error and copyed the File1.Obj to the delphi project (library)

and used the following


{$L File1.obj}
Procedure _SetCommand(szValue : PChar); cdecl; external;

An got an error


[Error] DepartureLib.dpr(63): Unsatisfied forward or external declaration: '_SetCommand'

I must be doing something wrong with importing the .obj file or maybe I have the C++ wrong..

Link to comment

As far as I know, you must invoke in this way a function (this is an example), if you are importing from a dll.

function MyFunction(Parameter1 : Cardinal; Parameter2: PChar): Integer;stdcall; external 'MyFile.dll' name 'MyFunction'; // <- This is the name of the function inside the external file

In the case of .obj files I haven't tested yet, but you could try this invoking your .obj file to find if it works...

When invoking from C++ with an external Delphi module, you must include in the name of your exported function the number of bytes used in arguments, like this:

// Declaring in the Delphi module

exports MyFunction name 'MyFunction@8';

I don't know if this number must be included also in your case, that is the reverse of my last example, I really never tested it before.

Good luck with this...

Nacho_dj

Edited by Nacho_dj
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...