| | |
convert from 'ref double[]' to 'ref object'
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2005
Posts: 10
Reputation:
Solved Threads: 0
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
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?
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?
Alex Cavnar, aka alc6379
•
•
Join Date: Nov 2005
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by alc6379
Maybe it's because I'm just learning this stuff, but what exactly are you trying to accomplish?
•
•
•
•
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?
When and how can I do cast in c#?
A cast is what you're doing when you do this code:
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.
C# Syntax (Toggle Plain Text)
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.
Alex Cavnar, aka alc6379
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
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
Best regards,
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
•
•
Join Date: Nov 2005
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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
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
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
Best regards,
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
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
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
Best regards,
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
![]() |
Other Threads in the C# Forum
- Previous Thread: getting details from the loop
- Next Thread: Using Microsoft Excel 10.0 Libary Object -Dang .dlls
| Thread Tools | Search this Thread |
.net access algorithm alignment app array barchart bitmap box broadcast c# c#gridviewcolumn cast check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing elevated encryption enum excel file focus form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml






