Departure Posted November 9, 2010 Posted November 9, 2010 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;//UsageSetConsoleVariable(0x8003F0,"ShowFps 1");Or this snippettypedef int (__cdecl* RunConsoleCommand_t)(char* cmd);RunConsoleCommand_t pRunConsoleCommand = (RunConsoleCommand_t)0x00485E10;//UsagepRunConsoleCommand("ShowFPS 1");Now for my attemptsfunction 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'));Andtype 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 Memend;Also tryed doing it this way...procedure RunConsoleCommand(Const command : String);var RCC : TRunConsoleCommand;begin RCC:= TRunConsoleCommand($00485E10); RCC(Pchar(command));end//UsageRunConsoleCommand('ShowFPS 1');and here is what it looks like at those address's00485E0F CC INT300485E10 /$ 8B4424 04 MOV EAX,[ESP+4]00485E14 |. 50 PUSH EAX00485E15 |. 68 F0038000 PUSH 008003F000485E1A |. E8 A1EDFFFF CALL 00484BC000485E1F |. 83C4 08 ADD ESP,800485E22 \. C3 RET00485E23 CC INT3As 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 have00484BC0 /$ 8B4424 08 MOV EAX,[ESP+8]00484BC4 |. 8B4C24 04 MOV ECX,[ESP+4]00484BC8 |. 6A 00 PUSH 000484BCA |. 6A 00 PUSH 000484BCC |. 50 PUSH EAX00484BCD |. 51 PUSH ECX00484BCE |. E8 2DF8FFFF CALL 0048440000484BD3 |. 83C4 10 ADD ESP,10As 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?
0xFF Posted November 10, 2010 Posted November 10, 2010 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.
Departure Posted November 10, 2010 Author Posted November 10, 2010 Can you do an example?by the way the assembly code is from the game itself, and not the C++ or delphi code
0xFF Posted November 10, 2010 Posted November 10, 2010 Can you do an example?by the way the assembly code is from the game itself, and not the C++ or delphi codethen he's just redefining the static import.try this:procedure lpSetConsoleVariable( console : Integer; szVal : PChar ); cdecl; external 'XXx.dll/exe';
Departure Posted November 10, 2010 Author Posted November 10, 2010 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 Endscenevoid 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 DelphiFirst 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.
atom0s Posted November 10, 2010 Posted November 10, 2010 //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 DelphiFirst 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.
0xFF Posted November 10, 2010 Posted November 10, 2010 (edited) type lpSetConsoleVariable = procedure( console: Integer; szVal: PChar ); cdecl; PSetConsoleVariable = ^lpSetConsoleVariable; // Pointer To lpSetConsoleVariable SetConsoleVariable = lpSetConsoleVariable; Edited November 10, 2010 by rotem156
Departure Posted November 11, 2010 Author Posted November 11, 2010 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.
0xFF Posted November 12, 2010 Posted November 12, 2010 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;
Departure Posted November 16, 2010 Author Posted November 16, 2010 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 delphiI 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 Globalstypedef 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..
Nacho_dj Posted November 18, 2010 Posted November 18, 2010 (edited) 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 fileIn 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 moduleexports 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 November 18, 2010 by Nacho_dj
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now