Author Topic: my program and an online program  (Read 2522 times)

shankle

  • Member
  • ****
  • Posts: 807
my program and an online program
« on: August 14, 2017, 09:53:18 AM »
             
          8-13-2017

I want to call an online program(B) from my program(A) and pass
some data from the online program(B) to my program(A) repeatedly.

Been trying this for months and getting nowhere. Yes I know 3rd
party programs can capture the data and pass it to the clipboard
but this is way to slow for the process I need to do. As an example
Fastcapture does this very nice but it is way to slow.

What my program(A) needs to do is call the online program(B) and
capture a small rectangle from program(B) and pass that data back
to my program(A) many times.

Since other programs can capture data from an online program screen,
there must be a way to do it in GoAsm. Tried dozens of ways to do it
with bitblt but none work.
Thanks for any suggestions.
          

Yuri

  • Member
  • **
  • Posts: 174
Re: my program and an online program
« Reply #1 on: August 15, 2017, 11:19:06 PM »
Look at this function, for example. It takes the window handle, the (client area) coordinates, width and height of the rectangle and returns in EAX a pointer to a buffer containing in each DWORD the color of a pixel in BGR format: the low byte is for blue, the next for green, the third for red. The fourth byte is probably not guaranteed to always contain 0, so it's better to mask it somehow.

If the function fails, it sets the carry flag and returns an error code in EAX.

The buffer is allocated each time the function is called, which is not an optimal solution, of course.

Code: [Select]
captureRect: FRAME hWnd, x, y, w, h
             USES rbx,rdi,rsi
             LOCALS bih:BITMAPINFOHEADER, pBmp, err

    lea rdi,[bih]
    mov ecx,sizeof BITMAPINFOHEADER / 4
    xor eax,eax
    mov [err],rax
    rep stosd
    invoke GetDC, [hWnd]
    mov rsi,rax
    test rax,rax
    jnz >
        mov D[err],1
        jmp >>.error
    :
    invoke CreateCompatibleDC, rsi
    mov rdi,rax
    test rax,rax
    jnz >
        mov D[err],2
        jmp >>.error
    :
    invoke CreateCompatibleBitmap, rsi, [w], [h]
    mov rbx,rax
    test rax,rax
    jnz >
        mov D[err],3
        jmp >>.error
    :
    invoke SelectObject, rdi, rbx
    test rax,rax
    jnz >
        mov D[err],4
        jmp >>.error
    :
    invoke BitBlt, rdi, 0, 0, [w], [h], rsi, [x], [y], SRCCOPY
    test eax,eax
    jnz >
        mov D[err],5
        jmp >>.error
    :
    mov eax,[w]
    shl eax,2
    mul D[h]
    invoke GlobalAlloc, GMEM_ZEROINIT, rax
    mov [pBmp],rax
    mov D[bih.biSize],sizeof BITMAPINFOHEADER
    mov edx,[w]
    mov ecx,[h]
    neg ecx
    mov [bih.biWidth],edx
    mov [bih.biHeight],ecx
    neg ecx
    mov W[bih.biPlanes],1
    mov W[bih.biBitCount],32
    mov D[bih.biCompression],BI_RGB
    invoke GetDIBits, rdi, rbx, 0, ecx, [pBmp], addr bih, DIB_RGB_COLORS
    test eax,eax
    jnz >
        mov D[err],6
        jmp >>.error
    :
    invoke DeleteDC, rdi
    invoke DeleteObject, rbx
    invoke ReleaseDC, [hWnd], rsi
    mov rax,[pBmp]
    clc
.exit
    ret
.error
    cmp D[err],6
    jb >
        invoke GlobalFree, [pBmp]
    :
    cmp D[err],4
    jb >
        invoke DeleteObject, rbx
    :
    cmp D[err],3
    jb >
        invoke DeleteDC, rdi
    :
    cmp D[err],2
    jb >
        invoke ReleaseDC, [hWnd], rsi
    :
    mov eax,[err]
    stc
    jmp .exit

ENDF
« Last Edit: August 16, 2017, 02:39:16 PM by Yuri »

Yuri

  • Member
  • **
  • Posts: 174
