开发者

COM - How to get the properties of an interface within the type library the interface is defined

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 attemp开发者_如何学运维t a case statement the only value in hr is 0. I need to check the value being set on the clientside.


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...
}


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:
    // ...
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