Re: Reading a byte array from a Variant
- From: "Jeff Robichaud" <jfrobichaud@xxxxxxxxx>
- Date: Fri, 12 May 2006 12:57:04 -0400
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
.
- Follow-Ups:
- Re: Reading a byte array from a Variant
- From: Vi2
- Re: Reading a byte array from a Variant
- From: Brian Muth
- Re: Reading a byte array from a Variant
- References:
- Reading a byte array from a Variant
- From: Jeff Robichaud
- Re: Reading a byte array from a Variant
- From: Brian Muth
- Reading a byte array from a Variant
- Prev by Date: Re: help regarding internal comple error
- Next by Date: Re: COM question
- Previous by thread: Re: Reading a byte array from a Variant
- Next by thread: Re: Reading a byte array from a Variant
- Index(es):
Relevant Pages
|