Problems with converting a piece of .net code to progress - MemoryStream

Den Duze

Member
Hi,
I'm already struggling for a while with converting the following .net code to Progress (Drag-and-Drop Attached File From Outlook (97 and above) to C# Window Form).
The idea is to Drag&Drop some attachments from outlook to a progress .net form and then save those files in the DB.
It looks like this can work but I can not convert the code.
My biggest problem (I think) is the following part of that code.

byte [] fileGroupDescriptor = new byte[512];
theStream.Read(fileGroupDescriptor,0,512);
// used to build the filename from the FileGroupDescriptor block
StringBuilder fileName = new StringBuilder("");
// this trick gets the filename of the passed attached file
for(int i=76; fileGroupDescriptor!=0; i++)
{ fileName.Append(Convert.ToChar(fileGroupDescriptor));}
theStream.Close();

It looks like theStream in my code is a valid-object and is of the type MemoryStream and his length is 336 (so I guess that looks fine).
The problem is how to know the filename of that (and that is the code I mentioned here that I can't convert correctly so I get a name)

Regards
 

Osborne

Active Member
I think this could be a rough translation of the code:
Code:
DEFINE VARIABLE FileGroupDescriptor AS "System.Byte[]" NO-UNDO.
DEFINE VARIABLE theStream AS System.IO.MemoryStream NO-UNDO.
DEFINE VARIABLE fileName AS System.Text.StringBuilder NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.

FileGroupDescriptor = CAST(System.Array:CreateInstance(Progress.Util.TypeHelper:GetType("System.Byte"), 512),
                           "System.Byte[]").
theStream:Read(FileGroupDescriptor,0,512).

fileName = NEW System.Text.StringBuilder("").

i = 76.
DO WHILE FileGroupDescriptor <> 0:
   filename:Append(System.Convert:ToChar(FileGroupDescriptor)).
   i = i + 1.
END.

theStream:Close().
 
Last edited:

Den Duze

Member
Hi Osborne,

Thanks for this!!
But now I get an error on the line "filename:Append(System.Convert:ToChar(FileGroupDescriptor))."
I get:
System.InvalidCastException: Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.

If I understand your provided code a bit:
The cast will create array of 512 System.Byte variables
The read is not clear to me...
then the do while loops that array and if it has some value it will add that to the string
But why does that start at 76? I know it was in the example I provided but maybe you know why that is.

Regards
Didier
 

Osborne

Active Member
Unfortunately I have not worked with memory streams or System.Bytes so cannot answer your questions of what everything is actually doing.

The code posted was a best guess at what I thought the ABL code could be.

Having a fresh look though I wonder if the two lines should be something similar to this:
Code:
DO WHILE INTEGER(FileGroupDescriptor:GetValue(i):ToString()) <> 0:
   filename:Append(System.Convert:ToChar(FileGroupDescriptor:GetValue(i))).

Failing that, this article covers converting System.Byte to a character variable which may be a solution as you can parse that:


If binary though, maybe something like this:
Code:
DEFINE VARIABLE memptr AS MEMPTR NO-UNDO.
DEFINE VARIABLE oIntPointer AS System.IntPtr NO-UNDO.

SET-SIZE (memptr) = FileGroupDescriptor:Length .
oIntPointer = NEW System.IntPtr (GET-POINTER-VALUE (memptr)).
System.Runtime.InteropServices.Marshal:Copy (FileGroupDescriptor, 0, oIntPointer, FileGroupDescriptor:Length).

DELETE OBJECT oIntPointer.
// Remember to clean up memptr when finished.
 

Osborne

Active Member
A bit of a long shot, but if you read each byte and add the characters together does the file name appear anywhere?:
Code:
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE j AS INTEGER NO-UNDO.
DEFINE VARIABLE vCharacters AS CHARACTER NO-UNDO.

j = FileGroupDescriptor:Length - 1.
DO i = 0 TO j:
   vCharacters = vCharacters + System.Convert:ToChar(FileGroupDescriptor:GetValue(i)).
END.
MESSAGE vCharacters VIEW-AS ALERT-BOX.

Failing that, you could be right in that it does not quite work as it says.
 

Den Duze

Member
Still te same like may other things I tried, the vCharacters stays empty
The strange thing is when I do the same coding with e:Data:GetData("FileContents") (instead of e:Data:GetData("FileGroupDescriptor")) I get a result.
So no idea if that FileGroupDescriptor should still work or not and/or what's the difference with the FileContents.
I also found some other .net examples that should do the same adn now I'm trying to convert those .. maybe more luck that way.

Thanks for the help
 
Top