Re: Reading a byte array from a Variant

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Ok here it is. My C program calls a .NET DLL (DLL-1) that calls another .NET
DLL (DLL-2).

In DLL-1, I have only one public method:

public object CallMethod(string assemblyName, string className, string
methodName, object parameters);

This method is exposed to COM like this when I look through OLE View:

HRESULT CallMethod(
[in] BSTR assemblyName,
[in] BSTR className,
[in] BSTR methodName,
[in] VARIANT parameters,
[out, retval] VARIANT* pRetVal);

In DLL-2, I have this method, that does nothing but return the array
received:

Public Function GetArray(ByRef b() As Byte) As Byte()
Return b
End Function

In my C program, I create an array of 5 bytes (that I convert to a Variant),
then I call my proxy method in DLL-1 which will in turn call the GetArray
method in DLL-2.

IMyProxy *pProxy = NULL;

HRESULT hr = CoCreateInstance(CLSID_MyProxy, NULL, CLSCTX_LOCAL_SERVER,
IID_IMyProxy, reinterpret_cast<void**> (&pProxy));

if (SUCCEEDED(hr))
{
BYTE params[5] = {84, 101, 115, 116, 33};
VARIANT vaParams = ConvertByteArrayToVariant(params, paramsSize); // see
below

VARIANT vaRet = pProxy->CallMethod("TestByteArray", "clsByteArray",
"GetArray", vaParams);

if vaRet.vt == (VT_ARRAY | VT_UI1))
{
unsigned long arraySize = vaRet.parray->rgsabound[0].cElements;
BYTE* pBuffer = new BYTE[arraySize ];

if(pBuffer != NULL)
{
void* pArrayData;

hr = SafeArrayAccessData(vaRetour.parray, &pArrayData);
if (SUCCEEDED(hr))
{
memcpy(ppBuf, pArrayData, pcBufLen);
SafeArrayUnaccessData(vaRet.parray);
}
}
}
}



VARIANT ConvertByteArrayToVariant(BYTE* pByteArray, int arrayLen)
{
VARIANT vaRet;
vaRet.vt = VT_NULL;

// Create safe array
SAFEARRAY* psa = SafeArrayCreateVector(VT_UI1, 0, arrayLen);
if (psa != NULL)
{
// Store elements into the SafeArray
BYTE HUGEP* pByte = pByteArray;
HRESULT hr = S_OK;
for (long i=0; i<arrayLen; i++)
{
hr = SafeArrayPutElement(psa, &i, pByte);
pByte++;
}

// Get a pointer to the elements of the array.
hr = SafeArrayAccessData(psa, (void HUGEP**)&pByte);

vaRet.vt = VT_ARRAY|VT_UI1;
vaRet.parray = psa;
}

return vaRet;
}

All of the above scheme works well when using VB6 instead of VC. It also
works well in VC when working with strings instead of byte arrays. Please
let me know if I'm missing something here...

- Jeff




.



Relevant Pages

  • Re: Split Function Creates Error 13 Type Mismatch
    ... A Variant can hold a string and an array of string. ... Dim vAs Variant ... As it is, was Variant accept an array of string returned by Array, but not by Split. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Grouping shapes in Word
    ... The reason that it expects a Variant array as opposed to a String array is that ... a Variant can hold data of any type. ... Variant array of Shape names (string) or a Variant array of shape index numbers ... Dim avarShape() As Variant ...
    (microsoft.public.office.developer.vba)
  • Re: Sorting a variant array
    ... Dim tempList As Variant ... Dim testerList() As String ... Function sortTesters(rangeName As String) ... column range--but even that ends up as a x-rows by 1 column array. ...
    (microsoft.public.excel.programming)
  • Re: Paasing an array in OpenArgs
    ... Although the OpenArgs property is a variant, the Access 2003 help file indicates it must contain a string expression. ... I thought a good way to do this would be to create an array with the two date values and pass the array in the openargs argument. ...
    (microsoft.public.access.formscoding)
  • Re: Populate listbox from array
    ... Static dbsAs String isn't required in your case: ... Static MyDeptArray As Variant ... > array. ... >> write a call-back function to populate your listbox. ...
    (microsoft.public.access.modulesdaovba)