更改 CComVariant 变量的类型在 ATL 方法中不起作用
Changing type of CComVariant variable doesn't work inside ATL method
我正在尝试构建一个 ATL COM class,它具有以下方法:
STDMETHODIMP CTestClass::TestMethod(VARIANT VarIn, VARIANT_BOOL* bRes)
{
//VarIn is of type VT_INT
CComVariant var(VarIn);
var.ChangeType(VT_UI8); //doesn't work, HRESULT return value reports type mismatch
ULONGLONG ullVal = var.ullVal; //wrong value is assigned
*bRes = VARIANT_TRUE;
return S_OK;
}
问题是 CComVariant
实例的类型没有在 方法中更改。例如,如果我将基于 int 的值 123 传递给上述方法,则不会执行到 VT_UI8
的类型转换。
然而,如果 CComVariant
class 用于独立函数,则通过 ChangeType()
方法更改类型可以完美地工作:
void Function()
{
CComVariant var(123);
var.ChangeType(VT_UI8); //ok
ULONGLONG ullVal = var.ullVal; //correct value is assigned
}
我是否忽略了什么?谢谢。
你的测试代码应该是这样的:
CComVariant v;
v.vt = VT_INT;
v.intVal = 123;
const HRESULT n = v.ChangeType(VT_UI8); // 0x80020005 DISP_E_TYPEMISMATCH Type mismatch.
ChangeType
得到 VariantChangeType
API 的支持,其中...
...handles coercions between the fundamental types (including numeric-to-string and string-to-numeric coercions).
然而,问题是定义的 "fundamental" 类型太多,有些 "more fundamental" 比其他的要多。例如,VT_I4
优于 VT_INT
:
CComVariant v;
v.vt = VT_INT;
v.intVal = 123;
const HRESULT n1 = v.ChangeType(VT_UI8); // 0x80020005 DISP_E_TYPEMISMATCH Type mismatch.
v.vt = VT_I4;
const HRESULT n2 = v.ChangeType(VT_UI8); // S_OK
ULONGLONG n = v.ullVal; // 123
也就是说,API 转换似乎不接受 VT_INT
。您会更安全地选择此列表顶部的类型:
enum VARENUM
{
VT_EMPTY = 0,
VT_NULL = 1,
VT_I2 = 2,
VT_I4 = 3,
VT_R4 = 4,
VT_R8 = 5,
VT_CY = 6,
VT_DATE = 7,
VT_BSTR = 8,
VT_DISPATCH = 9,
VT_ERROR = 10,
VT_BOOL = 11,
VT_VARIANT = 12,
VT_UNKNOWN = 13,
VT_DECIMAL = 14,
VT_I1 = 16,
VT_UI1 = 17,
VT_UI2 = 18,
VT_UI4 = 19,
VT_I8 = 20,
VT_UI8 = 21,
// *** Cut off line ***
VT_INT = 22,
VT_UINT = 23,
延伸阅读:
- Question regarding VT_INT in oleaut32
- Allowed "out" parameter types in a COM automation interface
我正在尝试构建一个 ATL COM class,它具有以下方法:
STDMETHODIMP CTestClass::TestMethod(VARIANT VarIn, VARIANT_BOOL* bRes)
{
//VarIn is of type VT_INT
CComVariant var(VarIn);
var.ChangeType(VT_UI8); //doesn't work, HRESULT return value reports type mismatch
ULONGLONG ullVal = var.ullVal; //wrong value is assigned
*bRes = VARIANT_TRUE;
return S_OK;
}
问题是 CComVariant
实例的类型没有在 方法中更改。例如,如果我将基于 int 的值 123 传递给上述方法,则不会执行到 VT_UI8
的类型转换。
然而,如果 CComVariant
class 用于独立函数,则通过 ChangeType()
方法更改类型可以完美地工作:
void Function()
{
CComVariant var(123);
var.ChangeType(VT_UI8); //ok
ULONGLONG ullVal = var.ullVal; //correct value is assigned
}
我是否忽略了什么?谢谢。
你的测试代码应该是这样的:
CComVariant v;
v.vt = VT_INT;
v.intVal = 123;
const HRESULT n = v.ChangeType(VT_UI8); // 0x80020005 DISP_E_TYPEMISMATCH Type mismatch.
ChangeType
得到 VariantChangeType
API 的支持,其中...
...handles coercions between the fundamental types (including numeric-to-string and string-to-numeric coercions).
然而,问题是定义的 "fundamental" 类型太多,有些 "more fundamental" 比其他的要多。例如,VT_I4
优于 VT_INT
:
CComVariant v;
v.vt = VT_INT;
v.intVal = 123;
const HRESULT n1 = v.ChangeType(VT_UI8); // 0x80020005 DISP_E_TYPEMISMATCH Type mismatch.
v.vt = VT_I4;
const HRESULT n2 = v.ChangeType(VT_UI8); // S_OK
ULONGLONG n = v.ullVal; // 123
也就是说,API 转换似乎不接受 VT_INT
。您会更安全地选择此列表顶部的类型:
enum VARENUM
{
VT_EMPTY = 0,
VT_NULL = 1,
VT_I2 = 2,
VT_I4 = 3,
VT_R4 = 4,
VT_R8 = 5,
VT_CY = 6,
VT_DATE = 7,
VT_BSTR = 8,
VT_DISPATCH = 9,
VT_ERROR = 10,
VT_BOOL = 11,
VT_VARIANT = 12,
VT_UNKNOWN = 13,
VT_DECIMAL = 14,
VT_I1 = 16,
VT_UI1 = 17,
VT_UI2 = 18,
VT_UI4 = 19,
VT_I8 = 20,
VT_UI8 = 21,
// *** Cut off line ***
VT_INT = 22,
VT_UINT = 23,
延伸阅读:
- Question regarding VT_INT in oleaut32
- Allowed "out" parameter types in a COM automation interface