473,413 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

COM Interop - Problems with marshalling .NET object to C++ VARIANT pointer

I have a COM interface declared in C++

Expand|Select|Wrap|Line Numbers
  1. // This interface has all required attributes to make it visible for .NET Interop so I skip them.
  2.  
  3. class IMyInterface
  4. {
  5. public:     
  6.       HRESULT MyMethod(int param1, VARIANT param2, VARIANT* param3) = 0;
  7. };
  8.  
This interface is implemented in C++.

Expand|Select|Wrap|Line Numbers
  1. class MyInterfaceImpl : IMyInterface
  2. {
  3. public:     
  4.       HRESULT MyMethod(int param1, VARIANT param2, VARIANT* param3)
  5. {
  6.       // Do some activities here…
  7.       Print(param1);    // Let’s say that Print() is a function which is able to display the content of any type
  8.       Print(param2);
  9.       Print(*param3);
  10.  
  11.       *param3 = 4444;   // Assign value and pass it back to caller.
  12. };
  13. };
  14.  
I also have this interface declaration in .NET assembly like shown below:

Expand|Select|Wrap|Line Numbers
  1. [ComImport]
  2. [Guid("11111111-1111-1111-1111-111111111111")] // Some GUID
  3. [TypeLibType(4160)]
  4. [ComVisible(true)]
  5. public interface IMyInterface
  6. {
  7.       [DispId(1)] void MyMethod(int param1, object param2, out object param3);
  8. }
  9.  
This interface is also implemented in C#. I’m facing the problem with passing ‘param3’ from .NET to C++. For example I have code:

Expand|Select|Wrap|Line Numbers
  1. // .NET code
  2. IMyInterface interfacePtr;   
  3. // Initialize ‘interfacePtr’. This part works fine so I’ll skip it.
  4.  
  5. int param1 = 1111;
  6. object param2 = 2222;
  7. object param3 = 3333;
  8. interfacePtr.MyMethod(param1, param2, out param3);
  9. Print(param3);
  10.  
The output of this program looks like:

================================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
Sep 27 '10 #1
0 1132

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Lars von Wedel | last post by:
Hello, I have problems to implement a C# component which is called by a VB6 application through COM. A method Item accepts a variant as a parameter which can contain either a string or an...
2
by: Jason Bell | last post by:
I know this functionality already exists in .NET 2.0 via Marshal.GetDelegateForFunctionPointer, but I'd rather not force my users to use a beta framework (not to mention the fact that I'm still...
1
by: weixian_shen | last post by:
I'm trying to call my DLL written in C, and got the error: "Cannot marshal field 'b' of type 'mystruct': There is no marshaling support for this type." The 2 functions in the DLL are: void...
0
by: Manny Bonet | last post by:
I downloaded some code from MSDN to allow the upload of a file from an ASP page. I know that .NET can do this in an easire fashion but converting all the code would just be too much of a pain in...
0
by: Boris | last post by:
The .NET documentation talks about blittable and non-blittable types. While objects of type System::Object* are non-blittable it doesn't say anything about objects of user-defined classes. All the...
0
by: tsansoterra | last post by:
I have an asp.net/vb.net app that opens Excel, creates a report and then saves the file for download. The problem I am having is the Excel process persists (visible under the Task Manager) after...
1
by: Peter Reid | last post by:
Hi I've created an interop dll with the following commands: tlbimp NegociosObjects.dll /keyfile:key.snk /out:NegociosObjectsInterop.dll gacutil -i NegociosObjectsInterop.dll Then, in a web...
4
by: john | last post by:
I'm having some Interop problems. I really need someone's help on this, i'm running out of ideas. I have upgraded my Data access controls to the latest ver. Now then i have a form with these...
0
by: dwinson | last post by:
I am writing an add-in in C# for a server written in VB6. In order for my add-in to work I need to implement this method: MyMethod(string inputData) .... so my C# code looks like this: ...
14
by: Bryan Parkoff | last post by:
Do you know that current C++ Compiler limits to 64KB segments in source code? It is good news that Microsoft Visual C++ 2005 has expanded to 4GB segments in source code. 4GB segment is ideal for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.