简体   繁体   中英

How to pass bool from c# through c++ com interface in idl

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM