.386
.Model Flat,StdCall
Option CaseMap :None

Include Windows.inc
Include user32.inc
Include Gdi32.inc
Include kernel32.inc
Include oleaut32.inc
include ole32.inc
IncludeLib user32.lib
IncludeLib Gdi32.lib
IncludeLib kernel32.lib
IncludeLib oleaut32.lib
includelib ole32.lib

Include MACRO.ASM
IPicture_SaveAsFileProto           typedef proto :DWORD, :DWORD,:DWORD,:DWORD
IPicture_SaveAsFile                typedef ptr IPicture_SaveAsFileProto
IPicture STRUCT
    ; IUnknown methods
    QueryInterface          dd   ?
    AddRef                  dd   ?
    Release                 dd   ?
    ; IPicture methods
    get_Handle              dd   ?
    get_hPal                dd   ?
    get_Type                dd   ?
    get_Width               dd   ?
    get_Height              dd   ?
    Render                  dd   ?
    set_hPal                dd   ?
    get_CurDC               dd   ?
    SelectPicture           dd   ?
    get_KeepOriginalFormat  dd   ?
    put_KeepOriginalFormat  dd   ?
    PictureChanged          dd   ?
    SaveAsFile              IPicture_SaveAsFile   ?
    get_Attributes          dd   ?
IPicture ENDS

PICTDESC_BMP STRUCT
    cbSizeOfStruct  dd ?
    PicType dd ?
    hBitmap dd ?
    hPal dd ?
    Reserved dd ?
PICTDESC_BMP ENDS

IStream STRUCT
    Read dd ?
    Write dd ?
    Seek dd ?
    SetSize dd ?
    CopyTo dd ?
    Commit dd ?
    Revert dd ?
    LockRegion dd ?
    UnlockRegion dd ?
    Stat dd ?
    Clone dd ?
IStream ENDS

.data
IID_IPicture GUID <00020400h,0000h,0000h,<0C0h,000h, 000h,000h, 000h, 000h, 000h,046h>>
.data?
buffer db 256 dup(?)
.code

_CreateBitmapPicture proc
local hDCSrc ,hDCMemory,hBitmap,hOldBitmap
local pPic,sStrm
local dwWidth,dwHeight,dwByte
local hPal,hPalPrev
local hGlobal,hMem,lSize,@pData
local HasPaletteScrn, PaletteSizeScrn
local LogPal :LOGPALETTE
local pic :PICTDESC_BMP


invoke GetSystemMetrics,SM_CXSCREEN
mov dwWidth,eax
invoke GetSystemMetrics,SM_CYSCREEN
mov dwHeight,eax
invoke GetDC,0
mov hDCSrc,eax
   invoke CreateCompatibleDC,eax
   mov hDCMemory,eax
   invoke CreateCompatibleBitmap,hDCSrc,dwWidth,dwHeight
   mov hBitmap,eax
   invoke SelectObject,hDCMemory,eax
   mov hOldBitmap,eax
   invoke GetDeviceCaps,hDCSrc,RASTERCAPS;Raster
   and eax,RC_PALETTE
   mov HasPaletteScrn,eax   ;Palette
invoke GetDeviceCaps,hDCSrc,SIZEPALETTE ;Size of
mov PaletteSizeScrn,eax
.if HasPaletteScrn && (PaletteSizeScrn == 256)
mov LogPal.palVersion , 300h
mov LogPal.palNumEntries , 256
invoke GetSystemPaletteEntries,hDCSrc,0,256,addr LogPal.palPalEntry
invoke CreatePalette,addr LogPal
mov hPal,eax
invoke SelectPalette,hDCMemory,hPal,0
mov hPalPrev,eax
invoke RealizePalette,hDCMemory
.endif
   invoke BitBlt,hDCMemory,0,0,dwWidth,dwHeight,hDCSrc,0,0,SRCCOPY
   invoke SelectObject,hDCMemory,hOldBitmap
   mov hBitmap,eax
   .if HasPaletteScrn && (PaletteSizeScrn == 256)
    invoke SelectPalette,hDCMemory,hPalPrev,0
    mov hPal,eax
   .endif
   invoke ReleaseDC,0,hDCSrc
   invoke DeleteDC,hDCMemory
   
   mov pic.cbSizeOfStruct,sizeof pic ; Length of structure
   mov pic.PicType,1 ; Type of Picture (bitmap)
   mov eax,hBitmap
   mov pic.hBitmap,eax ; Handle to bitmap
   mov eax,hPal
   mov pic.hPal,eax ; Handle to palette (may be null)

invoke OleCreatePictureIndirect,addr pic,addr IID_IPicture,TRUE,addr pPic
.if eax==S_OK
invoke MessageBox,0,0,0,0
.endif
szText sf,"snap.bmp"
   invoke CreateStreamOnHGlobal,NULL,TRUE,addr sStrm
   mov ebx,pPic
   mov ebx,
   invoke (IPicture ptr ).SaveAsFile,pPic,sStrm,TRUE,addr dwByte          ;;;;;Here  ERROR!!!
    .if eax==S_OK
    invoke MessageBox,0,addr sf,0,0
   .endif
  invoke GetHGlobalFromStream,sStrm,hGlobal
