jump to navigation

Late Binding in C# May 9, 2006

Posted by codinglifestyle in C#.
Tags: , , , ,

A recurring nuisance for me is the ungraceful crash of a C# application due to a missing or out-of-date class library.  When linking DLLs normally the Windows Loader does the job of loading everything in to virtual memory.  As far as I know there is no way to catch an error at this point because, technically, your program never even started running.  What I would like is to be able to inform the user that a DLL is missing and exit gracefully.  This sent me googling for alternatives.  In C++ there is the concept of late binding allowing DLLs to be loaded when required.  In addition to reducing the startup time of your application an additional benefit is being able to catch the error if the DLL fails to load and handle it gracefully.  To the best of my knowledge, this is the only solution to address this problem.  Whether it makes sense to do this (vs. investing time in your installer) is up to you.

The following discusses this idea for loading unmanaged Dlls:

There is a similar concept available in .NET 1.1 which is discussed here, but I warn you this is for the more die-hard among us: http://www.codeproject.com/csharp/dyninvok.asp

Here is a bit of wisdom I found in a forum regarding late binding:

If you are using .NET 2.0 or above, this is simple. You can declare
LoadLibrary and GetProcAddress to load your dll and get the address of your
function. Once you do that, you can pass the pointer to the function to the
static GetDelegateForFunctionPointer method and have it return your delegate
(you will have to create a delegate that has the appropriate signature).

    You can get the declarations for GetProcAddress and LoadLibrary at

    If you are running .NET 1.1 or before, then you will have to have an
unmanaged shim which will load the library for you, and return the function
pointer (which you can marshal as a delegate).

You will note the codeproject article uses the unmanaged shim mentioned above.  Take a look at http://pinvoke.net mentioned above.  This looks to be very useful site.

Another idea which sprang to mind is to write a loader to walk dependencies of the destination exe.  If all dependencies are present then it would execute the application.  If not, it could inform the user or even attempt to download them.

Lastly, there is a open-source utility called Netz which will pack the exe and its dlls together in to a single exe thus avoiding the whole issue altogether.


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: