C++ Builder ActiveX get property method overflow

-1

In C++ Builder XE7, I created a simple COM automation object:

  1. File > New > ActiveX > ActiveX Library
  2. File > New > ActiveX > Automation Object
  3. Add new property MyProperty
  4. Push 'Refresh implementation'

So, the wizard created declarations/implementations for get/set methods:

interface IMyObject: IDispatch
{
  [propget, id(0x000000CA)]
  HRESULT _stdcall MyProperty([out, retval] long* Value);
  [propput, id(0x000000CA)]
  HRESULT _stdcall MyProperty([in] long Value);
  };

STDMETHODIMP TMyObjectImpl::get_MyProperty(long* Value)
{
  try
  {

  }
  catch(Exception &e)
  {
    return Error(e.Message.c_str(), IID_IMyObject);
  }
  return S_OK;
}
// ---------------------------------------------------------------------------
...

In order to get the property value for automation clients, I inserted the code to assign the Property value to the Value referenced by the parameter:

STDMETHODIMP TMyObjectImpl::get_MyProperty(long* Value)
{
  try
  {
    *Value = MyProperty;
  }
  catch(Exception &e)
  {
    return Error(e.Message.c_str(), IID_IMyObject);
  }
  return S_OK;
}

Getting the property value in the client application, the server function TMyObjectImpl::get_MyProperty(long* Value) seems to run recursively until gets stack overflow.

Here is a simple client code:

Variant Object;
double N;
V = Variant::CreateObject("MyProject.MyObject");
N = V.OlePropertyGet("MyProperty");

What I am doing wrong in this assignment?

*Value = MyProperty;
delphi
c++builder
ole
activexobject
asked on Stack Overflow Nov 2, 2015 by Giovanni

1 Answer

1
STDMETHODIMP TMyObjectImpl::get_MyProperty(long* Value)
{
  try
  {
    *Value = MyProperty;
  }
  catch(Exception &e)
  {
    return Error(e.Message.c_str(), IID_IMyObject);
  }
  return S_OK;
}

Reading MyProperty is implemented by a call to get_MyProperty. Hence the unterminated recursion.

You need to implement get_MyProperty by returning a value obtained by some other means. For instance, you might return a constant value:

*Value = 42;

Or you might return a value stored in a member field:

*Value = myPropertyValue;

where myPropertyValue is a member field of your class.

answered on Stack Overflow Nov 2, 2015 by David Heffernan

User contributions licensed under CC BY-SA 3.0