invoke GlobalLock,hGlobal
mov hMem,eax
invoke CreateFile,addr sf,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
invoke WriteFile,@hFile,hMem,lSize, @pData, NULL
invoke CloseHandle,@hFile
invoke GlobalUnlock,hGlobal
invoke GlobalFree,hMem
   
ret
_CreateBitmapPicture endp

start:


invoke _CreateBitmapPicture
invoke ExitProcess,NULL
end start




First,I create a bitmap and a palette ,then call OleCreatePictureIndirect to create a picture object,
then save this picture object to a bmp file.
I use IPicture::SaveAsFile to a Stream, then call writefile put to a file. But have a ERR happend.

Why error?
Pls anyone to help me! TKS!



Posted on 2005-09-06 12:08:25 by soulee
Posting the code is good :) now perhaps you could also explain what you're trying to accomplish, and what specific problems you have encountered.
Posted on 2005-09-06 12:52:41 by QvasiModo

.386
.Model Flat, StdCall
Option Casemap :None 
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Include \masm32\include\windows.inc
Include \masm32\include\user32.inc
Include \masm32\include\kernel32.inc
INCLUDE \masm32\include\Gdi32.inc
INCLUDE \masm32\include\oleaut32.inc
INCLUDE \masm32\include\ole32.inc

IncludeLib \masm32\lib\user32.lib
IncludeLib \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\Gdi32.lib
INCLUDELIB \masm32\lib\oleaut32.lib
INCLUDELIB \masm32\lib\ole32.lib

INCLUDE \masm32\macros\macros.asm
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IPicture_SaveAsFileProto    typedef proto :DWORD, :DWORD,:DWORD,:DWORD
IPicture_SaveAsFile          typedef ptr IPicture_SaveAsFileProto
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IPicture STRUCT
    ; IUnknown methods
    QueryInterface          dd  ?
    AddRef                  dd  ?
    Release                dd  ?
    ; IPicture methods
    get_Handle              dd  ?
    get_hPal                dd  ?
    get_Type                dd  ?
    get_Width              dd  ?
    get_Height              dd  ?
    Render                  dd  ?
    set_hPal                dd  ?
    get_CurDC              dd  ?
    SelectPicture          dd  ?
    get_KeepOriginalFormat  dd  ?
    put_KeepOriginalFormat  dd  ?
    PictureChanged          dd  ?
    SaveAsFile              IPicture_SaveAsFile  ?
    get_Attributes          dd  ?
IPicture ENDS
;--------------------------------------------------------
PICTDESC_BMP STRUCT
    cbSizeOfStruct      dd ?
    PicType     dd ?
    hBitmap     dd ?
    hPal     dd ?
    Reserved     dd ?
PICTDESC_BMP ENDS
;--------------------------------------------------------
IStream STRUCT
    Read     dd ?
    Write     dd ?
    Seek     dd ?
    SetSize     dd ?
    CopyTo     dd ?
    Commit     dd ?
    Revert     dd ?
    LockRegion     dd ?
    UnlockRegion     dd ?
    Stat     dd ?
    Clone     dd ?
IStream ENDS
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data
dwWrite  dw 0
IID_IPicture GUID <00020400h,0000h,0000h,<0C0h,000h, 000h,000h, 000h, 000h, 000h,046h>>
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
szMessage db 256 dup (?)
seh dd 6 dup (?)
buffer db 256 dup(?)
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.const
  szErrorThread  db 13, 10, "Error at %08Xh", 13, 10, "Registers:", 13, 10, "eax = %08Xh ebx = %08Xh ecx = %08Xh", 13, 10, "edx = %08Xh esp = %08Xh ebp = %08Xh", 13, 10, "esi = %08Xh edi = %08Xh", 13, 10, 13, 10, "Recovering...", 13, 10,0
  szErrorFinal  db 13, 10, "Error at %08Xh", 13, 10, "Quitting...", 13, 10, 0
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ErrorHandler proc C lpExcept:DWORD, lpFrame:DWORD, lpContext:DWORD, lpDispatch:DWORD
mov  eax, lpExcept
mov  ecx, lpContext
invoke  wsprintf, ADDR szMessage, ADDR szErrorThread, ,\
, , , ,\
, , ,
invoke MessageBox,NULL,  ADDR szMessage,  SADD("1_Error Occured"),MB_ICONEXCLAMATION

mov  eax, lpContext
m2m  ,
m2m  ,
m2m  ,
m2m  ,
m2m  ,
m2m  ,

xor  eax, eax  ; continue execution
ret
ErrorHandler endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ExceptionFilter proc lpExcept:DWORD
mov eax, lpExcept
invoke wsprintf, ADDR szMessage, ADDR szErrorFinal,
invoke MessageBox,NULL,  ADDR szMessage,  SADD("2_Error Occured"),MB_ICONEXCLAMATION
invoke ExitProcess, 0
xor  eax, eax
inc  eax      ; EXCEPTION_EXECUTE_HANDLER
ret
ExceptionFilter endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_CreateBitmapPicture proc
local hDCSrc ,hDCMemory,hBitmap,hOldBitmap
local pPic,sStrm
local dwWidth,dwHeight,dwByte
local hPal,hPalPrev
local hGlobal,hMem,lSize,@pData
local HasPaletteScrn, PaletteSizeScrn
local LogPal :LOGPALETTE
local pic :PICTDESC_BMP
Local @hFile:dword

invoke GetSystemMetrics,SM_CXSCREEN
mov dwWidth,eax
invoke GetSystemMetrics,SM_CYSCREEN
mov dwHeight,eax
invoke GetDC,0
mov hDCSrc,eax
invoke CreateCompatibleDC,eax
mov hDCMemory,eax
invoke CreateCompatibleBitmap,hDCSrc,dwWidth,dwHeight
mov hBitmap,eax
invoke SelectObject,hDCMemory,eax
mov hOldBitmap,eax
invoke GetDeviceCaps,hDCSrc,RASTERCAPS;Raster
and eax,RC_PALETTE
mov HasPaletteScrn,eax  ;Palette
invoke GetDeviceCaps,hDCSrc,SIZEPALETTE ;Size of
mov PaletteSizeScrn,eax
.if HasPaletteScrn && (PaletteSizeScrn == 256)
mov LogPal.palVersion , 300h
mov LogPal.palNumEntries , 256
invoke GetSystemPaletteEntries,hDCSrc,0,256,addr LogPal.palPalEntry
invoke CreatePalette,addr LogPal
mov hPal,eax
invoke SelectPalette,hDCMemory,hPal,0
mov hPalPrev,eax
invoke RealizePalette,hDCMemory
.endif
invoke BitBlt,hDCMemory,0,0,dwWidth,dwHeight,hDCSrc,0,0,SRCCOPY
invoke SelectObject,hDCMemory,hOldBitmap
mov hBitmap,eax
.if HasPaletteScrn && (PaletteSizeScrn == 256)
  invoke SelectPalette,hDCMemory,hPalPrev,0
  mov hPal,eax
.endif
invoke ReleaseDC,0,hDCSrc
invoke DeleteDC,hDCMemory
 
mov pic.cbSizeOfStruct,sizeof pic ; Length of structure
mov pic.PicType,1 ; Type of Picture (bitmap)
mov eax,hBitmap
mov pic.hBitmap,eax ; Handle to bitmap
mov eax,hPal
mov pic.hPal,eax ; Handle to palette (may be null)

invoke OleCreatePictureIndirect,addr pic,addr IID_IPicture,TRUE,addr pPic
.if eax!=S_OK
invoke MessageBox,0,0,0,0
.endif
invoke CreateStreamOnHGlobal,NULL,TRUE,addr sStrm
mov ebx,pPic
mov ebx,
invoke (IPicture ptr ).SaveAsFile,pPic,sStrm,TRUE,addr dwByte          ;;;;;Here  ERROR!!!
.if eax==S_OK
  invoke MessageBox,0,SADD("Sok_Error Occured"),SADD("test"),MB_OK
.endif
  invoke GetHGlobalFromStream,sStrm,hGlobal
invoke GlobalAlloc,GPTR ,eax
mov hMem,eax
invoke CreateFile,SADD("scr_1.bmp"),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
mov @hFile,eax
invoke WriteFile,@hFile,hMem,lSize,addr dwWrite, NULL
invoke CloseHandle,@hFile
invoke GlobalUnlock,hGlobal
invoke GlobalFree,hMem
 
ret
_CreateBitmapPicture endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
invoke GetModuleHandle,NULL
invoke SetUnhandledExceptionFilter, ADDR ExceptionFilter
; install the SEH-frame
assume  fs:nothing
push  OFFSET ErrorHandler
push  FS:[0]
mov  , esp
mov  , ebp
mov  , ebx
mov  , esi
mov  , edi
mov  , OFFSET @@safe
mov  FS:[0], esp

; critical code
; zero uninitialized data
;==============================================================================
; here is your code
invoke _CreateBitmapPicture
;mov eax,0
;mov ecx,2
;div ecx
;==============================================================================
push  edi
push  esi
push  ebx
@@exit: 
pop  ebx
pop  esi
pop  edi
@@safe: 
pop  FS:[0]
jmp  ExitProcess
invoke ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

end start

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

no error happens, can't do anything,except created a bmp file with 0 byte.
Posted on 2005-09-06 13:33:54 by dcskm4200
worked code.
.386
.model flat, stdcall
option casemap :none
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_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*4]: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
local buffer [256]:BYTE

        invoke GetDC,0
        mov hdc,eax

        invoke GetDeviceCaps, hdc, HORZRES
        mov dwWidth,eax
        invoke GetDeviceCaps, hdc, VERTRES
        mov dwHeight,eax

        invoke GetDeviceCaps, hdc, BITSPIXEL
        mov dwBPP,eax
        .IF (eax<=8)
mov dwNumColors,256
        .ELSE
mov dwNumColors,0
        .ENDIF
       
invoke CreateCompatibleDC, hdc
mov memdc,eax

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
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 bmpinfo.bmiHeader.biClrUsed,0
mov bmpinfo.bmiHeader.biClrImportant,0
invoke CreateDIBSection,hdc,addr bmpinfo, DIB_PAL_COLORS,addr pBits, NULL, 0
mov hBitmap,eax
invoke SelectObject, memdc, hBitmap
invoke BitBlt, memdc,0,0, dwWidth, dwHeight, hdc, 0,0, SRCCOPY
mov eax,dwNumColors
.IF (eax!=0)
invoke GetDIBColorTable, hdc,0, dwNumColors, addr colors
mov dwNumColors,eax
.ENDIF

        mov bitmapfileheader.bfType,4D42h
        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
        mov bitmapinfoheader.biSizeImage,eax
        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
        mov bitmapinfoheader.biBitCount,ax
        mov bitmapinfoheader.biCompression,BI_RGB     
        mov bitmapinfoheader.biXPelsPerMeter,0
        mov bitmapinfoheader.biYPelsPerMeter,0
        mov bitmapinfoheader.biClrUsed,0
        mov bitmapinfoheader.biClrImportant,0

        invoke CreateFile, lpFileName,GENERIC_WRITE,0,\
                NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
        mov hFile,eax
        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 DeleteObject ,hBitmap
        invoke DeleteDC, memdc
        invoke ReleaseDC,0,hdc
ret
_CapScreen endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
invoke _CapScreen, SADD("Scr_1.bmp")
invoke ExitProcess,0
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

end start

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Posted on 2005-09-06 19:13:31 by dcskm4200
Tks dcskm4200!

in your test,
no error happens, can't do anything,except created a bmp file with 0 byte.


but in my pc,have a error,SHE Dilog hint 1 error occured.


Why?
Attachments:
Posted on 2005-09-06 20:45:42 by soulee
Hi,dcskm4200:
Your post a worked code,
Now have a small problem.

Please see the diagram.

Attachments:
Posted on 2005-09-06 21:11:47 by soulee
sorry,I forgot to say i test in 256 colors.
16 bit and 24 bit no problem.
Posted on 2005-09-06 21:16:11 by soulee
hey,soulee

yes, there is an error on running. it is a difficult problem toward me. I'm going to errands. next time, i'll try together with you.

regards
Posted on 2005-09-07 00:36:35 by dcskm4200

hello,

the version using IPicture contained some errors, the most severe one using a wrong IID for IPicture.

I did some modifications and now this version seems to run



.386
.Model Flat, StdCall
Option Casemap :None 
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Include \masm32\include\windows.inc
Include \masm32\include\user32.inc
Include \masm32\include\kernel32.inc
INCLUDE \masm32\include\Gdi32.inc
INCLUDE \masm32\include\oleaut32.inc
INCLUDE \masm32\include\ole32.inc

IncludeLib \masm32\lib\user32.lib
IncludeLib \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\Gdi32.lib
INCLUDELIB \masm32\lib\oleaut32.lib
INCLUDELIB \masm32\lib\ole32.lib

INCLUDE \masm32\macros\macros.asm
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IPicture_SaveAsFileProto    typedef proto :DWORD, :DWORD,:DWORD,:DWORD
IPicture_SaveAsFile          typedef ptr IPicture_SaveAsFileProto
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IPicture STRUCT
    ; IUnknown methods
    QueryInterface          dd  ?
    AddRef                  dd  ?
    Release                dd  ?
    ; IPicture methods
    get_Handle              dd  ?
    get_hPal                dd  ?
    get_Type                dd  ?
    get_Width              dd  ?
    get_Height              dd  ?
    Render                  dd  ? 
    set_hPal                dd  ?
    get_CurDC              dd  ?
    SelectPicture          dd  ?
    get_KeepOriginalFormat  dd  ?
    put_KeepOriginalFormat  dd  ?
    PictureChanged          dd  ?
    SaveAsFile              IPicture_SaveAsFile  ?
    get_Attributes          dd  ?
IPicture ENDS
;--------------------------------------------------------
PICTDESC_BMP STRUCT
    cbSizeOfStruct        dd  ?
    PicType          dd  ?
    hBitmap          dd  ?
    hPal          dd  ?
;    Reserved          dd  ?
PICTDESC_BMP ENDS
;--------------------------------------------------------
protoRelease  typedef proto :DWORD
pRelease      typedef ptr protoRelease
IStream_SetSizeProto    typedef proto :DWORD, :DWORD
IStream_SetSize          typedef ptr IStream_SetSizeProto
IStream STRUCT
    ; IUnknown methods
    QueryInterface          dd  ?
    AddRef                  dd  ?
    Release                pRelease ?
    ; IStream methods
    Read          dd  ?
    Write          dd  ?
    Seek          dd  ?
    SetSize          IStream_SetSize  ?
    CopyTo          dd  ?
    Commit          dd  ?
    Revert          dd  ?
    LockRegion          dd  ?
    UnlockRegion      dd  ?
    Stat          dd  ?
    Clone          dd  ?
IStream ENDS
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data
      dwWrite  dw 0
;  IID_IPicture  GUID <00020400h,0000h,0000h,<0C0h,000h, 000h,000h, 000h, 000h, 000h,046h>>
    IID_IPicture  GUID <7BF80980h,0BF32h,101Ah,<8Bh,0BBh,00h,0AAh,00h,30h,0Ch,0ABh>>


