A
A
Andrey Ovsyankin2014-06-02 12:21:36
C++ / C#
Andrey Ovsyankin, 2014-06-02 12:21:36

How to call a given function from an assembly in managed c++?

There is an assembly in C#, which has a public method that accepts System.Object, treating it like a COM object.
From C#, this method is used like this:

var ComType = Type.GetTypeFromProgID("Excel.Application");
var obj = Activator.CreateInstance(ComType);
ProcessObject(obj); // ProcessObject(System.Object objToProcess)

The ProcessObject function accepts an object, treating it like an IDispatch.
Challenge: call this function from a managed c++ assembly. I have a normal (unmanaged) pointer to IDispatch.
MyClass::MyMethod(IDispatch* pDesigner)
    {
  // здесь должен быть вызов функции ProcessObject 
        // из сборки на С# с передачей туда указателя pDesigner
    }

Somehow you need to convert it to System.Object and pass it to C#. Interested in the right way to do it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Ovsyankin, 2014-06-05
@EvilBeaver

The toaster is traditionally bad with specific answers. Only reasoning about "you are doing it wrong"
Here is a specific answer, in case it is useful to someone:

// IUnknown* pDesigner
IntPtr handle = IntPtr(pDesigner);
Object^ managedObject = System::Runtime::InteropServices::Marshal::GetObjectForIUnknown(handle);

A
AxisPod, 2014-06-05
@AxisPod

In general, read about Ole Automation first. For your task, you are using slightly the wrong thing. Then look at how it's done in .NET, there are tools out there. I did it myself in C ++ and C #, so in C ++ it's hell, it's better not to go there. But in C#, everything is fine, you don’t even have to think about COM, Ole in general.
UPD. At first I cheated with the version, it was on .net 1.1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question