0

I am writing a license module (DLL) for multiple applications. This DLL will be used in applications by adding reference. One of the requirement (pass case) for this DLL is that, if license validation fails, calling application should terminate/crash. It should not gracefully shutdown; it must crash. I do not want to show message, write log etc.

DLL and applications (using this DLL) are written in DotNet 4.

Quick solution I can think of is to throw exception instead of returning value from method. But, the exception could be caught by application and purpose will not be fully served.

Workaround for this is to declare custom exception as internal in my DLL. But, this also could be bypassed by catching Exception class.

One dirty alternative I can think of is to write a code (endless recursion or something) that will throw StackOverflowException. But I am looking for something better.

Is there any way to throw custom non-catchable exception?

References:

Ref1 and Ref2 discuss about in built DotNet non-catchable exception. My question is about custom non-catchable exceptions.

Community
  • 1
  • 1
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • You could always just kill the current process with a non-zero exit code. – Abion47 Jan 31 '17 at 11:59
  • 4
    How about Environment.FailFast? https://msdn.microsoft.com/en-us/library/ms131100(v=vs.110).aspx – Joe Jan 31 '17 at 11:59
  • If you want it to crash, what's wrong with a StackOverflowException? In what way are you looking for something better? – Steve Smith Jan 31 '17 at 12:04

1 Answers1

1

Environment.FailFast is the way to go, nothing can then prevent your application from shutting down.

Keep in mind that C# libraries can be easily changed and recompiled, so you might also want to look into using obfuscators as well.

Community
  • 1
  • 1
galister
  • 430
  • 3
  • 9
  • I am already obfuscating DLL as per needs; so that is not an issue. I will try your solution. – Amit Joshi Jan 31 '17 at 12:22
  • Does this crash all applications including WinForm, WinService, WPF? How does this work on applications hosted under IIS like WebService, WebAPI, WebForms? Will it crash IIS or it will only crash application? – Amit Joshi Jan 31 '17 at 12:25
  • It pretty much kills the calling process, doesn't matter which of those technologies you're using. It will also display the "application stopped unexpectedly" box on windows, if applicable. – galister Jan 31 '17 at 13:02