;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
  szMessage  db 256 dup (?)
  seh      dd 6 dup (?)
  buffer      db 256 dup(?)
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.const
  szErrorThread  db 13, 10, "Error at %08Xh", 13, 10, "Registers:", 13, 10, "eax = %08Xh ebx = %08Xh ecx = %08Xh", 13, 10, "edx = %08Xh esp = %08Xh ebp = %08Xh", 13, 10, "esi = %08Xh edi = %08Xh", 13, 10, 13, 10, "Recovering...", 13, 10,0
  szErrorFinal  db 13, 10, "Error at %08Xh", 13, 10, "Quitting...", 13, 10, 0
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

m2m macro dest, src
push src
    pop dest
endm

ErrorHandler proc C lpExcept:DWORD, lpFrame:DWORD, lpContext:DWORD, lpDispatch:DWORD
  mov  eax, lpExcept
  mov  ecx, lpContext
  invoke  wsprintf, ADDR szMessage, ADDR szErrorThread, ,\
      , , , ,\
      , , ,
  invoke MessageBox,NULL,  ADDR szMessage,  SADD("1_Error Occured"),MB_ICONEXCLAMATION

  mov  eax, lpContext
  m2m  ,
  m2m  ,
  m2m  ,
  m2m  ,
  m2m  ,
  m2m  ,

  xor  eax, eax  ; continue execution
  ret
ErrorHandler endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ExceptionFilter proc lpExcept:DWORD
  mov eax, lpExcept
  invoke wsprintf, ADDR szMessage, ADDR szErrorFinal,
  invoke MessageBox,NULL,  ADDR szMessage,  SADD("2_Error Occured"),MB_ICONEXCLAMATION
  invoke ExitProcess, 0
  xor  eax, eax
  inc  eax      ; EXCEPTION_EXECUTE_HANDLER
  ret
ExceptionFilter endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_CreateBitmapPicture proc
  local hDCSrc ,hDCMemory,hBitmap,hOldBitmap
  local pPic,sStrm
  local dwWidth,dwHeight,dwByte
  local hPal,hPalPrev
  local hGlobal,hMem,lSize,@pData
  local HasPaletteScrn, PaletteSizeScrn
  local LogPal  :LOGPALETTE
  local pic  :PICTDESC_BMP
  Local @hFile:dword

  mov hPal,NULL
  invoke  GetSystemMetrics,SM_CXSCREEN
  mov dwWidth,eax
  invoke  GetSystemMetrics,SM_CYSCREEN
  mov dwHeight,eax
  invoke GetDC,0
  mov hDCSrc,eax 
  invoke CreateCompatibleDC,eax
  mov hDCMemory,eax
  invoke CreateCompatibleBitmap,hDCSrc,dwWidth,dwHeight
  mov hBitmap,eax
  invoke SelectObject,hDCMemory,eax
  mov hOldBitmap,eax
  invoke GetDeviceCaps,hDCSrc,RASTERCAPS;Raster
  and eax,RC_PALETTE
  mov HasPaletteScrn,eax  ;Palette
  invoke GetDeviceCaps,hDCSrc,SIZEPALETTE  ;Size of
  mov PaletteSizeScrn,eax
  .if HasPaletteScrn && (PaletteSizeScrn == 256)
      mov LogPal.palVersion , 300h
      mov LogPal.palNumEntries , 256
      invoke GetSystemPaletteEntries,hDCSrc,0,256,addr LogPal.palPalEntry
      invoke CreatePalette,addr LogPal
      mov hPal,eax
      invoke SelectPalette,hDCMemory,hPal,0
      mov hPalPrev,eax
      invoke RealizePalette,hDCMemory
  .endif
  invoke BitBlt,hDCMemory,0,0,dwWidth,dwHeight,hDCSrc,0,0,SRCCOPY
  invoke SelectObject,hDCMemory,hOldBitmap
  mov hBitmap,eax
  .if HasPaletteScrn && (PaletteSizeScrn == 256)
        invoke SelectPalette,hDCMemory,hPalPrev,0
        mov hPal,eax
  .endif
  invoke ReleaseDC,0,hDCSrc
  invoke DeleteDC,hDCMemory
 
  mov pic.cbSizeOfStruct,sizeof pic      ; Length of structure
  mov pic.PicType,1            ; Type of Picture (bitmap)
  mov eax,hBitmap
  mov pic.hBitmap,eax            ; Handle to bitmap
  mov eax,hPal
  mov pic.hPal,eax            ; Handle to palette (may be null)

  invoke OleCreatePictureIndirect,addr pic,addr IID_IPicture,TRUE,addr pPic
  .if eax!=S_OK
      invoke MessageBox,0,SADD("OleCreatePictureIndirect"),0,0
  .endif
  invoke CreateStreamOnHGlobal,NULL,TRUE,addr sStrm
  mov ebx,pPic
  mov ebx,
  invoke (IPicture ptr ).SaveAsFile,pPic,sStrm,TRUE,addr lSize          ;;;;;Here  ERROR!!!
  .if eax!=S_OK
        invoke MessageBox,0,SADD("IPicture.SaveAsFile"),0,MB_OK
  .endif
  invoke GetHGlobalFromStream,sStrm,addr hGlobal
  .if (eax == S_OK)
  invoke CreateFile,SADD("scr_1.bmp"),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
  mov @hFile,eax
      invoke GlobalLock, hGlobal
      mov ecx, eax
  invoke WriteFile,@hFile,ecx,lSize,addr dwWrite, NULL
      invoke GlobalUnlock, hGlobal
  invoke CloseHandle,@hFile
  .endif
  mov ebx,sStrm
  mov ebx,
  invoke (IStream ptr ).Release,sStrm
 
  ret
