Red Gate forums :: View topic - Add "automated error reporting" to DLLs
Return to RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
SmartAssembly 5
SmartAssembly 5 forum

Add "automated error reporting" to DLLs

Search in SmartAssembly 5 forum
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.
Jump to:  
Author Message

Joined: 16 Jul 2010
Posts: 12

PostPosted: Mon Jul 19, 2010 9:57 am    Post subject: Add "automated error reporting" to DLLs Reply with quote


The main part of our program is still in Visual Basic 6 and some smaller parts in .Net. The .Net parts are DLL assemblies. If I want to add the automated error reporting to my .Net DLLs, the SmartAssembly say:
Because this assembly is a library, these options will not work automatically. They will only work if accessed by an EXE that has been processed by SmartAssembly.
Is there a way to add the automated error reporting manually to DLL assemblies?

Back to top
View user's profile Send private message

Joined: 03 Feb 2010
Posts: 83
Location: Cambridgeshire

PostPosted: Wed Jul 21, 2010 8:34 pm    Post subject: Reply with quote

I think the best answer I can give is ....possibly.

It is possible to get SmartAssembly to add error reporting to a DLL, none of which are particularly nice (as described below, it is not sensible for the vast majority of situations, so we discourage it).

However, when I tried it for the situation you describe using COM between them it did not work. I'm not sure whether it is something to do with my test programs, or whether there is something on the way COM works for .NET is stopping it.

You may get it to work though - I've given the instructions below.
Back to top
View user's profile Send private message

Joined: 03 Feb 2010
Posts: 83
Location: Cambridgeshire

PostPosted: Wed Jul 21, 2010 8:38 pm    Post subject: Reply with quote

The problem with DLL is that they are intended to be used by an external application.
In this schema, throwing exceptions in a must-have.

For example, this is the responsibility of the DLL to throw an ArgumentException if a method is not properly called.
And the caller (the .EXE) must handle the exception and behave accordingly.

So, most of the exceptions must be simply report to the caller.

For this very reason, it is not possible to report unhandled exception as a global practice.
That's why the option is not available for DLL in {smartassembly}.

Anyway, a couple of workaround is available for some kind of DLL (mainly web-services or add-ins) but it requires a few changes in the DLL code (and {smartassembly}'s SDK for the second one):

1) Creating a DLL with an entry point.

The trick is to create a Console App EXE instead of a DLL. The only difference between the two is that the Console App has an entry-point.
You do nothing in the entry-point (the 'Main' method), but {smartassembly} will add its own code in this method.
In the static constructor of your plug-in class, you call the entry-point.

class Program
static void Main()

class MyPlugin : IPlugin
static MyPlugin()
//ensure that {sa} initialization is executed

//your code here...

Then, you just have to rename the .EXE into a .DLL, and you have a DLL with an entry-point.

2) Using SmartAssembly.ReportException.dll (Requires the Enterprise edition of {smartassembly}).
You need to refer the dependency "C:\Program Files\{smartassembly}\SDK\bin\SmartAssembly.ReportException.dll" (this dependency will be removed when processed by {smartassembly}).

Then, for any public method of your DLL that you want to protect, you need to do the following:
Of course, you just need to do it for the "entry-points" of your DLL.

using SmartAssembly.ReportException;

public class MyClass
private void DoSomethingInternal()
//Your code here

public bool DoSomething()
return true;
catch (Exception exception)
return false;

A couple of things to note:
- if you use method 1, SmartAssembly has slightly different default obfuscation and pruning settings for applications and libraries (for applications the protection is generally slightly stronger).
- if you use method 2, on SmartAssembly 4 you need to manually add option to enable the unhandled exception feature in the project file. If you open the "{sa}proj" file for the library (it is a simple XML file) you need to add the node "<ExceptionReporting ReportExceptions="1" />" under the "Options" section - e.g.
<ExceptionReporting ReportExceptions="1" />

In SmartAssembly 5 there is an option in the UI to change the setting so you don't have to manually edit the project file.

The other method you can use is similar to method 2; however, instead of adding a try/catch to every method, you manually add a handler to the framework's unhandled exception event.
The handled exception event varies being different types of applications. For a WPF class library the main one of AppDomain.UnhandledException. So you will want to add something like:
Public Sub AppDom_UnhandledExceptions(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs) Handles _curDomain.UnhandledException
SmartAssembly.ReportException.ExceptionReporting.Report(CType(args.ExceptionObject, Exception))
End Sub

You will then need to add a static initialiser which links this event handler to the event.
Back to top
View user's profile Send private message
Display posts from previous:   
This topic is locked: you cannot edit posts or make replies. All times are GMT + 1 Hour
Page 1 of 1

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group