CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2013
    Posts
    34

    [RESOLVED] GetPixel won't pull from java programs?

    Hey, I'm building a simple little program for minecraft to reduce the repetitiveness, and to help me along, I'm using getpixel, it's slow, but i only need to grab about 6-12 pixels.

    but the problem is, I can get the pixel data if I capture from the entire desktop display, but as soon as I tell it to grab from the active window (wich goes MUCH faster) it doesn't return any colour value's.

    any Idea why that is? I'd rather not have to use getDIBits or program in java or anything since I want to keep it fairly kiss, and if it can't be done, then.. ah well, I'll just grab from the entire desktop, speed isn't to much of an issue, but it'll be a pain to keep the window at the same place.

    any help would be awesome, thanks.

    (since people always want to see code, here's the code I'm using (simplified, but compilable)

    Code:
    #include <Windows.h>
    #include <iostream>
    
    using namespace std;
    
    // vvv Ignore this, it's just used for retrieving the handle of a window without giving the full window name vvvv
    
    struct results 
    {
        char* text;
        UINT (__stdcall*GET)(HWND,LPSTR,UINT);
        HWND hRet;
    };
    
    BOOL WINAPI StruEnumProc( HWND hwnd, results* stru )
    {
        char loc_buf[128];
        stru->GET( hwnd, loc_buf, 127 );
        if( strstr( loc_buf, stru->text ) ) {
            stru->hRet = hwnd;
            return FALSE;
        }
        return TRUE;
    }
    
    HWND FindWindowTitleContains( char* text )
    {
        results res = { text, (UINT (__stdcall *)(HWND,LPSTR,UINT))GetWindowTextA, 0 };
        EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
        return res.hRet;
    }
    
    // ^^^^^ Ignore this ^^^^^^^^^^
    
    // just some global variables
    int colour;
    
    
    int main()
    {
    	HWND handle = FindWindowTitleContains( "Minecraft" );
    	HDC hdca = GetDC( handle); // make 'handle' 'NULL' to grab from the entire desktop
    
    	while(true)
    	{
    		colour = GetPixel(hdca, 100, 100);
    		cout << colour << '\r';
    	}
    
    
    	return 0;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: GetPixel won't pull from java programs?

    I don't see in your code snippet where and how you check the return values of the APIs you call.
    So why do you think that your FindWindowTitleContains returns some valid window handle? Why do you think GetDC returns some valid HDC?
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2013
    Posts
    34

    Re: GetPixel won't pull from java programs?

    Quote Originally Posted by VictorN View Post
    I don't see in your code snippet where and how you check the return values of the APIs you call.
    So why do you think that your FindWindowTitleContains returns some valid window handle? Why do you think GetDC returns some valid HDC?

    I thought I had the right handle since, at first I was just getting 'invalid handle' returned, but now I was getting a valid handle.

    but now you mentioned it, I decided to double check with spy++

    turns out I was wrong though since i AM getting a valid handle to the window which the java is running in, the java itself uses a different handle, which would explain the wrong return values.

    so I gotta find a way to get the correct handle to the actual javascript ^.^ thank you for helping me solve the problem

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: [RESOLVED] GetPixel won't pull from java programs?

    getpixel from another process than the one painting "the pixels" will only work when:
    - you have disabled desktop composition.
    - you use a DC for the entire desktop.


    With desktop composition enabled, all DC's are virtualized, and windows optimizes the actual screen updating in the WDM (potentially using 3D hardware accelleration in the process). This is the only way it can efficiently handle partial transparencies such as the glass effect on the window borders.
    Using a desktop DC you get the pixel value through the WDM after all the DC's have been actually blitted onto a separate memory DC which is why this takes extra processing time. In fact you will see the WDM taking a substantial amount of CPU time in task manager, and if you have games running you will see a significant drop in fps as a result.
    This happens every time you get a pixel, which makes getpipxel extra bad. You really need to at least try to optimize your code to blit all the pixels you'll need in one GDI call rather than in several getpixel calls.

    You can disable desktop composition.
    This will however have a global effect on windows (it will disable several windows features such as transparencies/glass). Screen updates will be slower, and can be significantly slower depending on what apps are running and if they aren't properly designed to work without WDM. Some apps may downright not function properly having all sorts of screen artifacts, tearing, shearing, alpha bleeding, ...
    Getpixel however will work as expected (in so far as the app itself displays proper pixel values and doens't suffer from disabling WDM) and should even be faster than in the WDM enabled mode, however, YMMV depending on what videocard you have. There are several video cards our there where all hardware accelleration is disabled when WDM is off.
    Last edited by OReubens; June 5th, 2013 at 07:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured