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# and C++]Reading a pointer
Goto page 1, 2  Next
 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Fri May 29, 2009 10:35 pm    Post subject: [C# and C++]Reading a pointer Reply with quote

Code:
   __inline ULONG_PTR ReadPointer(ULONG_PTR* ulBase, INT nOffset)
   {
      if ( !IsBadReadPtr((VOID*)ulBase, sizeof(ULONG_PTR)) )
         if ( !IsBadReadPtr((VOID*)((*(ULONG_PTR*)ulBase)+nOffset), sizeof(ULONG_PTR)) )
            return *(ULONG_PTR*)((*(ULONG_PTR*)ulBase)+nOffset);
      return 0;
   }


That is a code I found for reading a pointer in C++

However I can't get it to work (converting it) for C#.

I do know what I am doing, but I THINK that C#'s limitations with pointers is what's stopping me.

Code:
/*Pointer: 33605FD8
Offset: 65c*/


There's the pointer and offset, if you could please show me how to display it in C#.

This is what I've come up with:

Code:
ulong* ptr = (ulong*)((ulong)0x33605FD8 + (ulong)0x65C);

Console.WriteLine((int)ptr);


Heh, I'm still trying to learn these types of things, with pointers and such.

I'm still learning about C++, so I'm doing everything in C#

I tried this in VS 2008 and it gives me errors:
Code:
#include <Windows.h>
#include <iostream>
#include <string>

int main()
{
        std::cout << Ptr::ReadPointer(0x33605FD8, 0x65C);

   return 0;
}

class Ptr
{
public:
   __inline ULONG_PTR ReadPointer(ULONG_PTR* ulBase, INT nOffset)
   {
      if ( !IsBadReadPtr((VOID*)ulBase, sizeof(ULONG_PTR)) )
         if ( !IsBadReadPtr((VOID*)((*(ULONG_PTR*)ulBase)+nOffset), sizeof(ULONG_PTR)) )
            return *(ULONG_PTR*)((*(ULONG_PTR*)ulBase)+nOffset);
      return 0;
   }
protected:
private:
};


Gives me errors:
Code:
Error   1   error C2653: 'Ptr' : is not a class or namespace name   c:\Users\Aaron\Documents\Visual Studio 2008\Projects\PixelColor\PixelColor\main.cpp   27   PixelColor
Error   2   error C3861: 'ReadPointer': identifier not found   c:\Users\Aaron\Documents\Visual Studio 2008\Projects\PixelColor\PixelColor\main.cpp   27   PixelColor


And I DO know what they are talking about, but whatever I do, the errors remain.

The solution name is a long story btw.

Thank you =D

_________________


Last edited by yoyonerd on Fri May 29, 2009 10:54 pm; edited 1 time in total
Back to top
View user's profile Send private message AIM Address
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Fri May 29, 2009 10:48 pm    Post subject: Reply with quote

move your class definition to above your main function
Back to top
View user's profile Send private message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Fri May 29, 2009 10:56 pm    Post subject: Reply with quote

now it compiles but it always returns 0
_________________
Back to top
View user's profile Send private message AIM Address
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Sat May 30, 2009 7:07 am    Post subject: Reply with quote

yoyonerd wrote:
now it compiles but it always returns 0


um. Try reading the pointer with cheatengine. If that fails then you know that your pointer is incorrect. If it does work, then there is a problem with your code.
Back to top
View user's profile Send private message
talkerzero
Grandmaster Cheater
Reputation: 1

Joined: 24 Jul 2008
Posts: 560
Location: California

PostPosted: Sat May 30, 2009 10:48 am    Post subject: Reply with quote

Code:
__inline ULONG_PTR ReadPointer(ULONG_PTR* ulBase, INT nOffset)
{
   if ( !IsBadReadPtr((VOID*)ulBase, sizeof(ULONG_PTR)) )
   {
      if ( !IsBadReadPtr((VOID*)((*(ULONG_PTR*)ulBase)+nOffset), sizeof(ULONG_PTR)) )
         return *(ULONG_PTR*)((*(ULONG_PTR*)ulBase)+nOffset);
      return -1;
   }
   return 0;
}

Use that to figure out where the function's failing.
Back to top
View user's profile Send private message Visit poster's website
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Sat May 30, 2009 12:15 pm    Post subject: Reply with quote

Maybe the value stored at that address + offset is zero.
Back to top
View user's profile Send private message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Sat May 30, 2009 12:28 pm    Post subject: Reply with quote

@dnsi0

I know the pointer + offset is correct, I've asked several people for them, and always get the same response.

@tombana

Naw, it is supposed to return the Map ID of the map my character is in.

EDIT:
@colour0xFFA500

Still returns 0

_________________
Back to top
View user's profile Send private message AIM Address
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Sat May 30, 2009 12:39 pm    Post subject: Reply with quote

yoyonerd wrote:
@dnsi0

I know the pointer + offset is correct, I've asked several people for them, and always get the same response.

@tombana

Naw, it is supposed to return the Map ID of the map my character is in.

EDIT:
@colour0xFFA500

Still returns 0


Haha for ms? Just do:
HAHA=*(DWORD*)((*(DWORD*)MapBase)+Offset)

And use the try and except blocks to look for errors.
Back to top
View user's profile Send private message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Sat May 30, 2009 1:20 pm    Post subject: Reply with quote

dnsi0 wrote:
yoyonerd wrote:
@dnsi0

I know the pointer + offset is correct, I've asked several people for them, and always get the same response.

@tombana

Naw, it is supposed to return the Map ID of the map my character is in.

EDIT:
@colour0xFFA500

Still returns 0


Haha for ms? Just do:
HAHA=*(DWORD*)((*(DWORD*)MapBase)+Offset)

And use the try and except blocks to look for errors.


Yep >:]

I tried something different

I replaced return 0 with the return of the Pointer's value, so it would be forced to return the pointer, but it gives me an access violation error.

_________________
Back to top
View user's profile Send private message AIM Address
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Sat May 30, 2009 1:32 pm    Post subject: Reply with quote

yoyonerd wrote:
dnsi0 wrote:
yoyonerd wrote:
@dnsi0

I know the pointer + offset is correct, I've asked several people for them, and always get the same response.

@tombana

Naw, it is supposed to return the Map ID of the map my character is in.

EDIT:
@colour0xFFA500

Still returns 0


Haha for ms? Just do:
HAHA=*(DWORD*)((*(DWORD*)MapBase)+Offset)

And use the try and except blocks to look for errors.


Yep >:]

I tried something different

I replaced return 0 with the return of the Pointer's value, so it would be forced to return the pointer, but it gives me an access violation error.


That just means that its an invalid address/offset. Is this a dll? And did you inject it to the right process?
Back to top
View user's profile Send private message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Sat May 30, 2009 2:45 pm    Post subject: Reply with quote

dnsi0 wrote:
yoyonerd wrote:
dnsi0 wrote:
yoyonerd wrote:
@dnsi0

I know the pointer + offset is correct, I've asked several people for them, and always get the same response.

@tombana

Naw, it is supposed to return the Map ID of the map my character is in.

EDIT:
@colour0xFFA500

Still returns 0


Haha for ms? Just do:
HAHA=*(DWORD*)((*(DWORD*)MapBase)+Offset)

And use the try and except blocks to look for errors.


Yep >:]

I tried something different

I replaced return 0 with the return of the Pointer's value, so it would be forced to return the pointer, but it gives me an access violation error.


That just means that its an invalid address/offset. Is this a dll? And did you inject it to the right process?


Lol, epic fail~

I forgot you need to use an injected dll to directly read memory.

It was actually just a win32 console app >.>

_________________
Back to top
View user's profile Send private message AIM Address
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Sat May 30, 2009 5:16 pm    Post subject: Reply with quote

