Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++/DLL]Crashing when injected

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
shakib187
Expert Cheater
Reputation: 0

Joined: 24 May 2007
Posts: 215

PostPosted: Mon Apr 28, 2014 5:21 am    Post subject: [C++/DLL]Crashing when injected Reply with quote

Code:

#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>
#include <float.h>
using namespace std;

void block()
{
   mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
   Sleep(1);
   mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
}

DWORD WINAPI LoopFunction( LPVOID lpParam )
{
   HANDLE phandle = GetCurrentProcess();
   DWORD BaseAddr = (DWORD)GetModuleHandleA("entus.exe");
   DWORD painted = 0x017234238;
   DWORD pointer2 = BaseAddr+painted;
   DWORD thesecond = (DWORD)(*(DWORD*)pointer2 + 0x17c);
   DWORD thethird = (DWORD)(*(DWORD*)thesecond + 0x030);
   DWORD thefourth = (DWORD)(*(DWORD*)thethird + 0x05D0); 
   DWORD theresult = (DWORD)(*(DWORD*)thefourth + 0x4E8); 
   DWORD pointed;
   pointed = *(DWORD*)theresult;

    while(1) {
      if (GetAsyncKeyState(VK_HOME)&1)
      if ((*(DWORD*)pointed) == 13)
      {
      (block());
      }
      }   
    Sleep(1);
    return 0;
}

BOOL WINAPI DllMain (HINSTANCE hModule, DWORD dwAttached, LPVOID lpvReserved)
{
    if (dwAttached == DLL_PROCESS_ATTACH) {
        CreateThread(NULL,0,&LoopFunction,NULL,0,NULL);
    }
    return 1;
}


Anyone know what is wrong?
Back to top
View user's profile Send private message
zm0d
Master Cheater
Reputation: 7

Joined: 06 Nov 2013
Posts: 423

PostPosted: Mon Apr 28, 2014 10:09 am    Post subject: This post has 1 review(s) Reply with quote

Didn't got deep in your code but on my first opinion there's probably something wrong with your pointer calculation. Use SteveAndrews method to calculate multi-level pointers:

Code by SteveAndrew he gave me once:
Code:
void *GetPointerSafest(DWORD PointerBase, ...)
{
   DWORD CurrentPointer = (PointerBase + (DWORD)GetModuleHandle(0));
   DWORD LastOffset = 0, BytesRead;
   va_list Params;
   va_start(Params, PointerBase);
   for(;;)
   {
      unsigned int CurrentOffset = va_arg(Params, unsigned int);
      if(CurrentOffset != 0xffffffff)
      {
         CurrentPointer += LastOffset;
         ReadProcessMemory((HANDLE)-1, (void*)CurrentPointer, (void*)&CurrentPointer, 4, &BytesRead);
         if(BytesRead != 4 || CurrentPointer == 0) //failed reading or we ran into null pointer!
         {
            CurrentPointer = 0;
            break;
         }
         LastOffset = CurrentOffset;
      }
      else
      {
         CurrentPointer += LastOffset;
         break;
      }
   }
   va_end(Params);

   return (void*)CurrentPointer;
}


It's ease to use and works correct.

Edit:

Also I'd prefer to use GetModuleHandle(0) when using an injected DLL since this will always return your executables base address. =)

Regards,
zm0d
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites