format PE GUI 4.0 DLL
entry dllmain

include '%fasminc%\win32a.inc'

VERTRES	= 10
HORZRES	= 8

section '.data' data readable writeable

dllH			dd ?		; base address / instance / module handle

bmpFileName		rb 0xFF
bmpSize		dd ?
bmpH			dd ?		; bitmap file handle
bmpWritten		dd ?		; bytes written
bmpAllocH		dd ?		; memory alloc handle
bmpFileHeader		BITMAPFILEHEADER 0x4D42,0,0,0,54
bmpInfo		BITMAPINFOHEADER sizeof.BITMAPINFOHEADER,0,0,1,24,BI_RGB,0,0,0,0,0
			rd 6

scwH			dd ?
scwRect		RECT
scwHeight		dd ?
scwWidth		dd ?
scwDc			dd ?		; original dc
scwDcMem		dd ?		; compatible dc
scwDcBmp		dd ?		; bitmap dc


section '.code' code readable executable

; dllmain
; - the main entry for dll file
proc dllmain hinstDLL, fdwReason, lpvReserved
		push	[hinstDLL]
		pop	[dllH]
		mov	eax,TRUE
		ret
endp

; set_bmpFileName
; - set the output name for the captured bitmap filename
proc set_bmpFileName uses ebx esi edi, ptrString 
	invoke	lstrcpy, bmpFileName, [ptrString]
		ret
endp

; set_scwH
; - get the wanna scanned window handle
proc set_scwH uses ebx esi edi, hLong
		push	[hLong]
		pop	[scwH]
		ret
endp

; scan_window
; - start scanning the [scwH]
proc scan_window uses ebx esi edi
	invoke	GetWindowDC,[scwH]
		mov	[scwDc],eax
	invoke	CreateCompatibleDC,eax
		mov	[scwDcMem],eax
	invoke	GetWindowRect,[scwH],scwRect
		mov	eax,[scwRect.bottom]
		sub	eax,[scwRect.top]
		mov	[scwHeight],eax
		push	eax
		mov	eax,[scwRect.right]
		sub	eax,[scwRect.left]
		mov	[scwWidth],eax
		push	eax
	invoke	CreateCompatibleBitmap,[scwDc]
		mov	[scwDcBmp],eax
	invoke	SelectObject,[scwDcMem],[scwDcBmp]
		push	eax
	invoke	BitBlt,[scwDcMem],0,0,[scwWidth],[scwHeight],[scwDc],0,0,SRCCOPY
	call	scan_save
		pop	eax
	invoke	SelectObject,[scwDcMem],eax
	invoke	DeleteObject,[scwDcMem]
	invoke	ReleaseDC,[scwH],[scwDc]
		ret
endp

; scan_save
; - save the bitblt memory dc into bitmap file
proc scan_save uses ebx esi edi
		push	[scwHeight] [scwWidth]
		pop	[bmpInfo.biWidth] [bmpInfo.biHeight]
	invoke	CreateFile,bmpFileName,GENERIC_READ + GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
		mov	[bmpH],eax
	invoke	WriteFile,eax,bmpFileHeader,sizeof.BITMAPFILEHEADER,bmpWritten,0
		mov	eax,[scwHeight]
		imul	eax,[scwWidth]
		imul	eax,3
		mov	[bmpSize],eax
	invoke	GlobalAlloc,GMEM_FIXED,eax
		mov	[bmpAllocH],eax
	invoke	GetDIBits,[scwDc],[scwDcBmp],0,[scwHeight],eax,bmpInfo,0
	invoke	WriteFile,[bmpH],bmpInfo,sizeof.BITMAPINFOHEADER,bmpWritten,0
	invoke	WriteFile,[bmpH],[bmpAllocH],[bmpSize],bmpWritten,0
	invoke	CloseHandle,[bmpH]
	invoke	GlobalFree,[bmpAllocH]
		ret
endp

section '.edata' export data readable
	export 'DSCANNER.DLL',\
			scan_window,		'scan_window',\
			set_scwH,		'set_scwH',\
			set_bmpFileName,	'set_bmpFileName'

section '.idata' import data readable
	library	user32,		'USER32.DLL',\
			kernel32,		'KERNEL32.DLL',\
			gdi32,			'GDI32.DLL'

	include '%fasminc%\API\USER32.INC'
	include '%fasminc%\API\KERNEL32.INC'
	include '%fasminc%\API\GDI32.INC'

section '.reloc' fixups data discardable