Passing System::Object to a function - how to

Hi,

I am stuck on a problem with pasing an argument to a function. I am using Visual C++ Express Edition and I imported an Active X component. It has a function like this:

Virtual Bool AxTRIOPCLib::AxTrioPC::GetTable( int Val1, int Val2 , System Object ^% Values)

Can sombody tell me how I can declare and pass an object to it and then later convert returned values into Double I cannot find any examples and I am not so experienced with . NET objects.

Thanks a lot,

Jerry.




Answer this question

Passing System::Object to a function - how to

  • Snouto Override

    Thanks again,

    That part I understand, I am just lost when it comes to passing a reference to an object and then casting it to an array of 200 doubles. How would that be coded

    You're a big help,

    Jerry



  • eyelkin

    I am not sure where to look. I am relying on a pop up that shows me the sytax and it looks like this:

    virtual bool AxTRIOPCLib::AxTrioPC::GetTable( int startPosition, int numberOfValues, System::Object ^% values )

    I don't understand what "^%" is.

    Here's an example of Visual C++ 2003 or C++ Builder accessing this function:

    int numvals = 0;

    int runningcount = 0;

    Variant d_array = VarArrayCreate( OPENARRAY(int,(0,count)), varDouble );

    if( TrioPC_GetVr(VR_TrioAXIS1_DataReady) ) // verify there is data waiting...

    {

    numvals = TrioPC_GetVr( VR_TrioAXIS1_NumVals ); // starting point.

    while( runningcount < numvals ){

    Debug->TrioPC->GetTable( runningcount,numvals - runningcount,d_array );

    for( int i=runningcount, j=0; i<numvals; i++, j++ )

    datavalsIdea = d_array.GetElement(j);

    runningcount += (numvals-runningcount);

    numvals = TrioPC_GetVr( VR_TrioAXIS1_NumVals );

    }



  • Vaclav Macak

    Thanks a lot,

    This is the error message I am geting now:

    error C2664: 'AxTRIOPCLib::AxTrioPC::GetTable' : cannot convert parameter 3 from 'cli::array<Type> ^' to 'System::Object ^%'

    I tried casting Values to System::Object^, it does not complain but I am getting all 0's.



  • Huy V Nguyen

    Hi,

    This:

    System::Object^ Values = gcnew System::Object();
    GetTable(0,1,Values);
    double d = static_cast<double>(Values);

    causes System.InvalidCastException.

    Any other ideas

    Thanks a lot.



  • Daniel UJ

    Hi,

    Thank you for your patience.

    I think it needs to be allocated first. If I don't, I get back unassigned object.

    I do not have any documentation, it is a third part activeX that I need to use and I only have some examples of how it was used in LabWindows/CVI environment, which is hard to translate to Objects.

    How can I look up what it does, there's no header file or anything else.

    Can you also tell me what "Object ^%" means

    This runs:

    array<double>^ Values = gcnew array<double>(iNumNumbers);

    this->axTrioPC1->GetTable( 0, iNumNumbers,static_cast<System::Object ^>(Values));

    but all I get back is 0's.

    if I code like this:

    array<double>^ Values = gcnew array<double>(iNumNumbers);

    this->axTrioPC1->GetTable( 0, iNumNumbers,static_cast<System::Object ^%>(Values));

    I get and error: error C2440: 'static_cast' : cannot convert from 'cli::array<Type> ^' to 'System::Object ^%'

    I won't compile.

    Thanks again.



  • sunilkumar

    Dustmaker wrote:

    Hi,

    I am stuck on a problem with pasing an argument to a function. I am using Visual C++ Express Edition and I imported an Active X component. It has a function like this:

    Virtual Bool AxTRIOPCLib::AxTrioPC::GetTable( int Val1, int Val2 , System Object ^% Values)

    Can sombody tell me how I can declare and pass an object to it and then later convert returned values into Double I cannot find any examples and I am not so experienced with . NET objects.

    Thanks a lot,

    Jerry.

    System::Object ^% Values

    Values is a tracking reference to an Object^. Looks like it's an out-argument in this specific case. Once the funciton returns you can unbox it to a double. A simple cast would suffice.



  • James Boman

    Do you know what GetTable expects as the third parameter Does it expect an allocated array Or will it allocate the array for you

    If the former, you'd need to gcnew an array, cast that to Object^% and use that.

    If the latter, you'd need to send an Object^% (needn't even gcnew an object), and then after the call succeeds, cast the Object^ to an array<double>^ and extract the values out of it. If that fails, try casting it to an array<Object^>^ and check what the type of the Object^ is that's returned. It may not be a double for all you know.

    The core issue here is that you need to understand what the COM method does and how to call it before using it from .NET. Just because RCW allows you to easily call into COM doesn't mean you can avoid understanding what the COM library is doing.



  • zoltix

     Dustmaker wrote:

    Thanks again,

    That part I understand, I am just lost when it comes to passing a reference to an object and then casting it to an array of 200 doubles. How would that be coded

    You're a big help,

    Jerry

    I think there's something you did wrong in there. An array of 200 doubles will not be returned as an Object^%, it would be returned as an array<Object^>^. Can you show me what the original COM method looked like



  • mayurkotlikar

    Dustmaker wrote:

    Thanks Nishant.

    Can you provide an example please

    Thanks.

    J.

    I have no idea what your function does, so take this with a grain of salt.

    System::Object^ Values = gcnew System::Object();
    GetTable(100,200,Values);
    double d = static_cast<double>(Values);



  • Ramrajprabu Balasubramanian

    Try this :-

    array<double>^ Values = gcnew array<double>(100);
    GetTable(0,1,Values);
    for each(double d in Values)
    {
    Console::WriteLine(d);
    }

    I've randomly used 100 - but you need to use the exact number you need.



  • Tony1972

    Dustmaker wrote:

    I am not sure where to look. I am relying on a pop up that shows me the sytax and it looks like this:

    virtual bool AxTRIOPCLib::AxTrioPC::GetTable( int startPosition, int numberOfValues, System::Object ^% values )

    I don't understand what "^%" is.

    Here's an example of Visual C++ 2003 or C++ Builder accessing this function:

    int numvals = 0;

    int runningcount = 0;

    Variant d_array = VarArrayCreate( OPENARRAY(int,(0,count)), varDouble );

    if( TrioPC_GetVr(VR_TrioAXIS1_DataReady) ) // verify there is data waiting...

    {

    numvals = TrioPC_GetVr( VR_TrioAXIS1_NumVals ); // starting point.

    while( runningcount < numvals ){

    Debug->TrioPC->GetTable( runningcount,numvals - runningcount,d_array );

    for( int i=runningcount, j=0; i<numvals; i++, j++ )

    datavals = d_array.GetElement(j);

    runningcount += (numvals-runningcount);

    numvals = TrioPC_GetVr( VR_TrioAXIS1_NumVals );

    }

    Ah okay, the variant COM type is marshalled as a System::Object^ in .NET by default. I am assuming that the Variant type is VT_ARRAY in which case it's marshalled as a System::Array.

    Try passing an array<double> to the method. I am not sure it will work, but it's worth a try.



  • Ducbian

    Thanks Nishant.

    Can you provide an example please

    Thanks.

    J.



  • Passing System::Object to a function - how to