T
Tronster Hartley
I've written an ATL 7.1 component and have a C# 1.1 client that is
consuming it. The ATL component has a method with a [out,retval] that
returns a SAFEARRAY of INTs, but my C# component chokes on it.
The C# code:
// Display a message box with a value from the ATL control
private void GoButton_Click(object sender, System.EventArgs e)
{
SAServerLib.bobClass foo = new SAServerLib.bobClass();
object o = foo.Populate();
MessageBox.Show( o.ToString() );
int[] ch = (int[])o;
MessageBox.Show( ch.ToString() );
}
I receive an exception on:
int[] ch = (int[])o;
With the message being:
"An unhandled exception of type 'System.InvalidCastException' occurred
in Safearrays.exe
Additional information: Specified cast is not valid."
ATL code:
// Populate a safe array with the value "321".
STDMETHODIMP Cbob:opulate(SAFEARRAY** saData)
{
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 3;
*saData = SafeArrayCreate(VT_BSTR, 1, rgsabound);
long c = 3;
long lCount = 0L;
SafeArrayPutElement(*saData, &lCount, static_cast<void*>(&c) );
c = 2;
lCount++;
SafeArrayPutElement(*saData, &lCount, static_cast<void*>(&c) );
c = 1;
lCount++;
SafeArrayPutElement(*saData, &lCount, static_cast<void*>(&c) );
return S_OK;
}
All of this code is from a sample application I'm just trying to prove
the concept with. (Eventually I'll be returning SAFEARRAY's of custom
structures.)
I've googled and MSDNed for some information, but Any advice would be
greatly appreciated.
Cheers.
consuming it. The ATL component has a method with a [out,retval] that
returns a SAFEARRAY of INTs, but my C# component chokes on it.
The C# code:
// Display a message box with a value from the ATL control
private void GoButton_Click(object sender, System.EventArgs e)
{
SAServerLib.bobClass foo = new SAServerLib.bobClass();
object o = foo.Populate();
MessageBox.Show( o.ToString() );
int[] ch = (int[])o;
MessageBox.Show( ch.ToString() );
}
I receive an exception on:
int[] ch = (int[])o;
With the message being:
"An unhandled exception of type 'System.InvalidCastException' occurred
in Safearrays.exe
Additional information: Specified cast is not valid."
ATL code:
// Populate a safe array with the value "321".
STDMETHODIMP Cbob:opulate(SAFEARRAY** saData)
{
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = 3;
*saData = SafeArrayCreate(VT_BSTR, 1, rgsabound);
long c = 3;
long lCount = 0L;
SafeArrayPutElement(*saData, &lCount, static_cast<void*>(&c) );
c = 2;
lCount++;
SafeArrayPutElement(*saData, &lCount, static_cast<void*>(&c) );
c = 1;
lCount++;
SafeArrayPutElement(*saData, &lCount, static_cast<void*>(&c) );
return S_OK;
}
All of this code is from a sample application I'm just trying to prove
the concept with. (Eventually I'll be returning SAFEARRAY's of custom
structures.)
I've googled and MSDNed for some information, but Any advice would be
greatly appreciated.
Cheers.