简体   繁体   中英

Convert enum to _variant_t C++

I'm writing a COM client. One of the functions in my COM server takes enum as an argument and returns a number (long) Looks like this:

Function GetFlowsheetStatus(iQueryFlags As FlowSheetObjStatusFlag_enum) As Long I'm using both early and late binding (IDispatch and direct interfaces) So I wrote something like that:FlowSheetObjStatusFlag_enum flag;_variant_t p = _variant_t(flag);

So I need to convert the enum "FlowSheetObjStatusFlag_enum" to _variant_t in order to be able to pass it. How can I do that?

I'm not familiar with COM myself but C++ does allow casting from 1 type to another.

  • static_cast to cast 1 type to another compatible type
    (ie int to double )
  • dynamic_cast for down casting a base type to a derived type
  • reinterpret_cast which should ONLY be used if you absolutely need to use it. It takes a type and says "Its not this type, instead treat it as this."
    (ie int x = reinterpret_cast<int>(std::string{"my string hello"}); )
  • const_cast is used mostly to cast away const-ness but can be used to give const-ness too. I thought I would list it here anyway for depth.

For more details you should read this: http://en.cppreference.com/w/cpp/language/explicit_cast

My mistake was that I created my own enum type and wanted to pass it through Automation. I solved the problem by finding the enum exposed by the Server. My mistake was writing an identical enum for that. I guess the enum exposed was already compatible since it was originated via the server.

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