Hi all, that's me again... :)

So I looked at the 2 codes that you gave me in this topic : http://www.asmcommunity.net/board/viewtopic.php?p=154602#154602

Finally I chose the code wih CapScreen because it allows me to don't use the clipboard. But I have a problem with this code, that's when I put it in a loop :

Here the code I would like to run :
.386

.model flat, stdcall
option casemap :none

; INCLUDES ET BIBLIOTHEQUES (LIBRAIRIES)
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\shell32.inc
include \masm32\include\advapi32.inc
include \masm32\include\gdi32.inc
include \masm32\include\comctl32.inc
include \masm32\include\comdlg32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\shell32.lib
includelib \masm32\lib\advapi32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\comdlg32.lib
includelib \masm32\lib\masm32.lib

include \masm32\macros\macros.asm

CapScreen PROTO :DWORD

.data
NomBMP DB "CaptureBitmap.bmp",0
szDisplay db "DISPLAY",0
szNoDC db "Couldn''t create DC.",0
szNoMemDC db "Couldn''t create compatible DC.",0
szNoBMP db "Couldn''t create compatible bitmap.",0
szNoObj db "Couldn''t select bitmap.",0
szNoCopy db "Couldn''t copy bitmap.",0
szNoFile db "Couldn''t write file to disk.",0
szDone db "Bitmap created.",0

.code
start:
@Timer:

invoke CapScreen, addr NomBMP

CapScreen Proc lpFileName:DWORD
LOCAL hdc:HDC
LOCAL memdc:HDC
LOCAL dwBytes:DWORD
LOCAL bitmapfileheader:BITMAPFILEHEADER
LOCAL bitmapinfoheader:BITMAPINFOHEADER
LOCAL hFile:HANDLE
LOCAL colors[256]:RGBQUAD
LOCAL bmpinfo:BITMAPINFO
LOCAL hBitmap:HBITMAP
LOCAL pBits:DWORD
LOCAL dwWidth:DWORD
LOCAL dwHeight:DWORD
LOCAL dwNumColors:DWORD
LOCAL dwBPP:DWORD
LOCAL ColorSize:DWORD

invoke CreateDC, addr szDisplay, NULL, NULL, NULL ; On cr?e le DC
mov hdc,eax ; On met le handle du DC dans hdc
.IF (eax==NULL) ; Si le handle du DC = 0
invoke MessageBox, 0, addr szNoDC, NULL, 0 ; alors : "Couldn''t create DC"
jmp ExitFunc ; On quitte Capscreen.
.ENDIF

invoke GetDeviceCaps, hdc, HORZRES ; On met la largeur du DC
mov dwWidth,eax ; dans dwwidth
invoke GetDeviceCaps, hdc, VERTRES ; et la hauteur du DC
mov dwHeight,eax ; dans dwheight.

invoke GetDeviceCaps, hdc, BITSPIXEL
mov dwBPP,24 ;24 Bit for LWF/JPEG/JPEG2000 output, else doesnt work
.IF (eax<=8)
invoke GetDeviceCaps, hdc, NUMCOLORS
mov dwNumColors,eax
mov dwNumColors,256 ;this one looks bad
.ELSE
mov dwNumColors,0
.ENDIF

invoke CreateCompatibleDC, hdc ; On cr?e un DC compatible depuis le handle du DC
mov memdc,eax ; On met le handle du DC compatible dans memdc
.IF (eax==NULL) ; Si le handle du DC compatible = 0
invoke DeleteDC, hdc ; alors on detruit le DC,
invoke MessageBox, 0, addr szNoMemDC, NULL, 0 ; Et on affiche : "Couldn''t create compatible DC."
jmp ExitFunc ; On quitte CapScreen.
.ENDIF

mov bmpinfo.bmiHeader.biSize,sizeof BITMAPINFOHEADER
mov eax,dwWidth
mov bmpinfo.bmiHeader.biWidth,eax
mov eax,dwHeight
mov bmpinfo.bmiHeader.biHeight,eax
mov bmpinfo.bmiHeader.biPlanes,1
mov ax,word ptr [dwBPP]
mov bmpinfo.bmiHeader.biBitCount,ax
mov bmpinfo.bmiHeader.biCompression,BI_RGB ;BI_RGB
mov bmpinfo.bmiHeader.biSizeImage,0
mov bmpinfo.bmiHeader.biXPelsPerMeter,0
mov bmpinfo.bmiHeader.biYPelsPerMeter,0
mov eax,dwNumColors
mov bmpinfo.bmiHeader.biClrUsed,eax
mov bmpinfo.bmiHeader.biClrImportant,eax
invoke CreateDIBSection,hdc,addr bmpinfo, DIB_PAL_COLORS,addr pBits, NULL, 0
mov hBitmap,eax

.IF (eax==NULL)
invoke DeleteDC, hdc
invoke DeleteDC, memdc
invoke MessageBox, 0, addr szNoBMP, NULL, 0
jmp ExitFunc
.ENDIF

invoke SelectObject, memdc, hBitmap
.IF (eax==NULL) || (eax==GDI_ERROR)
invoke DeleteDC, hdc
invoke DeleteDC, memdc
invoke MessageBox, 0, addr szNoObj, NULL, 0
jmp ExitFunc
.ENDIF

invoke BitBlt, memdc, 0,0, dwWidth, dwHeight, hdc, 0,0, SRCCOPY ;SRCCOPY
.IF (!eax)
invoke DeleteDC, hdc
invoke DeleteDC, memdc
invoke MessageBox, 0, addr szNoCopy, NULL, 0
jmp ExitFunc
.ENDIF

mov eax,dwNumColors
.IF (eax!=0)
invoke GetDIBColorTable, memdc, 0, dwNumColors, addr colors
mov dwNumColors,eax
.ENDIF

mov bitmapfileheader.bfType,4D42h ;BM
mov eax,dwNumColors
xor edx,edx
mov ecx,sizeof RGBQUAD
mul ecx
mov ColorSize,eax
mov eax,dwWidth
xor edx,edx
mov ecx,dwHeight
mul ecx
xor edx,edx
mov ecx,dwBPP
mul ecx
shr eax,3
add eax,ColorSize
add eax,sizeof BITMAPFILEHEADER
add eax,sizeof BITMAPINFOHEADER
mov bitmapfileheader.bfSize,eax
mov bitmapfileheader.bfReserved1,0
mov bitmapfileheader.bfReserved2,0
mov eax,ColorSize
add eax,sizeof BITMAPFILEHEADER
add eax,sizeof BITMAPINFOHEADER
mov bitmapfileheader.bfOffBits,eax
mov bitmapinfoheader.biSize,sizeof BITMAPINFOHEADER
mov eax,dwWidth
mov bitmapinfoheader.biWidth,eax
mov eax,dwHeight
mov bitmapinfoheader.biHeight,eax
mov bitmapinfoheader.biPlanes,1
mov ax,word ptr [dwBPP]
mov bitmapinfoheader.biBitCount,ax ;ax
mov bitmapinfoheader.biCompression,BI_RGB ;BI_RGB
mov bitmapinfoheader.biSizeImage,0
mov bitmapinfoheader.biXPelsPerMeter,0
mov bitmapinfoheader.biYPelsPerMeter,0
mov eax,dwNumColors
mov bitmapinfoheader.biClrUsed,eax
mov bitmapinfoheader.biClrImportant,0

invoke CreateFile, lpFileName,GENERIC_WRITE,0,\
NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
mov hFile,eax

.IF (eax==INVALID_HANDLE_VALUE)
invoke DeleteObject, hBitmap
invoke DeleteDC, memdc
invoke DeleteDC, hdc
invoke MessageBox, 0, addr szNoFile, NULL, 0
jmp ExitFunc
.ENDIF

invoke WriteFile, hFile, addr bitmapfileheader, sizeof BITMAPFILEHEADER,addr dwBytes, NULL
invoke WriteFile, hFile, addr bitmapinfoheader, sizeof BITMAPINFOHEADER,addr dwBytes, NULL
mov eax,dwNumColors
.IF (eax!=0)
invoke WriteFile, hFile, addr colors, ColorSize, addr dwBytes,NULL
.ENDIF
mov eax,dwWidth
xor edx,edx
mov ecx,dwHeight
mul ecx
xor edx,edx
mov ecx,dwBPP
mul ecx
shr eax,3
mov ColorSize,eax
invoke WriteFile, hFile, pBits, ColorSize, addr dwBytes,NULL
invoke CloseHandle, hFile
invoke MessageBox, 0, addr szDone, NULL, 0
invoke DeleteObject ,hBitmap
invoke DeleteDC, memdc
invoke DeleteDC, hdc
ExitFunc:
ret
CapScreen endp

Push 5000
Call Sleep ; fait une pause de 5 secondes

JMP @Timer


end start


In first, when I run this program, it says me : Application error... The memory can't be "read". etc etc...
And I don't know why...

But I put this code (above) in another program and it works there. But there is another problem...
My second problem is that I would like to capture the screen and stock it into a bitmap (that's OK lol) but I would like to do it a lot of times (that's why I use the loop)...

Also, as you can see the program displays error msgbox when API functions fail. Also it displays "bitmap created" when it created the bitmap.

So when I start the program, it run the CapScreen procedure : it capture the screen and stock it into a bmp. Ok, then it jumps to @LOOP and it start the second lap of the loop : it capture the screen and it must ecrase (or erase I don't know, I'm french for those that who don't know it :] ) the old bmp creating the new bmp. But instead of that it displays me "Couldn''t write file to disk.".

So my question is : Why ? ^^' And how can I resolve this problem ?

(I think it's a problem with the handle of the created bitmap, but I'm not sure and I didn't find the solution...)

Thanks in advance for your help... :wink:
Posted on 2004-12-23 16:45:19 by azerty1000