_CreateBitmapPicture endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
  invoke GetModuleHandle,NULL
  invoke SetUnhandledExceptionFilter, ADDR ExceptionFilter
  ; install the SEH-frame
  assume  fs:nothing
  push  OFFSET ErrorHandler
  push  FS:[0]
  mov  , esp
  mov  , ebp
  mov  , ebx
  mov  , esi
  mov  , edi
  mov  , OFFSET @@safe
  mov  FS:[0], esp
 
  ; critical code
  ; zero uninitialized data
;==============================================================================
  ; here is your code
  invoke _CreateBitmapPicture
  ;mov eax,0
  ;mov ecx,2
  ;div ecx
;==============================================================================
  push  edi
  push  esi
  push  ebx
@@exit: 
  pop  ebx
  pop  esi
  pop  edi
@@safe: 
  pop  FS:[0]
  jmp  ExitProcess
  invoke  ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

end start


Posted on 2005-09-07 06:16:17 by japheth
Hey ,jaheth:

Thanks for your reply!

You modify the code works  in 16 bits or 32bits system very good.

but it run in 8 bits has a wrong hapend .

IPicture.SaveAsFile method returns not is  S_OK.
Result is make a zero byte .bmp file.

Still IID error?

Posted on 2005-09-07 07:23:52 by soulee
> Still IID error?

No. And I cant help because on my system it works for 8bits as well :)

Posted on 2005-09-07 08:52:01 by japheth
Hey,jaheth:
No. And I cant help because on my system it works for 8bits as well


Can you post a your bilud exe file to me?


I visit your web site. I find a good tool ::ComView.
It is a excellent tool.

I find  "IUnknown {00000000-0000-0000-C000-000000000046}" ,this GUID also can work in 16bits or 32bits.
but same,in 8bits don't work.

My asm code come from a Visual  Basic code.
it can work very good in all bits.
it use SavePicture to save file.

'Create Picture
Private Const RC_PALETTE As Long = &H100
Private Const SIZEPALETTE As Long = 104
Private Const RASTERCAPS As Long = 38
Private Type PALETTEENTRY
   peRed As Byte
   peGreen As Byte
   peBlue As Byte
   peFlags As Byte
End Type
Private Type LOGPALETTE
   palVersion As Integer
   palNumEntries As Integer
   palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors
End Type
Private Type GUID
   Data1 As Long
   Data2 As Integer
   Data3 As Integer
   Data4(7) As Byte
End Type
Private Type PicBmp
   Size As Long
   Type As Long
   hBmp As Long
   hPal As Long
   Reserved As Long
End Type
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
Private Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
Private Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As IPicture
   Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID

   'Fill GUID info
   With IID_IDispatch
       .Data1 = &H20400
       .Data4(0) = &HC0
       .Data4(7) = &H46
   End With

   'Fill picture info
   With Pic
       .Size = Len(Pic) ' Length of structure
       .Type = vbPicTypeBitmap ' Type of Picture (bitmap)
       .hBmp = hBmp ' Handle to bitmap
       .hPal = hPal ' Handle to palette (may be null)
   End With

   'Create the picture
   R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)

   'Return the new picture
   Set CreateBitmapPicture = IPic
End Function

Public Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As IPicture
   Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, R As Long
   Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long, HasPaletteScrn As Long
   Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE

   'Create a compatible device context
   hDCMemory = CreateCompatibleDC(hDCSrc)
   'Create a compatible bitmap
   hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
   'Select the compatible bitmap into our compatible device context
   hBmpPrev = SelectObject(hDCMemory, hBmp)

   'Raster capabilities?
   RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
   'Does our picture use a palette?
   HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
   'What's the size of that palette?
   PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of

   If HasPaletteScrn And (PaletteSizeScrn = 256) Then
       'Set the palette version
       LogPal.palVersion = &H300
       'Number of palette entries
       LogPal.palNumEntries = 256
       'Retrieve the system palette entries
       R = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
       'Create the palette
       hPal = CreatePalette(LogPal)
       'Select the palette
       hPalPrev = SelectPalette(hDCMemory, hPal, 0)
       'Realize the palette
       R = RealizePalette(hDCMemory)
   End If

   'Copy the source image to our compatible device context
   R = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)

   'Restore the old bitmap
   hBmp = SelectObject(hDCMemory, hBmpPrev)

   If HasPaletteScrn And (PaletteSizeScrn = 256) Then
       'Select the palette
       hPal = SelectPalette(hDCMemory, hPalPrev, 0)
   End If

   'Delete our memory DC
   R = DeleteDC(hDCMemory)

   Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
   
End Function


Posted on 2005-09-07 08:54:46 by soulee

> I visit your web site. I find a good tool ::ComView.

Oh thanks for reminding me. totally forgotten this thing  ;).

