dotnet/wpf · error · InvalidOperationException

SR.PenService_InvalidPacketData

Error message

SR.PenService_InvalidPacketData

What it means

PenThreadWorker throws InvalidOperationException (SR.PenService_InvalidPacketData) when a tablet packet stream has a per-packet byte size (cbPacket) that is not a multiple of 4. Packet data is marshaled into an int[], so non-word-aligned packet sizes cannot be processed.

Solutions

  1. Update or reinstall the digitizer/tablet driver so it reports a valid packet size
  2. Test with a standard compliant digitizer to confirm the device is the problem
  3. Capture with WISP vs Pointer stack differences (app.config Switch.System.Windows.Input.Stylus) to route around the faulty device path
  4. Report the device to its vendor; packet size must be a multiple of 4 bytes per packet

Example fix

// app.config — switch stylus to legacy WISP stack if Pointer stack chokes on device data
<configuration>
  <appSettings>
    <add key="Switch.System.Windows.Input.Stylus" value="Wisp"/>
  </appSettings>
</configuration>
Defensive patterns

Strategy: try-catch

Validate before calling

// Packet size validation happens on device data; guard the worker event
if (cbPacket % 4 != 0) { Log.Error($"Invalid packet size {cbPacket}"); return; }

Try / catch

try { ProcessPenData(cbPacket, cPackets, ...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("packet")) { Log.Error("Bad digitizer packet size", ex); }

Prevention

When it happens

Trigger: A digitizer/tablet device reporting a packet description whose cbPacket is not divisible by 4 — usually from a broken driver, non-standard HID digitizer, or corrupted pen data event arriving on the worker thread.

Common situations: Using obscure/cheap tablet hardware or virtualized HID devices with malformed packet descriptions; driver updates changing packet layout; remote-desktop or VM pen input bridging producing bad packet sizes.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/af946edbdd54d82c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Stylus/Wisp/PenThreadWorker.cs:783

            }

            return false;
        }

        /////////////////////////////////////////////////////////////////////

        internal void FireEvent(PenContext penContext, int evt, int stylusPointerId, int cPackets, int cbPacket, IntPtr pPackets)
        {
            // disposed?
            if (__disposed)
            {
                return;  // Don't process this event if we're in the process of shutting down.
            }

            // marshal the data to our cache
            if (cbPacket % 4 != 0)
            {
                throw new InvalidOperationException(SR.PenService_InvalidPacketData);
            }

            int cItems = cPackets * (cbPacket / 4);
            int[] data = null;
            if (0 < cItems)
            {
                data = new int [cItems]; // GetDataArray(cItems); // see comment on GetDataArray
                Marshal.Copy(pPackets, data, 0, cItems);
                penContext.CheckForRectMappingChanged(data, cPackets);
            }
            else
            {
                data = null;
            }

            int timestamp = Environment.TickCount;
            
            // Deal with caching packet data.

View on GitHub (pinned to 81131a70a4)