HandyOrg/HandyControl · error
Wrote an incorrect number of bytes
Error message
Wrote an incorrect number of bytes
What it means
Identical EFail throw inside HandyControl's InteropValues (the same IStream CopyTo logic embedded in the interop helper): when pstm.Write returns a count different from the bytes read, the stream wrapper declares the write failed and throws "Wrote an incorrect number of bytes". It guards against native IStream implementations that do not honor the COM contract of writing all requested bytes and reporting the count.
Solutions
- Verify the native side correctly implements IStream.Write and returns the actual bytes written.
- Ensure the COM stream's lifetime is managed correctly (release it only after copy completes; check Marshal.ReleaseComObject usage).
- Retry the operation with a freshly obtained stream (re-copy clipboard data or re-open the storage).
- Catch the failure in calling code and fall back to a managed stream path (e.g. copy via MemoryStream first).
Defensive patterns
Strategy: try-catch
Validate before calling
// Validate the interop stream before use:
if (pstm == null) throw new InvalidOperationException("IStream is null");
// If obtainable, confirm STATSTG cbSize > 0 via IStream.Stat before copying. Type guard
static bool HasComStream(object comObj) =>
comObj != null && Marshal.IsComObject(comObj); Try / catch
try
{
// interop copy path using InteropValues stream wrapper
}
catch (Exception ex) when (ex.Message == "Wrote an incorrect number of bytes")
{
// re-fetch the stream source (e.g. re-read clipboard data) and retry once
} Prevention
- Release COM streams only after the copy operation finishes.
- Re-acquire clipboard/OLE stream data instead of caching the IStream across calls.
- Log and surface HRESULT details when short writes occur to identify the faulty native implementation.
- Prefer copying through a managed MemoryStream when the native stream is of unknown quality.
When it happens
Trigger: Same as GPStream: CopyTo loop calls Read then pstm.Write; any Write return value != read (short write, 0, or an error HRESULT already converted) triggers the throw at the call site in InteropValues.cs:882.
Common situations: Using HandyControl interop helpers with clipboard/OLE streams (CF_HDROP / IStream from native code) where the native Write fails partway — often due to the source stream being released early, insufficient memory, or a custom IStream stub that returns the wrong count.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- Wrote an incorrect number of bytes
- ArgumentNullException
- NotImplemented
- IConnectionPoint::Advise returned an invalid cookie.
AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14).
Data as JSON: /api/errors/17c77115c9784448.
Report an issue: GitHub.
Appendix: source
Thrown at src/Shared/HandyControl_Shared/Tools/Interop/InteropValues.cs:882
public virtual long CopyTo(IStream pstm, long cb, long[] pcbRead)
{
const int bufsize = 4096; // one page
var buffer = Marshal.AllocHGlobal(bufsize);
if (buffer == IntPtr.Zero) throw new OutOfMemoryException();
long written = 0;
try
{
while (written < cb)
{
var toRead = bufsize;
if (written + toRead > cb) toRead = (int) (cb - written);
var read = Read(buffer, toRead);
if (read == 0) break;
if (pstm.Write(buffer, read) != read)
{
throw EFail("Wrote an incorrect number of bytes");
}
written += read;
}
}
finally
{
Marshal.FreeHGlobal(buffer);
}
if (pcbRead != null && pcbRead.Length > 0)
{
pcbRead[0] = written;
}
return written;
}
public virtual Stream GetDataStream() => DataStream;
View on GitHub (pinned to 2c0875ebd6)