tags:

views:

29

answers:

2

How to get the properties of an interface within the type library the interface is defined, Keeps returning HRESULT but would like it to return the actual value of the property.

EDIT:

IDL:

interface IMyClassInterface : IDispatch

{

[propget, id(1), helpstring("Gets user Type")] HRESULT getUserDefineTypeVal([out,retval] UserDefineEnum *ptrVal);

[propput, id(1), helpstring("Sets user Type ")]HRESULT setUserDefineTypeVal([in] UserDefineEnum newVal);

}

Property in Header File:

STDMETHOD(getUserDefineTypeVal)(UserDefineEnum *ptrVal);

STDMETHOD(setUserDefineTypeVal)(UserDefineEnum newVal);

Property in MYClass.cpp:

STDMETHODIMP CMYClass::getUserDefineTypeVal(UserDefineEnum *ptrVal) {

         *ptrVal = UserDefineEnum(private_var_UserDefineTypeVal);

            return S_OK;

    }



AnotherClass within the Type Library:

IMyClassInterface* private_var_MyClass



STDMETHODIMP CAnotherClass::someMethod(){

UserDefineEnum* p;

 if(private_var_MyClass->getUserDefineTypeVal(p)){

             //do somestuff



            }

}

The problem is the if condition doesn’t return true. However the below partially works.

HRESULT hr = private_var_MyClass->getUserDefineTypeVal(p);

                        if(hr == S_OK){ do somestuff }

The problem with this is if I attempt a case statement the only value in hr is 0. I need to check the value being set on the clientside.

A: 

COM usually uses out parameters to return values. In C/C++ you have to pass a pointer to a variable which will contain the result upon return.

The HRESULT return parameter is only used to report the success (or failure) of the method call.

EDIT For your code, you need to reserve the memory for the result by the caller:

UserDefineEnum p; // No * here ...
if (private_var_MyClass->getUserDefineTypeValue(&p) == S_OK) { // note '&' operator!
  switch (p) {
    case ENUM_1: // ... 
    case ENUM_2:
    // ...
  }
}
MartinStettner
Thanks I get what you're saying but I have added some code to go into more detail.
Phill Pafford
+2  A: 

The value of S_OK is 0, that's why your if() statement doesn't execute. You should use the SUCCEEDED macro:

UserDefinedEnum value;
HRESULT hr = private_var_MyClass->getUserDefineTypeVal(&value);
if (SUCCEEDED(hr)) {
  switch (value) {
    // etc...
  }
}
else {
  // do something with the error...
}
Hans Passant
I think you should put a `}` *before* the `else` and remove the brace after it ...
MartinStettner
Fixed, thanks for the hint.
Hans Passant