I checked the code once again. As I said, it worked on my system with 8bit palette, but now I wonder why, because there is another error in it:

  local LogPal  :LOGPALETTE

The LOGPALETTE struct is dangerous, because it has a variable sized array at its end (with items of type PALETTEENTRY). Your code fills the palNumEntries field with 256, but the declaration on the stack has only room for 1 entry, which is a pretty severe error. It didn't crash on my system because the GetSystemPaletteEntries function call - and its successors - failed.

I would suggest to move the LogPal to the .DATA? section, with 255 PALETTEENTY items behind it as it is done in this code. May be it helps.



.386
.Model Flat, StdCall
Option Casemap :None 
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Include \masm32\include\windows.inc
Include \masm32\include\user32.inc
Include \masm32\include\kernel32.inc
INCLUDE \masm32\include\Gdi32.inc
INCLUDE \masm32\include\oleaut32.inc
INCLUDE \masm32\include\ole32.inc

IncludeLib \masm32\lib\user32.lib
IncludeLib \masm32\lib\kernel32.lib
INCLUDELIB \masm32\lib\Gdi32.lib
INCLUDELIB \masm32\lib\oleaut32.lib
INCLUDELIB \masm32\lib\ole32.lib

INCLUDE \masm32\macros\macros.asm
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IPicture_SaveAsFileProto    typedef proto :DWORD, :DWORD,:DWORD,:DWORD
IPicture_SaveAsFile          typedef ptr IPicture_SaveAsFileProto
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IPicture STRUCT
    ; IUnknown methods
    QueryInterface          dd  ?
    AddRef                  dd  ?
    Release                dd  ?
    ; IPicture methods
    get_Handle              dd  ?
    get_hPal                dd  ?
    get_Type                dd  ?
    get_Width              dd  ?
    get_Height              dd  ?
    Render                  dd  ? 
    set_hPal                dd  ?
    get_CurDC              dd  ?
    SelectPicture          dd  ?
    get_KeepOriginalFormat  dd  ?
    put_KeepOriginalFormat  dd  ?
    PictureChanged          dd  ?
    SaveAsFile              IPicture_SaveAsFile  ?
    get_Attributes          dd  ?
IPicture ENDS
;--------------------------------------------------------
PICTDESC_BMP STRUCT
    cbSizeOfStruct        dd  ?
    PicType          dd  ?
    hBitmap          dd  ?
    hPal          dd  ?
;    Reserved          dd  ?
PICTDESC_BMP ENDS
;--------------------------------------------------------
protoRelease  typedef proto :DWORD
pRelease      typedef ptr protoRelease
IStream STRUCT
    ; IUnknown methods
    QueryInterface          dd  ?
    AddRef                  dd  ?
    Release                pRelease ?
    ; IStream methods
    Read          dd  ?
    Write          dd  ?
    Seek          dd  ?
    SetSize        dd  ?
    CopyTo          dd  ?
    Commit          dd  ?
    Revert          dd  ?
    LockRegion          dd  ?
    UnlockRegion      dd  ?
    Stat          dd  ?
    Clone          dd  ?
IStream ENDS
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data
      dwWrite  dd 0
;  IID_IPicture  GUID <00020400h,0000h,0000h,<0C0h,000h, 000h,000h, 000h, 000h, 000h,046h>>
    IID_IPicture  GUID <7BF80980h,0BF32h,101Ah,<8Bh,0BBh,00h,0AAh,00h,30h,0Ch,0ABh>>


;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
  szMessage  db 256 dup (?)
  seh      dd 6 dup (?)
  buffer      db 256 dup(?)
  LogPal  LOGPALETTE <>
PALETTEENTRY 255 dup (<>)
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.const
  szErrorThread  db 13, 10, "Error at %08Xh", 13, 10, "Registers:", 13, 10, "eax = %08Xh ebx = %08Xh ecx = %08Xh", 13, 10, "edx = %08Xh esp = %08Xh ebp = %08Xh", 13, 10, "esi = %08Xh edi = %08Xh", 13, 10, 13, 10, "Recovering...", 13, 10,0
  szErrorFinal  db 13, 10, "Error at %08Xh", 13, 10, "Quitting...", 13, 10, 0
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

m2m macro dest, src
push src
    pop dest
endm

ErrorHandler proc C lpExcept:DWORD, lpFrame:DWORD, lpContext:DWORD, lpDispatch:DWORD
  mov  eax, lpExcept
  mov  ecx, lpContext
  invoke  wsprintf, ADDR szMessage, ADDR szErrorThread, ,\
      , , , ,\
      , , ,
  invoke MessageBox,NULL,  ADDR szMessage,  SADD("1_Error Occured"),MB_ICONEXCLAMATION

  mov  eax, lpContext
  m2m  ,
  m2m  ,
  m2m  ,
  m2m  ,
  m2m  ,
  m2m  ,

  xor  eax, eax  ; continue execution
  ret
ErrorHandler endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ExceptionFilter proc lpExcept:DWORD
  mov eax, lpExcept
  invoke wsprintf, ADDR szMessage, ADDR szErrorFinal,
  invoke MessageBox,NULL,  ADDR szMessage,  SADD("2_Error Occured"),MB_ICONEXCLAMATION
  invoke ExitProcess, 0
  xor  eax, eax
  inc  eax      ; EXCEPTION_EXECUTE_HANDLER
  ret
