EditStreamReadM     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

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

         and     Hold, 0
        push     pMem
         pop     EditS.dwCookie
         mov     EditS.dwError, 0
         mov     EditS.pfnCallback, offset EditStreamReadM
      INVOKE     SendMessage, hREdit, EM_STREAMIN, SF_RTF, addr EditS
; If the input is RTF you can read it in as plain text
      INVOKE     SendMessage, hREdit, EM_STREAMIN, SF_TEXT, addr EditS
                              ; eax = bytes read
      INVOKE     HeapDestroy, hMem

;=====================================================================
; Edit Stream In Callback procedure - read from memory to a control
;=====================================================================
EditStreamReadM PROC  uses esi edi  dwCookie:DWORD, pbBuff, cb, pcb
         mov     ecx, cb             ; Block size for this callback
      .if BytesO > ecx
            sub     BytesO, ecx      ; BytesO = total bytes to read
      .else
            mov     ecx, BytesO
            and     BytesO, 0
      .endif
         mov     eax, pcb            ; Pointer to pcb
         mov     dword ptr[eax], ecx ; Actual bytes read for this callback
         mov     esi, dwCookie       ; Input
         add     esi, Hold           ; Apply offset to the input buffer
         mov     edi, pbBuff         ; OutPut, doesn't need the offset??
         add     Hold, ecx           ; Hold = accum offset for input buffer
         rep     movsb
         mov     eax, 0              ; Must return Zero
EditStreamReadM ENDP
