Solved

How to extract data in CLR C++ .NET console application

Posted on 2011-09-23
4
413 Views
Last Modified: 2012-05-12

So I'm accessing a COM vendor DLL from a VC++ .NET CLR console application and calling a "get" on one of their class properties as follows.

IDACSpectrumPtr pSpectrum;
VARIANT pfIntensities;
pSpectrum->get_Intensities(&pfIntensities); VARIANT debug data
From other languages I know this returns an array of floats.  I also know there are 299 values.  I can see in the debugging information (attached) that the parray element of the returned variant has 299 elements in it -- so I KNOW its there.  The question is, how do I get the data out of it?  So far can't get anything to work.  I saw an example in MFC C++ that uses SafeArrayAccessData to convert a VARIANT, but I do not think that is available to me in a CLR C++ .NET console application.  What is the .NET way to get my data out of a variant?

Thanks very much for any help.
0
Comment
Question by:riceman0
  • 3
4 Comments
 
LVL 32

Expert Comment

by:sarabande
ID: 36598050
i am not expert in clr c++ but i know that you can call GetObjectForNativeVariant passing the VARIANT as IntPtr. the call would return Object^.

i have seen c# code which uses the object returned "as array" and i think there are similar ways in managed c++.

i will look further and return if i found some appropriate code.

Sara
0
 
LVL 32

Expert Comment

by:sarabande
ID: 36598108
it seems you simply can make dynamic_cast on int array cause the Object returned already has right array type.

Sara
0
 

Author Comment

by:riceman0
ID: 36599229
Sounds promising, but in your travels did you see some syntax for that?  

For example,

 float * pc = dynamic_cast<float*>(pfIntensities.parray);

gives me the error

"float *" is an invalid target type for dynamic cast.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 500 total points
ID: 36599524
as told i am not a clr programmer myself.
but i would try like

 int[] arr = dynamic_cast<int[]> (GetObjectForNativeVariant(pfIntensities));

you will find some code when looking for GetObjectForNativeVariant and c++ keywords.

the float probably is not a suitable Array type in managed c++ while 32-bit integer is. the variant is realized as an union and type, rank and element count information is available from variant. so the clr pretty well can create an array object out of that.

Sara
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now