How to marshal safearray arguments when calling managed C# method from unmanaged C++? RRS feed

  • Question

  • I'm trying to utilize an old application framework from managed C# code but I don't understand how to marshal the safearray argument which is forwarded to the managed code. Here is a code snipped describing how the managed code is called:

      try {
        _ObjectHandlePtr pObjectHandle; 
        _ObjectPtr pObject; 
        _TypePtr pType;
        SAFEARRAY* psa;
        // Create an instance of a type from an assembly
        pObjectHandle = pDefaultDomain->CreateInstanceFrom(L"ClassLibrary1.dll",   // no path -- local directory
        variant_t vtobj = pObjectHandle->Unwrap();                   // Get an _Object (as variant) from the _ObjectHandle
        vtobj.pdispVal->QueryInterface(__uuidof(_Object),(void**)&pObject); // QI the variant for the Object iface
        pType = pObject->GetType();                             // Get the _Type iface
        psa = SafeArrayCreateVector(VT_VARIANT,0,1);               // Create a safearray (0 length)
    		LONG index = 0;
    		variant_t task = msg.task;
    		SafeArrayPutElement(psa, &index, &task);
    		//variant_t vtret;
        hr = pType->InvokeMember_3("Test",                           // Invoke "Test" method on pType
        SafeArrayDestroy(psa);                                  // Destroy safearray
      catch(_com_error& error) {
        _tprintf(TEXT("ERROR: %s\n"),(_TCHAR*)error.Description());
        goto exit;

    The msg.task is a plain unsigned int for testing purpose only. But how do I catch the psa safearray structure in the argument of the managed method (ClassLibrary1.Class1.Test)?

    Saturday, November 13, 2010 11:42 AM


All replies