Catch UnhandledException in com assemblies - c#

I search a way to locate UnhandledException in a native c++ desktop application. The error occurs in a .net assembly that is called by in process com interop. Problems are I do not know witch assembly causes the UnhandledException and I can not reproduce on my developer machine, so I can not simply use the debugger.
I try with AppDomain.CurrentDomain.UnhandledException but I think each com call get it's own AppDomain. Whatever it does not work, app is still crashing and event is not triggered.
Also I try to use stackify prefix, but may app is not an webservice and not an windows service, so this does not work.
Event logs are written but do not point to the error.
Any idea how I could identify the assembly causing the problem?
-- Edit --
Not all assemblies used via com interop are my own, so it is not that easy to add code to all of them.
Erros in event log is:
Source: .Net Framework
ID: 1026
Message: The process was terminated on a unhandeled exception.
Errorcode c0000005

Related

Fatal error. Internal CLR error. (0x80131506) on String.Format() [duplicate]

The exception code C0000005 is thrown from mscorwks.dll when the application is run on Windows Server 2008 R2 launched using test complete. Other platforms (Windows XP, Server 2003 R2, Windows 7 32-bit and 64-bit) do not present this exception.
The event log from a single execution has many of the following event with event ID 1023 raised by the .NET Runtime:
.NET Runtime version 2.0.50727.4952 - Fatal Execution Engine Error (7383851A) (80131506)
The application itself makes use of a SOAP interface generated by Visual Studio from a WSDL file, a COM object with an embedded interop, and is targeting .NET 4.
sfc /scannow was run and found no problems with system files on the affected system.
What troubleshooting can be done to identify a solution?
Fatal Engine Execution Error and an access violation are both symptoms of the same problem. FEEE is raised when the .NET garbage collector detects that the internal structure of the garbage collected heap is destroyed. An access violation is a hardware exception, raised by the processor when it is asked to access memory with an invalid address. A common cause of an AV is heap corruption.
These kind of mishaps are very commonly caused by unmanaged code. It is also quite common for unmanaged code to have latent memory management bugs that can go unnoticed for a long time. The kind of damage the bug can do tends to be quite random. Just running it on another operating system which has a different memory allocation pattern can be enough to trigger the bomb.
You have an excellent candidate for the source of the trouble. You'll need to work with the COM server vendor or author to chase the bug.
A 0xC0000005 is an exception code wrapping a Win32 error which means "Access Denied." Given that you are using COM interop and are getting an ExecutionEngineException (in COM, COR_E_EXECUTIONENGINE; 0x80131506), my guess is that either it's a NULL pointer in the COM component or a faulty ComImport directive in your .NET code.
This error is caused by flaws in the way that TestComplete 7 interacts with the heap in mixed managed/unmanaged applications. Instead of using the TestedApp.Run method using the following block of code, modified for you choice of scripting language, presented in VBScript:
Dim oScript, command
Set oScript = CreateObject("WScript.Shell")
command = "%comspec% /c " & PATH_TO_EXE & " " & Args
oScript.Run command, 10, True
The relevant MSDN article is Run Method (Windows Script Host).
TestComplete 7 (including the latest update 7.52) supports .NET Framework 4 only up to version .NET 4 Beta 2. It does not support the release version of the Framework, so this may be the reason of the problem.
Try building the application targeting .NET 2.0. This should resolve the problem.
To test .NET 4 applications, you will need to use TestComplete 8.

Dynamics CRM 2011 SyncWorkflowExecutionPlugin unable to find assembly

