Passing a ref to a SafeArray to c# .net component from MFC

I have a .Net component that passes a stringArray to a client program via an out parameter.

This works fine from a .net client

and I have successfully passed a SafeArray from MFC to a .net component as an in parameter.

What seems to evade me is returning returning an array from a .net component via the CCW as an out parameter.

Can anyone help?

regards
Rob

[373 byte] By [RobertMagowan] at [2008-1-25]
# 1
Robert Magowan wrote:
I have a .Net component that passes a stringArray to a client program via an out parameter.

This works fine from a .net client

and I have successfully passed a SafeArray from MFC to a .net component as an in parameter.

What seems to evade me is returning returning an array from a .net component via the CCW as an out parameter.

Can anyone help?

regards
Rob


Rob,

In order to do this, you would declare the parameter as a ref parameter, like this:

void DoSomething(ref string[] arr)

It will then allow you to pass a reference to a SAFEARRAY and have it populated on return.

Hope this helps.

- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

casperOne at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2
Thanks for the reply,
However, I am doing this already, and the tlb file generated from Tlbexe thus shows that the SafeArray should be marshaled as an [In,Out] Parameter.

The following definition is generated in the .tlh file of the MFC client program
for the function in question

virtual HRESULT __stdcall raw_TranslateID_2 (
/*[in]*/ struct ID_TRANS_CTX xTransCtx,

/*[in]*/ SAFEARRAY * astrInputIdNames,

/*[in,out]*/ SAFEARRAY * * rastrOrigIdNames,

/*[in,out]*/ SAFEARRAY * * rastrTranslatedIdNames,

/*[out,retval]*/ long * pRetVal ) = 0;
where by I am successfully passing a pointer to a safearray in.

but when I try to examine the results for the [OUT] safe arrays, all I get is garbage.

What am I doing wrong....

RobertMagowan at 2007-8-21 > top of Msdn Tech,Visual C#,Visual C# Language...