Crash when not using raw COM interface

By Adam Nagy

Let's say we have the following code:

// First select a RectangularPatternFeature then run the code

CComQIPtr<RectangularPatternFeature> pPattern =

  pInvApp->ActiveDocument->SelectSet->Item[1];

for (int i = 1; i <= pPattern->PatternElements->Count; i++)

{

  CComPtr<FeaturePatternElement> pElem =

    pPattern->PatternElements->Item[i];

 

  // In this case there is no crash

  pElem->put_Suppressed(false); // or true

 

  // No matter if I use false or true

  // both cases will cause an error

  pElem->Suppressed = false; // or true

}

The difference between put_Suppressed(false) and Suppressed = false is that the former is the raw interface which simply provides an HRESULT value to let you know if there was an error, while the latter is a wrapper around it that is throwing a _com_error exception in case of an error.

You can also check the rxinventor.tli file of your project to see the exact implementation of the raw and wrapper functions:

#pragma implementation_key(29648)

inline void Inventor::RectangularPatternFeature::PutSuppressed ( VARIANT_BOOL _arg1 ) {

    _com_dispatch_method(this, 0x500030a, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL,

        L"\x000b", _arg1);

}

 

#pragma implementation_key(29649)

inline HRESULT Inventor::RectangularPatternFeature::put_Suppressed ( VARIANT_BOOL _arg1 ) {

    return _com_dispatch_raw_method(this, 0x500030a, DISPATCH_PROPERTYPUT, VT_EMPTY, NULL,

        L"\x000b", _arg1);

}

What is the error in our code? The problem is that the first element of a rectangular pattern cannot be suppressed – you can easily verify this in the user interface:

PatternElementSuppress

While put_Suppressed(false) simply returns an error code, Suppressed = false will throw an error that you would need to catch. You can solve it like this:

// First select a RectangularPatternFeature then run the code

CComQIPtr<RectangularPatternFeature> pPattern =

  pInvApp->ActiveDocument->SelectSet->Item[1];

for (int i = 1; i <= pPattern->PatternElements->Count; i++)

{

  CComPtr<FeaturePatternElement> pElem =

    pPattern->PatternElements->Item[i];

 

  // In this case no crash

  pElem->put_Suppressed(false); // or true

 

  // Now we catch the error so nothing will crash

  // Note: the real solution would be to start the iteration

  // from 2 instead of 1 if we just want to change the

  // Suppressed state of the items. I left it on 1 just to

  // show that the error handling works 

  try

  {

    pElem->Suppressed = false; // or true

  }

  catch (_com_error e)

  {

    // do something if you want

  }

}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading