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 


[Help] Last time with GetPixel

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

Joined: 24 Dec 2007
Posts: 1268

PostPosted: Fri Jul 11, 2008 11:09 am    Post subject: [Help] Last time with GetPixel Reply with quote

Haha I know I am posting a lot....but his will be the last one for awhile.

I have this code

Code:
#include "stdafx.h"
#include <windows.h>
#include <iostream>

HINSTANCE hInstApp = NULL;

COLORREF CColor;
HDC SColor;   
POINT Pos;

int redv;
int bluev;
int greenv;

DWORD GPA = (DWORD)GetProcAddress(LoadLibrary("GDI32.DLL"), "GetPixel")+ 5;
_declspec(naked) COLORREF GetPix(HDC hdc, int nXPos, int nYPos) {
   _asm
   {
      mov edi, edi
      push ebp
      mov ebp, esp
      jmp[GPA]
   }
}

/////////////////////////////////
int main() {

   while (1==1) {
         SColor = GetDC(HWND_DESKTOP);
         GetCursorPos (&Pos);
         CColor = GetPix (SColor,Pos.x,Pos.y);
         
         redv = GetRValue(CColor);
         greenv = GetGValue(CColor);
         bluev = GetBValue(CColor);
            
         cout << "Red:   " << redv << endl;
         cout << "Green:  " << greenv << endl;
         cout << "Blue:   " << bluev << endl;
         cout << endl;

         //Sleep (1000);
   }

   return 0;
}


But after showing like 10 color points, it gives the error

Quote:
Unhandled exception at 0x00401060 in Test.exe: 0xC0000005: Access violation writing location 0x00130000.


Any Solutions?
Back to top
View user's profile Send private message Send e-mail
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jul 11, 2008 1:31 pm    Post subject: Reply with quote

Open it in a debugger and figure out where the exception is being generated, then I might be able to help you.

Also, mov edi, edi is not needed in your trampoline. The only reason that instruction is there is to provide a buffer for hot patching.
Back to top
View user's profile Send private message
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Fri Jul 11, 2008 1:36 pm    Post subject: Reply with quote

Flyte wrote:
Open it in a debugger and figure out where the exception is being generated, then I might be able to help you.

Also, mov edi, edi is not needed in your trampoline. The only reason that instruction is there is to provide a buffer for hot patching.

Why doesn't it use NOPs then?
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jul 11, 2008 1:48 pm    Post subject: Reply with quote

Symbol wrote:
Why doesn't it use NOPs then?


NOPs are used to pad the spaces between functions (INT3 is used in a debug build), so if they did use NOP it would look ugly (and be potentially confusing) in a debugger.

Fun fact: NOP is just an alias mnemonic for xchg eax, eax.
Back to top
View user's profile Send private message
kitterz
Grandmaster Cheater Supreme
Reputation: 0

Joined: 24 Dec 2007
Posts: 1268

PostPosted: Fri Jul 11, 2008 2:02 pm    Post subject: Reply with quote

Flyte wrote:
Open it in a debugger and figure out where the exception is being generated, then I might be able to help you.

Also, mov edi, edi is not needed in your trampoline. The only reason that instruction is there is to provide a buffer for hot patching.


Well im not sure how to load it into a debugger since its like a dos program....but I do know that it is screws up when calling

Code:
COLORREF GetPix(HDC hdc, int nXPos, int nYPos)
Back to top
View user's profile Send private message Send e-mail
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jul 11, 2008 2:38 pm    Post subject: Reply with quote

The following code works perfectly for me.

Code:
#include <windows.h>
#include <iostream>

DWORD GetPixelProc = (DWORD)GetProcAddress( LoadLibrary( "GDI32.DLL" ), "GetPixel" ) + 5;

COLORREF __declspec( naked ) GetPix( HDC hdc, int nXPos, int nYPos )
{
    __asm {
        mov   edi, edi
        push  ebp
        mov   ebp, esp
        jmp   [GetPixelProc]
    }
}

void main( void )
{
    COLORREF crColor;
    POINT    pPoint;
    //-------

    for(;; Sleep( 500 )) {
        GetCursorPos( &pPoint );
        crColor = GetPix( GetDC( HWND_DESKTOP ), pPoint.x, pPoint.y );
        std::cout << "Red:\t" << (int)GetRValue( crColor ) << std::endl;
        std::cout << "Green:\t" << (int)GetGValue( crColor ) << std::endl;
        std::cout << "Blue:\t" << (int)GetBValue( crColor ) << std::endl << std::endl;
    }
}


Edit: Crashes when you remove Sleep(500), I assume that it is because the console is being flooded with output, and has nothing to do with the trampolined API.
Back to top
View user's profile Send private message
kitterz
Grandmaster Cheater Supreme
Reputation: 0

Joined: 24 Dec 2007
Posts: 1268

PostPosted: Fri Jul 11, 2008 3:32 pm    Post subject: Reply with quote

Flyte wrote:
The following code works perfectly for me.

Code:
#include <windows.h>
#include <iostream>

DWORD GetPixelProc = (DWORD)GetProcAddress( LoadLibrary( "GDI32.DLL" ), "GetPixel" ) + 5;

COLORREF __declspec( naked ) GetPix( HDC hdc, int nXPos, int nYPos )
{
    __asm {
        mov   edi, edi
        push  ebp
        mov   ebp, esp
        jmp   [GetPixelProc]
    }
}

void main( void )
{
    COLORREF crColor;
    POINT    pPoint;
    //-------

    for(;; Sleep( 500 )) {
        GetCursorPos( &pPoint );
        crColor = GetPix( GetDC( HWND_DESKTOP ), pPoint.x, pPoint.y );
        std::cout << "Red:\t" << (int)GetRValue( crColor ) << std::endl;
        std::cout << "Green:\t" << (int)GetGValue( crColor ) << std::endl;
        std::cout << "Blue:\t" << (int)GetBValue( crColor ) << std::endl << std::endl;
    }
}


Edit: Crashes when you remove Sleep(500), I assume that it is because the console is being flooded with output, and has nothing to do with the trampolined API.


This works for you? Interesting. Cause it still gives me the Unhandled exception error.
Back to top
View user's profile Send private message Send e-mail
kitterz
Grandmaster Cheater Supreme
Reputation: 0

Joined: 24 Dec 2007
Posts: 1268

PostPosted: Fri Jul 11, 2008 9:06 pm    Post subject: Reply with quote

x0r wrote:
Probably because you're on Windows XP SP1 or earlier.

Nah im SP2
Back to top
View user's profile Send private message Send e-mail
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