MIDL VARIANT-type to proper C# type

B

Bjoern

Hi

I have a COM object which fires an event where I have to pass an array
of SHORTs as a parameter to the listening object. The array of SHORTs
is encapsulated in a the CComSafeArray-class and passed as a VARIANT*
in MIDL (since MIDL doesn't support SAFEARRAYs in events).

In C# (.NET 1.0) the MIDL VARIANT* data-type is registered as "ref
object" in the event-handler (delegate). How do I convert this "ref
object" to a short array?

-- Bjoern
 
M

Mattias Sjögren

How do I convert this "ref
object" to a short array?

You probably only need a cast.

void YourEventHandler(ref object o)
{
short[] arr = (short[])o;
// Do stuff...
}

If that doesn't work please tell us what o.GetType().FullName returns.


Mattias
 
B

Bjoern

Hi

When I get the full path of the VARIANT* type it says
"System.Boolean".

Here's how my IDL event is declared:

HRESULT OnUserAudioData([in] LONG nUserID, [in] LONG nSampleRate, [in]
VARIANT* pRawAudio, [in] LONG nSamples);

Here's how I pass my data to the event:

//-- begin
Audio* aud = reinterpret_cast<Audio*> (lParam);
CComSafeArray<SHORT, VT_I2> safeArray;
if(aud->samples)
safeArray.Add(aud->samples, aud->rawAudio, TRUE);
CComVariant vtVar(safeArray);
m_pListener->Fire_OnUserAudioData(aud->userid, aud->samplerate,
&vtVar, aud->samples);
//-- end

Any idea why C# thinks it's a boolean?

-- Bjoern


How do I convert this "ref
object" to a short array?

You probably only need a cast.

void YourEventHandler(ref object o)
{
short[] arr = (short[])o;
// Do stuff...

}

If that doesn't work please tell us what o.GetType().FullName returns.

Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top