.NET allows you to expose components as COM and consume them from unmanaged code. There are many references on how to this (and you can only start with MSDN), and I will not talk about that part. What I want to explain here is something different. Suppose you have this interface:
[Guid("2F8433FE-4771-4037-B6B2-ED5F6585ED04")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IAccounts { [DispId(1)] string[] GetUsers(); }
Method GetUsers() returns an array on string representing the user names. But what if you also wanted the user passwords or addresses? Since this is exposed as COM, you cannot return an array of User. But you can return multiple arrays of string. So, how would you deal with out string[]? This is what I want to show you in this tutorial.
This is a .NET interface exposed to COM. It has two methods, GetUsers() that returns an array of string representing user names, and GetUsers2() that returns an array of strings as an output parameters and a bool as return type, indicating whether any user was found.
namespace SampleLibrary { [Guid("2F8433FE-4771-4037-B6B2-ED5F6585ED04")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IAccounts { [DispId(1)] string[] GetUsers(); [DispId(2)] bool GetUsers2(out string [] users); } }
And this is the implementation:
namespace SampleLibrary { [Guid("C4713144-5D29-4c65-BF9C-188B1B7CD2B6")] [ClassInterface(ClassInterfaceType.None)] [ProgId("SampleLibrary.DataQuery")] public class Accounts : IAccounts { List< string > m_users; public Accounts() { m_users = new List< string > { "marius.bancila", "john.doe", "anna.kepler" }; } #region IDataQuery Members public string[] GetUsers() { return m_users.ToArray(); } public bool GetUsers2(out string[] users) { users = m_users.ToArray(); return users.Length > 0; } #endregion } }
Note: If you are trying this example make sure you set the ComVisible attribute to true, either for each type or per assembly (in AssemblyInfo.cs)
[assembly: ComVisible(true)]
Second, you have to check the “Register for COM interop” setting in the Build page of the project properties.
The first thing to do in C++ is importing the .TLB file that was generated by regasm.exe.
#import "SampleLibrary.tlb" using namespace SampleLibrary;
If we look in the .TLB file, we can see how the IAccounts interface looks like:
struct __declspec(uuid("2f8433fe-4771-4037-b6b2-ed5f6585ed04")) IAccounts : IDispatch { // // Wrapper methods for error-handling // // Methods: SAFEARRAY * GetUsers ( ); VARIANT_BOOL GetUsers2 ( SAFEARRAY * * users ); };
The following C++ functions, GetUsers1() retrieves the users users list using method GetUsers() from IAccounts. It puts the users in a CStringArray (notice that this container does not have an assignment operator, so the only way to return such an array is with a reference in the parameters list).
void GetUsers1(CStringArray& arrUsers) { IAccountsPtr pAccounts(__uuidof(Accounts)); SAFEARRAY* sarrUsers = pAccounts->GetUsers(); _variant_t varUsers; varUsers.parray = sarrUsers; varUsers.vt = VT_ARRAY | VT_BSTR; UnpackBstrArray(varUsers, arrUsers); SafeArrayDestroy(sarrUsers); pAccounts->Release(); }
UnpackBstrArray() is a function (see below) that extracts the elements of a SAFEARRAY and adds them to a CStringArray.
Function GetUsers2() uses the second method, GetUsers2() from IAccounts. This needs the address of a pointer to a SAFEARRAY (i.e. SAFEARRAY**) that will hold the values returned by the COM method. This time we have to create an empty SAFEARRAY and then pass its address to the COM method. The rest is similar to the previous case.
void GetUsers2(CStringArray& arrUsers) { IAccountsPtr pAccounts(__uuidof(Accounts)); SAFEARRAYBOUND aDim[1]; aDim[0].lLbound = 0; aDim[0].cElements = 0; SAFEARRAY* sarrUsers = SafeArrayCreate(VT_BSTR, 1, aDim); VARIANT_BOOL ret = pAccounts->GetUsers2(&sarrUsers); if(ret != VARIANT_FALSE) { _variant_t varUsers; varUsers.parray = sarrUsers; varUsers.vt = VT_ARRAY | VT_BSTR; UnpackBstrArray(varUsers, arrUsers); } SafeArrayDestroy(sarrUsers); pAccounts->Release(); }
The helper method UnpackBstrArray() used previous looks like this:
void UnpackBstrArrayHelper(VARIANT* pvarArrayIn, CStringArray* pstrarrValues) { if (!pstrarrValues || !pvarArrayIn || pvarArrayIn->vt == VT_EMPTY) return; pstrarrValues->RemoveAll(); VARIANT* pvarArray = pvarArrayIn; SAFEARRAY* parrValues = NULL; SAFEARRAYBOUND arrayBounds[1]; arrayBounds[0].lLbound = 0; arrayBounds[0].cElements = 0; if((pvarArray->vt & (VT_VARIANT|VT_BYREF|VT_ARRAY)) == (VT_VARIANT|VT_BYREF) && NULL != pvarArray->pvarVal && (pvarArray->pvarVal->vt & VT_ARRAY)) { pvarArray = pvarArray->pvarVal; } if (pvarArray->vt & VT_ARRAY) { if (VT_BYREF & pvarArray->vt) parrValues = *pvarArray->pparray; else parrValues = pvarArray->parray; } else return; if (parrValues != NULL) { HRESULT hr = SafeArrayGetLBound(parrValues, 1, &arrayBounds[0].lLbound); hr = SafeArrayGetUBound(parrValues, 1, (long*)&arrayBounds[0].cElements); arrayBounds[0].cElements -= arrayBounds[0].lLbound; arrayBounds[0].cElements += 1; } if (arrayBounds[0].cElements > 0) { for (ULONG i = 0; i < arrayBounds[0].cElements; i++) { LONG lIndex = (LONG)i; CString strValue = _T(""); VARTYPE vType; BSTR bstrItem; ::SafeArrayGetVartype(parrValues, &vType); HRESULT hr = ::SafeArrayGetElement(parrValues, &lIndex, &bstrItem); if(SUCCEEDED(hr)) { switch(vType) { case VT_BSTR: strValue = (LPCTSTR)bstrItem; break; } ::SysFreeString(bstrItem); } pstrarrValues->Add(strValue); } } } void UnpackBstrArray( const _variant_t &var, CStringArray &strarrValues ) { UnpackBstrArrayHelper( &(VARIANT)const_cast< _variant_t & >(var), &strarrValues ); }
Attached you can find a demo project (C# and C++) with the complete example show in this tutorial.
output SAFEARRAY** example (3390 downloads )