{"record":{"id":"6dc8d05fb674aec3","repo":"dotnet/wpf","slug":"formatexception","errorCode":null,"errorMessage":"FormatException","messagePattern":"FormatException","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/NativeMethodsCLR.cs","lineNumber":2112,"sourceCode":"\n                    if (vt == (int)tagVT.VT_I8) {\n                        return (long)longVal;\n                    }\n                    else {\n                        return (UInt64)longVal;\n                    }\n                }\n\n                if (Byref) {\n                    val = GetRefInt(val);\n                }\n\n                switch (vtType) {\n                case (int)tagVT.VT_R4:\n                case (int)tagVT.VT_R8:\n\n                    // can I use unsafe here?\n                    throw new FormatException(/*SR.GetString(SR.CannotConvertIntToFloat)*/);\n\n                case (int)tagVT.VT_CY:\n                    // internally currency is 8-byte int scaled by 10,000\n                    longVal = ((uint)data1 & 0xffffffff) | ((uint)data2 << 32);\n                    return new Decimal(longVal);\n                case (int)tagVT.VT_DATE:\n                    throw new FormatException(/*SR.GetString(SR.CannotConvertDoubleToDate)*/);\n\n                case (int)tagVT.VT_BSTR:\n                case (int)tagVT.VT_LPWSTR:\n                    return Marshal.PtrToStringUni(val);\n\n                case (int)tagVT.VT_LPSTR:\n                    return Marshal.PtrToStringAnsi(val);\n\n                case (int)tagVT.VT_DISPATCH:\n                case (int)tagVT.VT_UNKNOWN:\n                    {","sourceCodeStart":2094,"sourceCodeEnd":2130,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/NativeMethodsCLR.cs#L2094-L2130","documentation":"This VARIANT-to-managed conversion helper in NativeMethodsCLR explicitly rejects floating-point VARIANT types (VT_R4/VT_R8) when converting to a numeric (integral) destination, throwing an uninformative FormatException (the SR message is commented out). The conversion from float/double to the requested type is simply not implemented.","triggerScenarios":"Calling the NativeMethods VARIANT conversion API on a VARIANT whose vt is VT_R4 or VT_R8 — e.g. marshaling COM/automation property data that holds a float or double into an integral/string expected type.","commonSituations":"Reading COM/ActiveX/automation properties that are floats or doubles through this legacy conversion path; interop code written for integer VARIANTs suddenly receiving floating-point data after a COM component or data source change.","solutions":["Convert the VARIANT to a type the helper supports, or perform the float/double conversion in your own code before/after marshaling","Coerce the source data to an integer type (e.g. via VariantChangeType or explicit casting) before calling the converter","Bypass this helper and read the VARIANT with Marshal/Convert.ChangeType directly for R4/R8 types","Check the source COM property's declared type and adjust the consuming code's expected type"],"exampleFix":"// before\nobject value = NativeMethods.ConvertVariantToManaged(variant); // throws for VT_R8\n// after\nobject value = variant.vt == (short)tagVT.VT_R8\n    ? Convert.ChangeType(variant.dblVal, typeof(decimal))\n    : NativeMethods.ConvertVariantToManaged(variant);","handlingStrategy":"type-guard","validationCode":"if (variant.vt == (short)tagVT.VT_R4 || variant.vt == (short)tagVT.VT_R8) { /* convert manually */ }","typeGuard":"bool IsFloatingVariant(VARIANT v) => v.vt == (short)tagVT.VT_R4 || v.vt == (short)tagVT.VT_R8;","tryCatchPattern":"try { result = NativeMethods.ConvertVariant(variant); } catch (FormatException) { result = Convert.ChangeType(variant.fltVal != 0 ? variant.dblVal : variant.fltVal, typeof(decimal)); }","preventionTips":["Check the VARIANT's vt field before invoking the legacy converter","Coerce floating-point VARIANTs to integral types via VariantChangeType first","Prefer System.Convert/Marshal over this commented-out-message legacy path for R4/R8"],"tags":["wpf","interop","variant","format"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}