Re: my program and an online program
« Reply #2 on: August 16, 2017, 02:43:27 PM »
Added another "neg ecx" to make the height positive again for GetDIBits. It seemed to work OK without that, though.

shankle

  • Member
  • ****
  • Posts: 807
Re: my program and an online program
« Reply #3 on: August 16, 2017, 08:42:28 PM »
Thank you Yuri for responding.
It is not exactly what I want to do but I will use it as a guide.

HSE

  • Member
  • ****
  • Posts: 953
  • <AMD>< 7-32>
Re: my program and an online program
« Reply #4 on: August 18, 2017, 12:03:44 AM »
It's what I was searching last week (what a coincidence!). There is very few examples of GetDIBits, and my translation to assembly was failing  8) (or those example were incorrect, or even win32 help is wrong).

Thank you Yuri!  :t


Yuri

  • Member
  • **
  • Posts: 174
Re: my program and an online program
« Reply #5 on: August 18, 2017, 01:43:55 AM »
You are welcome. :icon_cool:

shankle

  • Member
  • ****
  • Posts: 807
Re: my program and an online program
« Reply #6 on: September 29, 2017, 12:26:20 AM »
                9-28-2017

    This message is intended for Yuri.
    If you would be so kind as to give your comments please.
    Two programs are involved here. 1. my program 2. an online program
    My program retrieves a small portion of the online program screen.
    Two bytes of rax are cleared. How do the other 2 bytes get created?
    This code compiled clean and is to be executed 12 times.
    The changes I have made to your code have comments after them.
   

    FRAME  hdc,iMsg,wParam,lParam,hPen,hWnd2,hWnd,xx,yy,ww,hh
    uses rbx,rdi,rsi
    LOCAL  ps:PAINTSTRUCT,pt:POINT,RR:RECT,rrrect:RECT
    LOCAL  bih:BITMAPINFOHEADER,pBmp,err


   lea rdi,[bih]
   mov ecx,sizeof BITMAPINFOHEADER / 4
   xor eax,eax
   mov [err],rax     ; how did the other 2 bytes of rax get loaded
   rep stosd
   invoke GetDC, NULL    ; this should get the handle of the online program
                                    ; which is unknown to me
   mov rsi,rax
   test rax,rax
    jnz >
     mov d[err],1
     jmp >>.error
:
     mov [hWnd2],rax   ; put online program handle info in this field
     invoke CreateCompatibleDC,rsi
     mov rdi,rax
     test rax,rax
      jnz >
   mov d[err],2
   jmp >>.error
:
     invoke CreateCompatibleBitmap, rsi,[ww],[hh]
     mov rbx,rax
     test rax,rax
      jnz >
    mov d[err],3
    jmp >>.error
:
     invoke SelectObject,rdi,rbx
     test rax,rax
       jnz >
         mov d[err],4
    jmp >>.error
:
     invoke BitBlt, [hWnd],0,0,[ww],[hh],rsi,[xx],[yy],SRCCOPY
                        ; hWnd is the handle to my program
     test eax,eax 
       jnz >
    mov d[err],5
    jmp >>.error
:
     invoke GlobalAlloc, GMEM_ZEROINIT,rax
     mov [pBmp],rax
     mov d[bih.biSize],sizeof BITMAPINFOHEADER
     mov edx,[ww]
     mov ecx,[hh]
     neg ecx
     mov [bih.biWidth],edx
     mov [bih.biHeight],ecx
     neg ecx
     mov w[bih.biPlanes],1
     mov w[bih.biBitCount],32
     mov d[bih.biCompression],BI_RGB
     invoke GetDIBits,rdi,rbx,0,ecx,[pBmp],addr bih,DIB_RGB_COLORS
     test eax,eax
       jnz >
    mov d[err],6
    jmp >>.error
:
     invoke DeleteDC, rdi
     invoke DeleteObject,rbx
     invoke ReleaseDC,[hWnd2],rsi   ; handle to the online program
     mov rax,[pBmp]
     clc
.exit            ; not necessary - more code to execute
    ret          ; this code has to be executed 12 times
.error
     cmp d[err],6
       jb >
      invoke GlobalFree,[pBmp]
:
     cmp d[err],4
        jb >
     invoke DeleteObject,rbx
