我正在使用boost :: lexical_cast将VARIANT转换为int,如下所示:
component.m_id= boost::lexical_cast<int>(id.intVal);
但看起来我在这里获得垃圾值:id.intVal。我在这里做错了什么?
最佳答案
如果您真的不知道变体的类型(在您的示例中,它似乎是表示为VT_BSTR的字符串),则最佳和最安全的方法是调用Windows API VariantChangeType(或VariantChangeTypeEx是本地化)是一个问题);这是一个示例(不是特定于Boost的):
VARIANT vIn;
VariantInit(&vIn);
vIn.vt = VT_BSTR;
vIn.bstrVal = ::SysAllocString(L"12345678");
VARIANT vOut;
VariantInit(&vOut);
// convert the input variant into a 32-bit integer
// this works also for other compatible types, not only BSTR
if (S_OK == VariantChangeType(&vOut, &vIn, 0, VT_I4))
{
// now, you can safely use the intVal member
printf("out int: %i\n", vOut.intVal);
}
VariantClear(&vOut);
VariantClear(&vIn);
关于c++ - 如何将VARIANT转换为整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39555784/