ExceptionFilter endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_CreateBitmapPicture proc
  local hDCSrc ,hDCMemory,hBitmap,hOldBitmap
  local pPic,sStrm
  local dwWidth,dwHeight,dwByte
  local hPal,hPalPrev
  local hGlobal,hMem,lSize,@pData
  local HasPaletteScrn, PaletteSizeScrn
  local pic  :PICTDESC_BMP
  Local @hFile:dword

  mov hPal,NULL
  invoke  GetSystemMetrics,SM_CXSCREEN
  mov dwWidth,eax
  invoke  GetSystemMetrics,SM_CYSCREEN
  mov dwHeight,eax
  invoke GetDC,0
  mov hDCSrc,eax 
  invoke CreateCompatibleDC,eax
  mov hDCMemory,eax
  invoke CreateCompatibleBitmap,hDCSrc,dwWidth,dwHeight
  mov hBitmap,eax
  invoke SelectObject,hDCMemory,eax
  mov hOldBitmap,eax
  invoke GetDeviceCaps,hDCSrc,RASTERCAPS;Raster
  and eax,RC_PALETTE
  mov HasPaletteScrn,eax  ;Palette
  invoke GetDeviceCaps,hDCSrc,SIZEPALETTE  ;Size of
  mov PaletteSizeScrn,eax
  .if HasPaletteScrn && (PaletteSizeScrn == 256)
      mov LogPal.palVersion , 300h
      mov LogPal.palNumEntries , 256
      invoke GetSystemPaletteEntries,hDCSrc,0,256,addr LogPal.palPalEntry
      invoke CreatePalette,addr LogPal
      mov hPal,eax
      invoke SelectPalette,hDCMemory,hPal,0
      mov hPalPrev,eax
      invoke RealizePalette,hDCMemory
  .endif
  invoke BitBlt,hDCMemory,0,0,dwWidth,dwHeight,hDCSrc,0,0,SRCCOPY
  invoke SelectObject,hDCMemory,hOldBitmap
  mov hBitmap,eax
  .if HasPaletteScrn && (PaletteSizeScrn == 256)
        invoke SelectPalette,hDCMemory,hPalPrev,0
;        mov hPal,eax
  .endif
  invoke ReleaseDC,0,hDCSrc
  invoke DeleteDC,hDCMemory
 
  mov pic.cbSizeOfStruct,sizeof pic      ; Length of structure
  mov pic.PicType,1            ; Type of Picture (bitmap)
  mov eax,hBitmap
  mov pic.hBitmap,eax            ; Handle to bitmap
  mov eax,hPal
  mov pic.hPal,eax            ; Handle to palette (may be null)

  invoke OleCreatePictureIndirect,addr pic,addr IID_IPicture,TRUE,addr pPic
  .if eax!=S_OK
      invoke MessageBox,0,SADD("OleCreatePictureIndirect"),0,0
  .endif
  invoke CreateStreamOnHGlobal,NULL,TRUE,addr sStrm
  mov ebx,pPic
  mov ebx,
  invoke (IPicture ptr ).SaveAsFile,pPic,sStrm,TRUE,addr lSize          ;;;;;Here  ERROR!!!
  .if eax!=S_OK
        invoke MessageBox,0,SADD("IPicture.SaveAsFile"),0,MB_OK
  .endif
  invoke GetHGlobalFromStream,sStrm,addr hGlobal
  .if (eax == S_OK)
  invoke CreateFile,SADD("scr_1.bmp"),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL
  mov @hFile,eax
      invoke GlobalLock, hGlobal
      mov ecx, eax
  invoke WriteFile,@hFile,ecx,lSize,addr dwWrite, NULL
      invoke GlobalUnlock, hGlobal
  invoke CloseHandle,@hFile
  .endif
  mov ebx,sStrm
  mov ebx,
  invoke (IStream ptr ).Release,sStrm
 
  ret
_CreateBitmapPicture endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
  invoke GetModuleHandle,NULL
  invoke SetUnhandledExceptionFilter, ADDR ExceptionFilter
  ; install the SEH-frame
  assume  fs:nothing
  push  OFFSET ErrorHandler
  push  FS:[0]
  mov  , esp
  mov  , ebp
  mov  , ebx
  mov  , esi
  mov  , edi
  mov  , OFFSET @@safe
  mov  FS:[0], esp
 
  ; critical code
  ; zero uninitialized data
;==============================================================================
  ; here is your code
  invoke _CreateBitmapPicture
  ;mov eax,0
  ;mov ecx,2
  ;div ecx
;==============================================================================
  push  edi
  push  esi
  push  ebx
@@exit: 
  pop  ebx
  pop  esi
  pop  edi
@@safe: 
  pop  FS:[0]
  jmp  ExitProcess
  invoke  ExitProcess,NULL
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

end start





Posted on 2005-09-07 09:40:16 by japheth
yes , I just  thought of it.
but I don't know to modify it.

I test your code. It is works as well in 8bits.

:P
japheth,thanks you very very much!

Posted on 2005-09-07 09:54:02 by soulee