:
     cmp d[err],3
   jb >
      invoke DeleteDC,rdi
:
     cmp d[err],2
   jb >
     invoke ReleaseDC,[hWnd2],rsi
:
     mov eax,[err]
     stc
     jmp .exit     ;this should fall through for more processing


Yuri

  • Member
  • **
  • Posts: 174
Re: my program and an online program
« Reply #7 on: September 29, 2017, 01:36:42 AM »
Actually, four bytes of RAX are cleared. The other four bytes are cleared by the processor automatically.

I don't quite understand the changes you made.

Code: [Select]
mov [hWnd2],rax   ; put online program handle info in this field
What is the point of doing this? hWnd2 is a parameter of your function.

Code: [Select]
invoke BitBlt, [hWnd],0,0,[ww],[hh],rsi,[xx],[yy],SRCCOPY
                        ; hWnd is the handle to my program
What is a handle to a program? The first argument should be a handle to a compatible DC that was created earlier and stored in RDI. It's a memory DC and doesn't belong to any window.

Code: [Select]
invoke GlobalAlloc, GMEM_ZEROINIT,rax
You removed the code before this call that calculated the size of the buffer passed in RAX. Now RAX contains the return value of BitBlt, which has nothing to do with the size.

shankle

  • Member
  • ****
  • Posts: 807
Re: my program and an online program
« Reply #8 on: January 02, 2019, 07:26:36 AM »
                 1-1-2019
 PURPOSE: to copy a small rectangle from a game program &
    put it in a window in my program.
    This program is failing at Bitblt #2 but the error could be caused
    earlier, but I am really not sure.
    I do not want to write the results to a file and 3rd party programs
    are to slow and so is the clipboard.
    Evidently I must fill out one or more of the many bitmap structures
    but these are a mystery to me. I have been using Microsofts "Capture
    an Image" for a guide but have been unsuccessful.

wjr

  • Member
  • **
  • Posts: 199
    • WJR's website
Re: my program and an online program
« Reply #9 on: January 02, 2019, 09:56:44 AM »
I noticed a few things:

After the call to GetCommandLine, the return value in rax should be saved to [CommandLine].

Your calls to BeginPaint and EndPaint require another parameter for the pointer to a PAINTSTRUCT (ADDR ps) which you can put in LOCAL for WndProc (LOCAL ps:PAINTSTRUCT).

By doing this, BeginPaint returns in rax the hdc so you may not need the following call to GetDC.

Check each WM_ message... if you are processing a message in WndProc, when done you should jmp to your END so as not to also do DefWndProc.

After the call to CreateCompatibleBitmap, it should be a jne >> or ja >> instead of a signed compare (check other ones too when comparing to 0).

In WM_KEYDOWN you use [SvHandlePaint] but that value hasn't been set elsewhere, throwing off your hdc value further down in your call to BitBlt.

shankle

  • Member
  • ****
  • Posts: 807
Re: my program and an online program
« Reply #10 on: January 02, 2019, 11:31:17 AM »
                 1-4-2019
 PURPOSE: to copy a small rectangle from a game program &
       put it in a window in my program.
       Thanks for responding WJR.

       This zipped version contains the suggestions made by WJR.
       This program is failing at Bitblt #2 but the error could be caused
       earlier, but I am really not sure.
       I do not want to write the results to a file and 3rd party programs
       are to slow and so is the clipboard.
       Evidently I must fill out one or more of the many bitmap structures
       but these are a mystery to me. I have been using Microsofts "Capture
       an Image" for a guide but have been unsuccessful.

             1-6-2019
       In Microsofts "CaptureAnImage" what does the
       code " HBITMAP hbmScreen = NULL" do?
       I can only find one reference to it in "CaptureAnImage".
       My "C" is almost non existent.
       I have not used it in my program and wonder
       if it is causing my problem?     
« Last Edit: January 07, 2019, 08:52:29 AM by shankle »

shankle

  • Member
  • ****
  • Posts: 807
Re: my program and an online program
« Reply #11 on: March 06, 2019, 11:42:03 PM »
This new way works but the cards played come to fast
for me to keep up.
The computer has to handle everything with no interference
by me to get the results I want.

« Last Edit: March 11, 2019, 10:09:19 PM by shankle »