Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to only perform a function if a (.NET) DLL is loaded?

Tags:

c#

dll

I am not sure the best way to explain this so please leave comments if you do not understand.

Basically, I have a few libraries for various tasks to work with different programs - notification is just one example.

Now, I am building a new program, and I want it to be as lightweight as possible. Whilst I would like to include my notification engine, I do not think many people would actually use its functionality, so, I would rather not include it by default - just as an optional download.

How would I program this?

With unmanaged Dlls and P/Invoke, I can basically wrap the whole lot in a try/catch loop, but I am not sure about the managed version.

So far, the best way I can think of is to check if the DLL file exists upon startup then set a field bool or similar, and every time I would like a notification to be fired, I could do an if/check the bool and fire...

I have seen from the debug window that DLL files are only loaded as they are needed. The program would obviously compile as all components will be visible to the project, but would it run on the end users machine without the DLL?

More importantly, is there a better way of doing this?

I would ideally like to have nothing about notifications in my application and somehow have it so that if the DLL file is downloaded, it adds this functionality externally. It really is not the end of the world to have a few extra bytes calling notification("blabla"); (or similar), but I am thinking a lot further down the line when I have much bigger intentions and just want to know best practices for this sort of thing.

like image 400
Wil Avatar asked Feb 26 '11 22:02

Wil


People also ask

How do you check if a DLL is already loaded?

Once running, enable viewing of loaded DLLs by either pressing CTRL+D or using the View > Lower Pane View > DLLs entry from the menu bar. Select the target process in the upper pane. The lower pane should now show loaded modules.

How do you call a DLL function in C#?

In order to reference a dll, just add the compiler flag /r:PathToDll/NameOfTheDll to csc. In FileWhichIsReferencingTheDll. cs add using namespace AppropriateNameSpace; to access the functions (by calling class. functionName if static or by creating an object of the class and invoking the function on the object).

What is the difference between exe and DLL in C#?

EXE is an extension used for executable files while DLL is the extension for a dynamic link library. 2.An EXE file can be run independently while a DLL is used by other applications. 3. A DLL file can be reused by other applications while an EXE cannot.


1 Answers

I do not think many people would actually use its functionality, so, I would rather not include it by default - just as an optional download.

Such things are typically described as plugins (or add-ons, or extensions).

Since .NET 4, the standard way to do that is with the Managed Exensibility Framework. It is included in the framework as the System.ComponentModel.Composition assembly and namespace. To get started, it is best to read the MSDN article and the MEF programming guide.

like image 87
Wim Coenen Avatar answered Oct 07 '22 03:10

Wim Coenen