Winforms: DataObject.GetData(FileContents) returns null

Created on 1 Apr 2020  路  6Comments  路  Source: dotnet/winforms

  • .NET Core Version:
    .NET Core SDK (reflecting any global.json):
    Version: 3.1.201
    Commit: b1768b4ae7

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:Program Filesdotnetsdk3.1.201

Host (useful for support):
Version: 3.1.3
Commit: 4a9f85e9f8

.NET Core SDKs installed:
3.1.201 [C:Program Filesdotnetsdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.App 3.1.3 [C:Program FilesdotnetsharedMicrosoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.3 [C:Program FilesdotnetsharedMicrosoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.3 [C:Program FilesdotnetsharedMicrosoft.WindowsDesktop.App]

  • Have you experienced this same bug with .NET Framework?:
    No

Problem description:
IDataObject.GetFormats returns FileContents, but IDataObject.GetData("FileContents") returns null

Expected behavior:
Returns the file contents

Minimal repro:

bug

All 6 comments

I never used this particular format myself so I can't guess which application you might have used. You'll probably have to be more specific in how to repro, since DataObject content is highly dependent on how the source application presents it data.

I never used this particular format myself so I can't guess which application you might have used. You'll probably have to be more specific in how to repro, since DataObject content is highly dependent on how the source application presents it data.

Select any file in File Explorer, ctrl+C
The available formats are:
Shell IDList Array, DataObjectAttributes, DataObjectAttributesRequiringElevation, Shell Object Offsets, Preferred DropEffect, AsyncFlag, FileDrop, FileNameW, FileName, FileContents, FileGroupDescriptorW

However, the FileContents format returns null.

Thanks, I can repro the null return value, but it happens both in Desktop Framework and .NET Core.

From debugging the .NET Core implementation I see that the clipboard returns a 0x800401D3 (CLIPBRD_E_BAD_DATA) exception when reading the FileContents data. From what I can tell this is either a bug in Windows or the source application (which would be explorer) and I doubt there's anything WinForms can do if the clipboard source doesn't provide data for a format (even if it announced it had data it can still fail to deliver it, or deliver it in a format WinForms doesn't understand)

I tried various different files in case this was file-dependent but I could not get it to work, neither in Desktop Framework nor in .NET Core. Do you have a scenario which works for you in Desktop Framework? because you said

Have you experienced this same bug with .NET Framework?: No

I've confirmed the "bad data" is actually a .net bug. .net is probably not using the lIndex property to indicate which index it wants. -1 is an invalid index (which is what's used in other clipformats).
In fact I'm able to get the data from explorer in my C# app by using p/invoke.

Nice! Do you have a fix suggestion?

Nice! Do you have a fix suggestion?

@RussKie Yeah I have a full implementation of clipboard APIs - including metafiles, EMF and file streams, here:
https://github.com/asklar/clipview

Specifically see https://github.com/asklar/clipview/blob/master/src/Clipboard/DataObject.cs the parts about FileContents and FileGroupDescriptor (DataObject.InternalGetData).

Was this page helpful?
0 / 5 - 0 ratings