views:

27

answers:

1

I need to set the color of an HTML element, I've managed to get a reference to the style but I now need to pass a VARIANT to the put_color method and I can't find information regarding how you construct a variant.

How would I go about specifying the color #ffaaaa for the put_color call?

CComPtr<IHTMLStyle> spStyle = htmlElement->get_style;
spStyle->put_color(what_goes_here?);
+1  A: 

You need to wrap the colour string in a BSTR value, which is one of the types accepted by a VARIANT:

VARIANT color;
color.vt = VT_BSTR;
color.bstrVal = SysAllocString(TEXT("#ffaaaa"));
spStyle->put_color(color);
VariantClear(&color);
casablanca
Thanks for your quick reply, you're an absolute life saver. It's a long time since I've coded in c++, this does seem an incredibly complicated way to specify a color. Think it would have taken quite a while to get there.
opsb
@opsb: You're welcome. You'll come across the `VARIANT` type in a lot of COM interfaces, because it is easier for the system to pass around this single wrapper type rather than have different argument types for each function.
casablanca
Don't forget to free the memory you allocate with `SysAllocString`. `VariantClear` will take care of this for you.
jeffamaphone
@jeffamaphone: Thanks for adding it in.
casablanca