1

I am having a weird problem on one of my computers but not the other where it works fine. I am trying to read the entire content of the clipboard and save that to an array, Dictionary<string, object>. In my concrete example below, I am copying a formatted text in Word.

In my foreach loop it run through the first 5 formats but when it come to a format called "EnhancedMetafile" then it comes with this error and halts the application:

Managed Debugging Assistant 'FatalExecutionEngineError' : 'The runtime has encountered a fatal error. The address of the error was at 0x702e4463, on thread 0x44e8. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.'

Continue running the application with F5 shows this error:

System.ExecutionEngineException: 'Exception of type 'System.ExecutionEngineException' was thrown.'

The clipboard data format array

My code is this and it fails in the last line with dictionary.Add(format, clipboardDataObject.GetData(format));:

// Variables
IDataObject clipboardDataObject = null;

// Get object from clipboard
clipboardDataObject = Clipboard.GetDataObject();

// Get all formats in to an array
var formats = clipboardDataObject.GetFormats(false);

// Create new array
var dictionary = new Dictionary<string, object>();

// Run through all formats and add it to the array
foreach (var format in formats)
{
    dictionary.Add(format, clipboardDataObject.GetData(format));
}

I am using Visual Studio 2019. CLRver.ToString() reports .NET 4.0.30319 though I have set the Target framework to be .NET Framework 4.8 in the project properties.

I did look in to Managed Debugging Assistant 'FatalExecutionEngineError' but there was nothing there helping me out. I have seen a few other proposals also, all suggesting to more or less update the computer but this is not easy when this is a company computer and I can tell exactly what should be updated.

Is there anyone knowing more about this?

Beauvais
  • 2,149
  • 4
  • 28
  • 63
  • 1
    0xc0000005 is an access violation (similar to NullReferenceException in .NET).Can we have the Word document for reproducing the issue? – Thomas Weller Sep 23 '20 at 20:20
  • I don't think the Word file is so unique, nor do I want to attach it here as I have no idea what kind of company metadata is automatically embedded. It also happens when I copy from Outlook. We are using Office 365. I don't have an overview, if this is only happening to Office applications. If this is the case then I assume it be our classifications that could be a problem, where we can restrict the various documents (none of what I copy has been classified). – Beauvais Sep 23 '20 at 20:54
  • With `clipboardDataObject.GetData(DataFormats.MetafilePict, false);` you should get a MemoryStream. – Jimi Sep 23 '20 at 21:10
  • 1
    EMF is [compromised](https://en.wikipedia.org/wiki/Windows_Metafile_vulnerability), you need to mistrust the kind of shrink-wrapped malware that pretends to solve this problem but screws up doing so. – Hans Passant Sep 23 '20 at 21:32
  • That format is not handled by .Net. and not exactly useful anyway. See the size of the MemoryStream. You can check the HtmlFormat if you have copied or drag/dropped images. These are copied to the System `Temp` folder and the path is provided. – Jimi Sep 23 '20 at 21:36
  • I don't need this specific format but it is present when copying a standard text snippet from e.g. Outlook and Word ;-) However, then it becomes a little troublesome if I need to have special attention on various formats as I am sure there will be other formats that could have the same problem? So when will my application crash the next time with another format? :-) – Beauvais Sep 24 '20 at 06:15
  • Read only the formats you want to handle. – Jimi Sep 24 '20 at 12:16
  • I actually want to restore the content of the clipboard after some time - which means I need to get all data and all formats from it first. – Beauvais Sep 24 '20 at 12:51
  • 1
    Nope, for that you need the IDataObject as a whole, not disassemble/reassemble pieces of it. Note that the ClipBoard data also has an owner... Handle specific formats for your own requirements. (Btw, you can create a new DataObject from the IDataObject you receive, `var dataObj = new DataObject([The original IDataObject]);`), – Jimi Sep 24 '20 at 14:40
  • @Jimi - if you could convert your comment to an answer with a little more details on how to save the full clipboard content in to an array and then restore the content again to clipboard then I would probably accept that as the answer. – Beauvais Sep 25 '20 at 06:34
  • But that would not be the answer to the question you asked. @Jimi's answer answers your overall problem, yes. What we see here is an XY issue: you tried to solve a task, you did it the wrong way (happens to all of us) and now you're asking the wrong question, because you're already too deep into it. – Thomas Weller Sep 25 '20 at 08:00
  • @HansPassant: I always appreciate your thoughts. However, it's sometimes hard for me to understand. By "shrink-wrapped malware" you mean virus scanners? And the virus scanner tries to protect us from payload in EMF, so the virus scanner code is being executed, resulting in an access violation, messing up the .NET runtime. So, OP should uninstall the virus scanner and try again? Is that what you have condensed into your comment? – Thomas Weller Sep 25 '20 at 08:06
  • @Jimi: we have the related https://stackoverflow.com/a/2579846/480982 Do you think your approach would bypass these issues? – Thomas Weller Sep 25 '20 at 08:08
  • @Thomas Weller Copy the IDataObject? Absolutely! After that, you build all `FORMATETC` structs to check whether you can actually handle the returned `STGMEDIUM`, unadvice/advice the connections' sinks but probably severe those obnoxious COM links to rebuild the object and there you have it. Now, setting back a managed Com Interop IDataObject may have mild side effects: the Shell may not work anymore, Explorer may not show its SysListView, the TaskBar is a wax statue. There's the Handles thing... If you ignore these details, it works perfectly. *Perfection* is notoriously a volatile concept. – Jimi Sep 25 '20 at 16:32
  • If you're trying to *restore* the Clipboard because you're planning to use the Clipboard as *communication medium*, then don't. Very simple managed named/unnamed pipes can be used for this. See [NamedPipeClientStream](https://docs.microsoft.com/en-us/dotnet/api/system.io.pipes.namedpipeclientstream) and [NamedPipeServerStream](https://docs.microsoft.com/en-us/dotnet/api/system.io.pipes.namedpipeserverstream) (these can also work both ways). With XML/JSON serialization, you can transfer managed class structures with almost no effort, you don't even need to know anything about serialization. – Jimi Sep 25 '20 at 17:06

0 Answers0