C# comes with a "FatalExecutionEngineError" when copying clipboard data formats to new object


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?

asked on Stack Overflow Sep 23, 2020 by Beauvais • edited Sep 23, 2020 by Beauvais

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0