How to Load Binary Data into a PowerBuilder Blob

Question: 

I am using Chilkat in a PowerBuilder 9 (ANSI) application where I receive a JSON from a request. The JSON contains binary data (a file) encoded in Base64. I am able to handle and save this data to disk using the BinData component. However, I am struggling to pass that binary information to a PowerBuilder BLOB data type. How could I achieve this?

Answer:

You can get the contents of the Chilkat BinData object as a COM Variant by calling BinData.GetBinary.  See https://chilkatsoft.com/refdoc/xChilkatBinDataRef.html#method16

In the Chilkat ActiveX, methods that directly return binary data do so by returning a COM Variant.  So now the question becomes:

How to Load a PowerBuilder Blog with the contents of a COM Variant?

Loading a PowerBuilder BLOB (Binary Large Object) with the contents of a COM Variant involves getting the bytes that can be stored in the BLOB.

Assuming you have a BLOB variable in PowerBuilder named `lb_BinaryData` and a COM Variant named `comVariantData`, you can follow these steps:

// Declare BLOB variable
Blob lb_BinaryData

// Declare COM Variant
OLEObject comVariantData

// Assuming you have obtained or set the COM Variant with data
// ...

// Convert COM Variant to BLOB
Integer li_Length
li_Length = comVariantData.Length

// Resize the BLOB to match the length of the COM Variant
lb_BinaryData.Resize(li_Length)

// Use GetByteArray to retrieve the binary data from the COM Variant
comVariantData.GetByteArray(lb_BinaryData, 1, li_Length)

// Now, lb_BinaryData contains the binary data from the COM Variant