A strange error suddenly cropped up on our Dynamics CRM 2011 server - a specific plugin assembly can not be found when the plugin execution is triggered through a workflow process, with the error:
[Microsoft.Crm.ObjectModel: Microsoft.Crm.ObjectModel.SyncWorkflowExecutionPlugin]
[324f29aa-6082-ea11-80ee-005056837add: ]
Starting sync workflow 'workflow name', Id: 2d4f29aa-6082-ea11-80ee-005056837add
Sync workflow 'workflow name' terminated with error 'The type initializer for '<Module>' threw an exception.'
And exception stack trace ultimately leads to:
Inner Exception: <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.
at <CrtImplementationDetails>.ThrowModuleLoadException(String errorMessage, Exception innerException)
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
at .cctor()
Inner Exception: System.Runtime.Serialization.SerializationException: Unable to find assembly 'AssemblyName, Version=8.75.0.267, Culture=neutral, PublicKeyToken=4e4051c21cef725b'.
The assembly is present on the server, version, culture, and public key token all match. Oddly enough the plugin steps work fine when the execution is triggered directly through a create/update/etc request - it's only when the execution is triggered through a workflow that the error occurs. And by triggered through a workflow I don't mean running code activity from that assembly, I mean a situation like:
Entity is created
OnCreate plugin from Assembly1 runs without issues
Workflow runs that updates a related entity, triggering OnUpdate plugin from Assembly1 - ERROR
This issue cropped up initially after a server restart. Another restart fixed it temporarily, until two days later it started happening again. We've tried everything we can think of - updating the assembly, unregistering/re-registering it, restarting all services, restarting the server again. Does anyone have any idea what might be the cause of this and how to fix it?
We discovered the issue was caused by a Windows update that had been applied before the initial restart, which had installed .NET framework v4.8 on the server. Although the application was still using an older version, apparently just having v4.8 installed was enough to cause errors. Reverting back to an earlier .NET version fixed the problem.
I am not certain what is the exact technical reason for this - seems to be something related to App Domains (https://learn.microsoft.com/en-us/dotnet/api/system.appdomain?view=netframework-4.8). But if you encounter a similar issue, check your .NET framework version.

C# compiled .exe System.IO.FileNotFOundException

(Environment = Windows 10 1803, program type is .exe)
I have written a program to be used across different computers at work, however, When i try to run the compile program from anyone elses machine other than my own (and my fellow IT coworkers), I end up with this error:
(From event viewer)
Event 1026, .NET Runtime Application: program123.exe Framework Version
v4.0.30.319 Description: The process was terminated due to an
unhandled exception. Exception Info: System.IO.FileNotFoundException
at
System.Reflection.RuntimeAssembly._nLoad(System.Reflection.AssemblyName,
System.String, System.Security.Policy.Evidence,
System.Reflection.RuntimeAssembly, ........
I have extensive logging in the application and pretty much log immediately upon program entry. However, my log never gets hit - (to me, this means that this application is being blocked, or dying before running any code).
This is different because my application does not even start. In the example that you recommended, the application begins and then fails on an event. My .exe will not work at all.
Any help would be appreciated. Thanks in advance.

AppDomain.CreateDomain causes Access Violation

I'm making a plugin loader for Unity game (Subnautica). The loading mechanism itself works fine but I want to load the assemblies into a separate app domain so I can unload them at runtime too.
However, calling AppDomain.CreateDomain(string) crashes Unity, leaving an error log with
Unknown caused an Access Violation (0xc0000005)
in module Unknown at 0033:049f7717.
...
Read from location 00000000 caused an access violation.
and the log the game produces with
0x0000000005048C6D ((<unknown>))
ERROR: SymGetSymFromAddr64, GetLastError: 'The specified module could not be found.'
ERROR: SymGetModuleInfo64, GetLastError: 'A dynamic link library (DLL) initialization routine failed.'
I have tried injecting the code that calls AppDomain.CreateDomain into the game's code directly and I tried injecting code that loads the dll that calls AppDomain.CreateDomain using Assembly.Load, both methods crash with the same message.
Unity version is 5.4.4f1, I'm on Windows 10 and the assembly that calls AppDomain.CreateDomain uses .net 3.5. This is the error.log created by Unity and the stack trace from output_log.txt created by Subnautica.
Does anyone have any idea what could be causing Unity to crash?
PS, I know with certainty that AppDomain.CreateDomain is causing the problem.

Exception not catched

I've a 64bit system with Win7 and I use VS2010 with .Net Framework 4.0.
In a Winform application I get an Exception from a third-party component, I'm trying to catch this exception with a Try-Catch but it doesn't work!
Why?
UPDATE
If I un-check CLR Exceptions It works properly but I'm forced to check it due to this known problem: Why the form load can't catch exception?
If I press F10 it continues entering in catch statement!!
Seems that you are using calling an unmanaged dll here. Try catching this exception using ExternalException Class. This may work for you but once you've gone outside of the .NET runtime's control, it's entirely up to the unmanaged code; there's nothing the .NET runtime can do.
Have you got the following setting unchecked:
Break when exceptions cross AppDomain or managed/native boundaries (Managed only)
in your Tools->Options->Debugging->General tab for Visual Studio 2010
Its explained in MSDN here

Categories