dnsi0 wrote:
yoyonerd wrote:
@dnsi0

I know the pointer + offset is correct, I've asked several people for them, and always get the same response.

@tombana

Naw, it is supposed to return the Map ID of the map my character is in.

EDIT:
@colour0xFFA500

Still returns 0


Haha for ms? Just do:
HAHA=*(DWORD*)((*(DWORD*)MapBase)+Offset)

And use the try and except blocks to look for errors.


Thats risky. The readpointer method is to test for invalid memory and not fail the program. If mapbase or mapbase+offset points to nothing the program will fail. Rolling Eyes

_________________
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Sat May 30, 2009 7:45 pm    Post subject: Reply with quote

blankrider wrote:
dnsi0 wrote:
yoyonerd wrote:
@dnsi0

I know the pointer + offset is correct, I've asked several people for them, and always get the same response.

@tombana

Naw, it is supposed to return the Map ID of the map my character is in.

EDIT:
@colour0xFFA500

Still returns 0


Haha for ms? Just do:
HAHA=*(DWORD*)((*(DWORD*)MapBase)+Offset)

And use the try and except blocks to look for errors.


Thats risky. The readpointer method is to test for invalid memory and not fail the program. If mapbase or mapbase+offset points to nothing the program will fail. Rolling Eyes


But wouldn't a try/catch block be able to catch the ACCESSVIOLATION exception?
Back to top
View user's profile Send private message
yoyonerd
Grandmaster Cheater
Reputation: 0

Joined: 26 Apr 2008
Posts: 699
Location: -->formerly yoyonerd<--

PostPosted: Sat May 30, 2009 9:46 pm    Post subject: Reply with quote

Does anyone know how to make a Win32 Console Application injectable?

It's already a DLL, but when I inject it, nothing appears.

I got these codes from an old trainer, but it looks like it is made for a resource form, not a Win32 Console App

Code:
//Show Dialogue
DWORD WINAPI MainWin( HMODULE hModule){
   Sleep(500);
   DialogBox(hModule, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgProc);
   ExitThread(0);
   return 0;
}

//Entry Point into program.
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                )
{
   switch (ul_reason_for_call)
   {
   case DLL_PROCESS_ATTACH:
         DisableThreadLibraryCalls(hModule);
         CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MainWin, hModule, 0, NULL);
         break;
   case DLL_THREAD_ATTACH:
   case DLL_THREAD_DETACH:
   case DLL_PROCESS_DETACH:
         FinishedExit = TRUE;
         break;
   }
   return TRUE;
}

_________________
Back to top
View user's profile Send private message AIM Address
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Sun May 31, 2009 7:33 am    Post subject: Reply with quote

yoyonerd wrote:
Does anyone know how to make a Win32 Console Application injectable?

It's already a DLL, but when I inject it, nothing appears.

I got these codes from an old trainer, but it looks like it is made for a resource form, not a Win32 Console App

Code:
//Show Dialogue
DWORD WINAPI MainWin( HMODULE hModule){
   Sleep(500);
   DialogBox(hModule, MAKEINTRESOURCE(IDD_DIALOG1), NULL, (DLGPROC)DlgProc);
   ExitThread(0);
   return 0;
}

//Entry Point into program.
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                )
{
   switch (ul_reason_for_call)
   {
   case DLL_PROCESS_ATTACH:
         DisableThreadLibraryCalls(hModule);
         CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MainWin, hModule, 0, NULL);
         break;
   case DLL_THREAD_ATTACH:
   case DLL_THREAD_DETACH:
   case DLL_PROCESS_DETACH:
         FinishedExit = TRUE;
         break;
   }
   return TRUE;
}


You need a dlgproc to handle all the messages the dialog is gonna throw at you. Just rip it out of kitterz trainer.
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
Goto page 1, 2  Next
Page 1 of 2

 
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