tags:

views:

400

answers:

1

I know I'm missing something simple, I have next to no experience with these com things.

I would like to do this within an interface in an idl

[id(5), helpstring("Returns true if the object is in a valid state.")]
HRESULT IsValid([out, retval] boolean bValid);

However this gives : [out] paramter is not a pointer.

Ok, I understand that.

However, in the C# code implementing this, I can't return a bool* from the method IsValid() because it is unsafe.

What is the correct way for me to return the boolean value?

+5  A: 

Try:

HRESULT IsValid([out, retval] VARIANT_BOOL *bValid);

In order to work as an output, it has to be a pointer to the value; this is how it will be written to on the C++ side:

*bValue = VARIANT_TRUE;

I don't know if you can write the type as boolean - I've only ever seen VARIANT_BOOL being used.

On the C# side, it will become equivalent to:

public bool IsValid()

In other words, the runtime callable wrapper (RCW) will implement a "nicer" version of the method signature and take care of the unsafe translation for you. If the C++ implementation returns E_FAIL (or E_WHATEVER), then the RCW's method will throw a ComException.

You might also consider adding the [propget] attribute, which will make IsValid available as a property instead of a method. As with any property, only do this if it is cheap to evaluate and has no side effects (the debugger will evaluate it as you step through C# code).

Daniel Earwicker
this worked. also, I was wondering how to use a property for this so you solved two of my problems.thanks!
SP