Please support our C# advertiser: Programming Forums
Views: 7929 | Replies: 9
![]() |
•
•
Join Date: Nov 2005
Posts: 10
Reputation:
Rep Power: 4
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
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation:
Rep Power: 11
Solved Threads: 102
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:
Rep Power: 4
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?
I want pass a double array from c# to a c++ dll.
•
•
•
•
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#?
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,336
Reputation:
Rep Power: 11
Solved Threads: 102
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.
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
Crimson K. Software _________________________________________________________________ Crimson K. Blog
•
•
Join Date: Nov 2005
Posts: 10
Reputation:
Rep Power: 4
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
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
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
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
Crimson K. Software _________________________________________________________________ Crimson K. Blog
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode