A
Arnshea
(apologies for the crosspost)
I'm working with an MFC based COM object. From C# I'd like to be able
to call a method on the COM object that takes a string array and
modifies the contents.
Is there any way to do this with a variable length array?
I've only been able to get it to work with a fixed size array. The
relevant code snippets are below. Suggestions are greatly appreciated
(e.g., there's a better way to do it, the code will cause memory
leaks, etc...).
MFC DISPATCH MAP ENTRY:
DISP_FUNCTION_ID(CMyClass, "StrArrayFunc", dispidStrArrayFunc,
StrArrayFunc, VT_I4, VTS_VARIANT)
..H PROTOTYPE:
HRESULT StrArrayFunc(VARIANT &vArray);
..CPP:
HRESULT CMyClass::StrArrayFunc(VARIANT &vArray)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
SAFEARRAY **ppsa = V_ARRAYREF(&vArray);
// ...
}
MANAGED/C# CLIENT:
static int Main(string[] args)
{
String[] ar = new string[30];
for (int i=0; i < ar.Length; i++)
ar = "str";
Array a = (Array) ar;
MyCOMServer.MyClassClass mc = new MyClassClass();
mc.StrArrayFunc(ref a); // INTEROP call
string[] nAr = (string[]) a;
// ... remaining code accesses modified array through nAr
}
I'm working with an MFC based COM object. From C# I'd like to be able
to call a method on the COM object that takes a string array and
modifies the contents.
Is there any way to do this with a variable length array?
I've only been able to get it to work with a fixed size array. The
relevant code snippets are below. Suggestions are greatly appreciated
(e.g., there's a better way to do it, the code will cause memory
leaks, etc...).
MFC DISPATCH MAP ENTRY:
DISP_FUNCTION_ID(CMyClass, "StrArrayFunc", dispidStrArrayFunc,
StrArrayFunc, VT_I4, VTS_VARIANT)
..H PROTOTYPE:
HRESULT StrArrayFunc(VARIANT &vArray);
..CPP:
HRESULT CMyClass::StrArrayFunc(VARIANT &vArray)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
SAFEARRAY **ppsa = V_ARRAYREF(&vArray);
// ...
}
MANAGED/C# CLIENT:
static int Main(string[] args)
{
String[] ar = new string[30];
for (int i=0; i < ar.Length; i++)
ar = "str";
Array a = (Array) ar;
MyCOMServer.MyClassClass mc = new MyClassClass();
mc.StrArrayFunc(ref a); // INTEROP call
string[] nAr = (string[]) a;
// ... remaining code accesses modified array through nAr
}