I developed a c++ ATL com dll. I'm trying to use it in C#.

In C# I have a method with a parametr of "ref object" type (in C++ is a VARIANT* I convert it to SAFEARRAY of doubles).
For use this method I create a double[] variable but it throws me this error:

Argument '3': cannot convert from 'ref double[]' to 'ref object'


If I create
object x =null;

And after call to method I execute: double[] myarray = (double []) x;
It run ok, but I would like avoid those steps.
Thanksss


_________________________
Hip Hop Directo
De Chiste

Maybe it's because I'm just learning this stuff, but what exactly are you trying to accomplish?

If we're always going to have a value of type double, why not just cast the object to double, have an array, and add it to that array? Will this array have other values later on?

Like I said, I'm kind of new at C#. But, it seems like there might be a better way. Will x ever be brought in as an array itself, or will it always be a single object?

Maybe it's because I'm just learning this stuff, but what exactly are you trying to accomplish?

I want pass a double array from c# to a c++ dll.

If we're always going to have a value of type double, why not just cast the object to double, have an array, and add it to that array? Will this array have other values later on?

I will be always array of doubles, but passing to c++ dll it have to be a VARIANT* in c++.
When and how can I do cast in c#?

A cast is what you're doing when you do this code:

object x =null;
double[] myarray = (double []) x;

basically, the (double []) part converts x from type object to type double. I assume that it's passing the contents of x, as a double, into myarray?

Can the DLL you wrote support Generics? If so, maybe you could create a generic collection (you can have a collection that contains different types of objects), and pass that to the DLL.

Hi,

There are lots of different "variant" type than you think. The COM object model uses a common variant type called OLEVariant while all languages have their own internal variant types. (VB6, Delphi) Java and C# even don't have a variant type but use a technique called boxing which basicly is to type-cast anything to a Object and later back to the former type. That's why for inter-platform (between .Net, COM, Win32 API(c++), Java) inter-operability it is wiser to use some form of serialization for non-basic data structures.

Loren Soth

Can the DLL you wrote support Generics? If so, maybe you could create a generic collection (you can have a collection that contains different types of objects), and pass that to the DLL.

sorry, I don't understan what are you meaning with generics

Hi,

There are lots of different "variant" type than you think. The COM object model uses a common variant type called OLEVariant while all languages have their own internal variant types. (VB6, Delphi) Java and C# even don't have a variant type but use a technique called boxing which basicly is to type-cast anything to a Object and later back to the former type. That's why for inter-platform (between .Net, COM, Win32 API(c++), Java) inter-operability it is wiser to use some form of serialization for non-basic data structures.

Loren Soth

Hi,
How is the best type for define the parameter in c++ atl dll?
Now, I'm using c++ VARIANT *.

Hi,

C++ variant and olevariant are two different things. You must use OleVariant on your COM DLL. Normally most of time OleVariant succesfully translates back and forth to System.Object but in case it won't (complex data type in OleVariant) then you can use something like :

Object[] p1= new Object[1] { "someting" };
IntPtr OleVariant= new IntPtr(1);
Marshal.GetNativeVariantForObject(p1,OleVariant);
Array myArray= (Array)(myComObject.myMethod(OleVariant));

Loren Soth

I'm searching in google about olevariant in C++ but I didn't find any example of use it in c++. It is the common form to pass array of doubles? are you sure?
Thanks

Hi,

I assume you use VC++ 6.0 or VC++.Net (7.0) with unmanaged code in those the default variant type might be the same as OleVariant (especially when you select COM object as project type) which isn't a binary data type but accessed through COM automation interfaces. (Bur for Borland C++ Builder, gcc, DevC++, Delphi, VB6 and VB/Java scripting those are two different variant types)
In this case your problem might be the ancapsulation of an array of a type in variant (variant of type array of type xxx) below are some links you might be interrested. They mention the wrapper classes CComVariant (for olevariant) and some helper com classes to safely access them from languages which don't support olevariant and olearrays. Also they mention the cases where default transparent marshaling from olevariant to System.Object wouldn't work.

http://www.roblocher.com/whitepapers/oletypes.aspx
http://www.thescripts.com/forum/thread263058.html
http://discuss.microsoft.com/SCRIPTS/WA-MSD.EXE?A2=ind9903c&L=atl&P=26588

Loren Soth

Hi, I just converted a code of VB.Net in C#.
and m getting error while passing variable names or value

Error :Argument '1': cannot convert from 'ref double' to 'ref object'

kindly help me to solve this error..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.