EditStreamWriteM     PROTO :DWORD, :DWORD, :DWORD, :DWORD

hMem           dd  0
pMem           dd  0
BytesO         dd  0
Hold           dd  0

;=====================================================================
; Allocate heap memory
;=====================================================================
         mov     BytesO, 1000000
      INVOKE     HeapCreate, HEAP_GENERATE_EXCEPTIONS, BytesO, 0
         mov     hMem, eax
      INVOKE     HeapAlloc, hMem, HEAP_GENERATE_EXCEPTIONS, BytesO
         mov     pMem, eax

;=====================================================================
; Writes to memory from a richedit control
;=====================================================================
LOCAL    EditS:EDITSTREAM

         and     Hold, 0
        push     pMem
         pop     EditS.dwCookie
         mov     EditS.dwError, 0
         mov     EditS.pfnCallback, offset EditStreamWriteM
      INVOKE     SendMessage, hREdit, EM_STREAMOUT, SF_RTF, addr EditS
; If the input is RTF you can write it out as plain text
      INVOKE     SendMessage, hREdit, EM_STREAMOUT, SF_TEXT, addr EditS
         mov     BytesO, eax   ; eax = bytes written

;=====================================================================
; Edit Stream Out Callback procedure - write to memory from a control
;=====================================================================
EditStreamWriteM PROC  uses ebx esi edi  dwCookie:DWORD, pbBuff, cb, pcb
         mov     ecx, cb             ; Block size for this callback
         mov     ebx, ecx
         mov     edi, dwCookie       ; Output
         add     edi, Hold           ; Apply offset to the output buffer
         mov     esi, pbBuff         ; InPut, doesn't need the offset??
         rep     movsb
         mov     eax, pcb            ; Pointer to pcb
         mov     dword ptr[eax], ebx ; Actual bytes written for this callback
         add     Hold, ebx           ; Hold = accum offset for input buffer
         mov     eax, 0              ; Must return zero
         ret
EditStreamWriteM ENDP
