473,327 Members | 1,930 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,327 software developers and data experts.

Very strange RCW behaviour with VARIANTs/SAFEARRAYs in Managed C++

I got no response to this in microsoft.public.dotnet.framework.interop,
and since it is a C++-related problem, I am now trying it here:

I am using an Interop assembly for communication between my Managed C++
application and an ATL-based COM server. The Interop assembly was
created with Visual Studio, and the COM server is third-party software
that is not under my control.

One of the COM interfaces supported by the server has a method defined
like this:

HRESULT Func(
[out] VARIANT* bunchOfIntegers,
[out] VARIANT* bunchOfStrings);

(the actual function has a lot more parameters, but I have tried to
strip the example down to the necessary minimum).

The VARIANT bunchOfIntegers is documented as SAFEARRAY VT_I4, VT_BYREF,
while bunchOfStrings is actually a SAFEARRAY VT_BSTR, VT_BYREF, with the
added twist that it's a two-dimensional array. The caller is supposed to
pass these arrays which are then filled by the server.

The corresponding member function in the Interop assembly's wrapper
class is described by the object browser as

public virtual void Func (out object bunchOfIntegers,
out object bunchOfStrings);

A couple of minutes of intensive conversation with the compiler
convinced my that the corresponding C++ function prototype is

virtual void Func (System::Object __gc* __gc* bunchOfIntegers,
System::Object __gc* __gc* bunchOfStrings);

(why does the IDE not provide me with this information, by the way?)

The function documentation says that null pointers may be used to
suppress information selectively. Going for the simpler data structure
first, I eventually arrived at something along the lines of:

// count is already known at this point

int bunchOfIntegers __gc [] = new int __gc [count];
Object* bunchOfIntegersObject = bunchOfIntegers;
Object* bunchOfStringsObject = 0;

Func (&bunchOfIntegersObject, &bunchOfStringsObject);

While this code gives me the creeps (taking addresses of managed
pointers?!), it was the only form that placated the compiler. Now comes
the really weird thing. When I executed Func in the debugger,
bunchOfIntegers was untouched, but bunchOfStringsObject referred to an
allocated two-dimensional array with meaningful values returned by Func!
In other words, allocating an array did not work, but passing the
address of a managed null pointer did. Can anyone explain this? And can
anyone tell me whether these scary contortions are the correct way of
obtaining values for the described VARIANT out parameters?

--
Gerhard Menzl

Humans may reply by replacing the obviously faked part of my e-mail
address with "kapsch".
Nov 17 '05 #1
0 962

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
1
by: ralphsieminsky | last post by:
I am seeing a strange behavior with a managed/unmanaged C++ program. The program is a large application made of DLLs, COM components, and an exe. When I recompile the exe with /clr a problem...
1
by: Jitesh Sinha | last post by:
Hi, I am running Windows 2003/ IIS 6.0. I was stuck with rather a abnormal behaviour of System.Web.mail class. It was truncating the message body after 3,071 character. The code i was testing...
5
by: R. MacDonald | last post by:
Hello, all, I am currently working on a .Net (VB) application that invokes routines in unmanaged (Fortran) DLLs. The unmanaged routines then communicate with the .Net application by means of a...
4
by: Sean Hammond | last post by:
I've managed to create a scenario in which editing an object in a list of objects seems to edit every object in the list, rather than just the one. I'm totally stumped and wondered if anyone would...
7
by: ChrisM | last post by:
I posted this last week, so apologies for re-posting but I'm still looking for a sensible answer, and I'm hoping somone new might be able to cast some light... Basically, I have a fairly...
0
by: WTH | last post by:
....and you get safe array type mismatch exceptions from the marshaller. I have a C++ COM object (thank God I could find the source) that was declared properly in the IDL for and requiring a...
4
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
11
Dormilich
by: Dormilich | last post by:
recently I encounter a very strange behaviour of the session variable. if the cookies are disabled, the session id is totally misplaced (see code). instead being attached to the URL it is placed...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.