Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

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

$
0
0

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
                              L"ClassLibrary1.Class1");
    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
               BindingFlags_InvokeMethod,
               NULL,
               vtobj,
               psa);
    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)?


Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>