How to pass a System.Byte*

Posted by Simon L. Prinsloo on 31-May-2018 11:36

Hi,

I have a variable that is of type System.IntPtr, that I need to pass to a method as a System.Byte*.

In the example I have, the cast it like this:

(byte*)memIntPtr.ToPointer()

Is it possible to translate that to ABL, and how would I do that?

All Replies

Posted by Laura Stern on 31-May-2018 14:01

What method are you calling?

Posted by Richard.Kelters on 01-Jun-2018 02:52

You probably find a solution in this class

/*------------------------------------------------------------------------
File : bytearrayhelper
Purpose : 
Syntax : 
Description : 
Author(s) : rkelters
Created : Wed Jul 30 09:14:04 CEST 2014
Notes : 
----------------------------------------------------------------------*/

USING Progress.Lang.*.

ROUTINE-LEVEL ON ERROR UNDO, THROW.

CLASS util.bytearrayhelper: 
METHOD PUBLIC "System.Byte[]" MemptrToByteArray( pmptr AS MEMPTR ):
DEFINE VARIABLE nPtr AS System.IntPtr NO-UNDO.
DEFINE VARIABLE vInt AS INTEGER NO-UNDO.
DEFINE VARIABLE nBytes AS "System.Byte[]".

vInt = GET-SIZE(pmPtr).
nBytes = NEW "System.Byte[]"(vInt).
nPtr = NEW System.IntPtr(GET-POINTER-VALUE(pmPtr)).
System.Runtime.InteropServices.Marshal:Copy(nPtr, nBytes, 0, vInt).

RETURN nBytes.
FINALLY:
/* nPtr = ?.*/
DELETE OBJECT nPtr.
SET-SIZE(pmPtr) = 0.
nBytes = ?.
END.

END METHOD.

METHOD PUBLIC MEMPTR ByteArrayToMemptr( nBytes AS "System.Byte[]" ):
DEFINE VARIABLE nPtr AS System.IntPtr NO-UNDO.
DEFINE VARIABLE mPtr AS MEMPTR NO-UNDO.

set-size(mPtr) = nBytes:LENGTH.
nPtr = NEW System.IntPtr(GET-POINTER-VALUE(mPtr)).
System.Runtime.InteropServices.Marshal:Copy(nBytes, 0, nPtr, nBytes:LENGTH).
RETURN mPtr.
FINALLY:
/* nPtr = ?.*/
DELETE OBJECT nPtr.
nBytes = ?. 
SET-SIZE(mPtr) = 0.
END.
END METHOD.

METHOD PUBLIC LONGCHAR ByteArrayToLongChar( INPUT data AS "System.Byte[]" ):
DEFINE VARIABLE result AS LONGCHAR NO-UNDO.
DEFINE VARIABLE memStream AS "System.IO.MemoryStream" NO-UNDO.
DEFINE VARIABLE streamReader AS "System.IO.StreamReader" NO-UNDO.

IF VALID-OBJECT(data)
THEN DO:
memStream = NEW System.IO.MemoryStream(data).
memStream:Position = 0.
streamReader = NEW System.IO.StreamReader(memStream).
result = streamReader:ReadToEnd () .
DELETE OBJECT memStream.
DELETE OBJECT streamReader.
END.

RETURN result.

END METHOD.

END CLASS.

Posted by goo on 03-Feb-2020 14:04

ByteArrayHelper.cls does not work, gives an error when going from byte to memptr.

System.OverflowException: Value was either too large or too small for an int32

But I have no clue how to fix it :-)

This thread is closed