Resolved writing memorystream to file

Hi everybody,

OE12.6, Win 8/10

I have a MemoryStream and want to save it to an external file. Found some progress-examples (Knowledebase, ProgressTalk), but none of them works.


Example 1 – using 'FileStream'. This gives 2 errors (at line "oFileStream:Write(oMemStream:GetBuffer(),":
984 - Error attempting to push run time parameters onto the stack.
<no-number> System.UnauthorizedAccessException: MemoryStream's internal buffer cannot be accessed

Code:
DEFINE VARIABLE oMemStream AS "System.IO.MemoryStream" NO-UNDO.
DEFINE VARIABLE oFileStream AS System.IO.FileStream NO-UNDO.
DEFINE VARIABLE cLocalfile AS CHAR NO-UNDO.
DEFINE VARIABLE lng AS INT NO-UNDO.

oMemStream = cast(e:Data:GetData("FileGroupDescriptor"), "System.IO.MemoryStream").
cLocalfile = "c:\temp\file.msg".
oFileStream = NEW System.IO.FileStream(cLocalfile, System.IO.FileMode:Create).
lng = oMemStream:length.

oFileStream:Write(oMemStream:GetBuffer(), 0, lng).
oFileStream:Close().

DELETE OBJECT oFileStream.
DELETE OBJECT oMemStream.


Example 2 – converting the stream to a longchar using 'StreamReader' (and then saving the longchar...)
This gives error 142 - Cannot update field at line lc = oStreamReader:ReadToEnd()

Code:
DEFINE VARIABLE oStreamReader AS "System.IO.StreamReader" NO-UNDO.
DEFINE VARIABLE lc            AS LONGCHAR                 NO-UNDO.

oMemStream = cast(e:Data:GetData("FileGroupDescriptor"), "System.IO.MemoryStream").
oMemStream:Position = 0.
oStreamReader = NEW System.IO.StreamReader(oMemStream).
lc = oStreamReader:ReadToEnd().

DELETE OBJECT oStreamReader.
DELETE OBJECT oMemStream.

As I mentioned before, I'm usually programming pure ABL and I'm not really familliar with this kind of coding. Maybe there are much easier ways to achieve what I want. Any help is greatly appreciated.

Wolf
 
Top