Passing Excel.Application object from C# to C++

E

excelthoughts

I am trying to pass an Excel.Application object from c# to c++ DLL via
PInvoke.

The c++ code expects an IDispatch.

The Pinvoke Wizard paulyao.com suggests:
[DllImport("myDLL.dll")]
public static extern short Initialise(ref LPDISPATCH pExcel);

so I have tried this code c#:

[DllImport("myDLL.dll")]
public static extern short
Initialise([MarshalAs(UnmanagedType.IDispatch)]ref object pExcel);

and

[DllImport("myDLL.dll")]
public static extern short
Initialise([MarshalAs(UnmanagedType.IUnknown)]ref object pExcel);

And this is the code that calls the routine.

Excel.Application excelApp= (Excel.Application)application; //(from
Connect class in COM Addin)
object o=(object)excelApp;
Initialise(ref o);

When the c++ code is called it throws a COleDispatchException error.
m_wCode is 00000000 and 80020005 respectively.
Any ideas on how I should pass the Excel Application object?


Regards
Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top