Expand|Select|Wrap|Line Numbers
- // This interface has all required attributes to make it visible for .NET Interop so I skip them.
- class IMyInterface
- {
- public:
- HRESULT MyMethod(int param1, VARIANT param2, VARIANT* param3) = 0;
- };
Expand|Select|Wrap|Line Numbers
- class MyInterfaceImpl : IMyInterface
- {
- public:
- HRESULT MyMethod(int param1, VARIANT param2, VARIANT* param3)
- {
- // Do some activities here…
- Print(param1); // Let’s say that Print() is a function which is able to display the content of any type
- Print(param2);
- Print(*param3);
- *param3 = 4444; // Assign value and pass it back to caller.
- };
- };
Expand|Select|Wrap|Line Numbers
- [ComImport]
- [Guid("11111111-1111-1111-1111-111111111111")] // Some GUID
- [TypeLibType(4160)]
- [ComVisible(true)]
- public interface IMyInterface
- {
- [DispId(1)] void MyMethod(int param1, object param2, out object param3);
- }
Expand|Select|Wrap|Line Numbers
- // .NET code
- IMyInterface interfacePtr;
- // Initialize ‘interfacePtr’. This part works fine so I’ll skip it.
- int param1 = 1111;
- object param2 = 2222;
- object param3 = 3333;
- interfacePtr.MyMethod(param1, param2, out param3);
- Print(param3);
================================Output============ ===============
1111
2222
Empty
4444
================================Output============ ===============
If I put a breakpoint at C++ code (i.e. in MyInterfaceImpl::MyMethod() function) I see that ‘param1’ is equal to ‘1111’, param2 is equal to ‘2222’ but param3 is always empty. But when param3 is assigned ‘4444’ value on C++ side .NET side successfully accepts it. As you can the problem occurs when I pass ‘param3’ – somehow C++ side is not able to receive/read it but it’s able to assign the appropriate value.
Any ideas what I’m doing wrong and how I can resolve the situation to make output look like:
================================Output============ ===============
1111
2222
3333
4444
================================Output============ ===============
Thanks in advance
Best regards
Serg