0

I have a C# project which is an exe file (console app) and some dlls and one dll ist written in C++. On Winodws 10 it runs fine and on a Win 7 too. But on a Win 7 and Win2008 Server on a Virtual Maschine I get an unhandled exception. In the main function I have already added

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

But unfortunately the exception must be thrown before the main function is called. How can I catch this exception?

I have installed .net 4.6.1 and I compared the installed packages between Win 10 and Win 7 on real PCs with the installed packages on the virutal maschines and I fine no different.

Thats what I can see in the event log:

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Application Error" />
<EventID Qualifiers="0">1000</EventID>
<Level>2</Level>
<Task>100</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2020-08-14T10:03:41.000000000Z" />
<EventRecordID>5060</EventRecordID>
<Channel>Application</Channel>
<Computer>PC-dummy</Computer>
<Security />
</System>
<EventData>
<Data>MyApp.exe</Data>
<Data>1.6.1.1</Data>
<Data>5f365f53</Data>
<Data>KERNELBASE.dll</Data>
<Data>6.1.7601.24511</Data>
<Data>5d3fa9ff</Data>
<Data>e0434352</Data>
<Data>000000000000b87d</Data>
<Data>658</Data>
<Data>01d67222300b6618</Data>
<Data>D:\MyProject\MyApp.exe</Data>
<Data>C:\Windows\system32\KERNELBASE.dll</Data>
<Data>6dbcb3b9-de15-11ea-b17f-000c29a2a32b</Data>
</EventData>
</Event>

and

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name=".NET Runtime" />
<EventID Qualifiers="0">1026</EventID>
<Level>2</Level>
<Task>0</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2020-08-14T10:03:41.000000000Z" />
<EventRecordID>5059</EventRecordID>
<Channel>Application</Channel>
<Computer>PC-dummy</Computer>
<Security />
</System>
<EventData>
<Data>Application: MyApp.exe Framework Version: v4.0.30319 Description: The  process was terminated due to an unhandled exception. Exception Info: System.IO.FileNotFoundException at MyConsole.Program.Main(System.String[])</Data>
</EventData>
</Event>
Fellnase
  • 103
  • 1
  • 9
  • What exception are you getting ? what do you mean by this ? => But unfortunately the exception must be thrown before the main function is called. Main is the entry point. – SRIDHARAN Aug 17 '20 at 13:47
  • surround it with a try-catch – sommmen Aug 17 '20 at 13:51
  • 2
    The exception details would give us more visibility into your issue. – imjohnking Aug 17 '20 at 13:52
  • 2
    Have a look in the application event log, unhandled exceptions crashing the process will be logged there. – Klaus Gütter Aug 17 '20 at 13:53
  • As @imjohnking points out, telling us all you can about exception would be useful. The type, the message, and especially the stack. What you are describing is pretty rare unless you have an assembly load issue. – Flydog57 Aug 17 '20 at 14:02
  • I have added what I have found in the event log. I'm wondering why UnhandledException is not catching the exception – Fellnase Aug 17 '20 at 14:21
  • The `.UnhandledException` event handler does *not* catch exceptions or handle them. It is strictly a last-minute way to log an exception if nothing else has been provided; it will not prevent the process from exiting. – Jeroen Mostert Aug 17 '20 at 14:24
  • Yes I know but the message isn't logged either. I added a breakpoint in the function to be able to see the call stack but it never reachs the breakpoint – Fellnase Aug 17 '20 at 14:30
  • Did you enclose your code in try-catch-blocks? This should give you access to the exception details. – Wolfgang Jacques Aug 17 '20 at 14:31
  • It's possible for a process to exit before the first line of `Main` is executed, but improbable, especially for managed code. At the very least a reference to a static type would be required to initialize an assembly before any `Main` code can run, and this exception stack isn't indicating that's happening (it only gives `Main` as the location). If it is happening in a static initializer, you're basically screwed. Capture the stack dump with something like `procdump`, load it in your favorite debugger (like WinDbg with the SOS extensions) and inspect the exception info. – Jeroen Mostert Aug 17 '20 at 14:33
  • From the details that are in the messages you updated with, it looks like you, from inside your main, are trying to open a file that does not exist. – fredrik Aug 17 '20 at 14:34
  • If you can run a debugger on the machine where it happens, you can simply enable the debugger to stop on all managed exceptions and see the location that way, no breakpoints required. Alternatively, you can use `procmon` on the machine to capture the actual "file not found" error with the file path that's not found, which can give clues as to what's happening. All this is assuming you already wrapped the entire content of your `Main` in a `try .. catch`, as others have advised. Do that first if you haven't done it yet. – Jeroen Mostert Aug 17 '20 at 14:35
  • 1
    FileNotFoundException is very common when you deploy to another machine, you simply forgot to copy a DLL that the program needs. Commonly it is one that some machines happen to have by accident, like the [C++ runtime libraries](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads). – Hans Passant Aug 17 '20 at 14:36
  • It is possible that the exception occurs when compiling the main method. A way to test this would be to move all the code in main to a separate method and call this from main, in a try/catch to allow you to log the exception. Methods are usually compiled the first time they are called, references are resolved at compile time, and will throw if they fail. – JonasH Aug 17 '20 at 15:00
  • SysInternals' Process Monitor may help you find out what's going on. Set it up, filter on the process name and then try to run your app. You should see what file the File Not Found is trying to find – Flydog57 Aug 17 '20 at 15:32
  • unfortunately I didn't succeed with all your ideas. I tried to create a dump which said it was not successful. Processmonitor didn't show me anything either. I added a try catch which should write at least a log entry but no logfile is created. My guess is that I miss some files beause on Win10 everything runs fine. Is there anything like dependswalker which can track the app if any dll is missing? – Fellnase Aug 18 '20 at 07:31
  • Process Monitor not showing anything is, well, not impossible but extremely unlikely. If you're filtering events specifically for the executable, you definitely should get `NAME NOT FOUND` errors for the file in question (among a bunch of other events that won't be relevant to your scenario), even when it's the loader producing the events and no application code has run yet. Be sure to run Process Monitor with elevation and compare a successful with an unsuccessful run. Tracking dependencies manually is error-prone because files are often loaded dynamically. – Jeroen Mostert Aug 18 '20 at 13:21

0 Answers0