941,506 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 15074
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 7th, 2006
0

convert from 'ref double[]' to 'ref object'

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wakeup is offline Offline
10 posts
since Nov 2005
Apr 8th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

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?
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Apr 10th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

Quote originally posted by alc6379 ...
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.

Quote originally posted by alc6379 ...
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#?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wakeup is offline Offline
10 posts
since Nov 2005
Apr 10th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

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

C# Syntax (Toggle Plain Text)
  1. object x =null;
  2. 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.
Team Colleague
Reputation Points: 186
Solved Threads: 147
Cookie... That's it
alc6379 is offline Offline
2,519 posts
since Dec 2003
Apr 11th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

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
Reputation Points: 28
Solved Threads: 4
Posting Whiz in Training
Lord Soth is offline Offline
233 posts
since Mar 2006
Apr 11th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

Quote originally posted by alc6379 ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wakeup is offline Offline
10 posts
since Nov 2005
Apr 11th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

Quote originally posted by Lord Soth ...
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 *.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wakeup is offline Offline
10 posts
since Nov 2005
Apr 12th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

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
Reputation Points: 28
Solved Threads: 4
Posting Whiz in Training
Lord Soth is offline Offline
233 posts
since Mar 2006
Apr 19th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wakeup is offline Offline
10 posts
since Nov 2005
Apr 19th, 2006
0

Re: convert from 'ref double[]' to 'ref object'

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...&L=atl&P=26588

Loren Soth
Reputation Points: 28
Solved Threads: 4
Posting Whiz in Training
Lord Soth is offline Offline
233 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: create pragramaticaly access database table of type boolean
Next Thread in C# Forum Timeline: Data grids for smart devices





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC