Dynamically loaded DLL thows AccessViolationException on first function call

October 22, 2019 .net c#

In my library GLESDotNet I load the libegl.dll and libglesv2.dll DLLs dynamically via the Win32 functions LoadLibrary and GetProcAddress. I already had everything working via DLLImport but I wanted to be able to load the DLLs from different subdirectories depending on the architecture.

For the first test I only loaded the libegl.dll DLL before moving on to the libglesv2.dll. I made the classic programmer mistake of changing too much at one time. The first call to any function in libegl.dll resulted in an AccessViolationException. I couldn't figure out what was going wrong so I decided to revert my changes and try again.

I noticed that when the DLLs were in the same directory the AccessViolationException went away. Changing the working directory to the directory of the DLLs also solved the problem once the DLLs were placed in subdirectories again. This led me to believe that loading libegl.dll must be implicitly loading libglesv2.dll. Loading libglesv2.dll via LoadLibrary along with libegl.dll solved the problem.

Abusing IDisposable

August 27, 2019 .net c#

In C# the interface IDisposable is usually used to indicate that an object needs to dispose of unmanaged resources. C# provides the using syntax to ensure that the Dispose method is called once the instance of the object is no longer needed.

Dispose is just like any other method though and the using syntax can be used quite nicely for other use cases as well. Let's create a class we can use to collect some actions to be "deferred" until a later time.

Read More

Generating string permutations for unit testing

February 11, 2019 .net c# nunit

I needed to parse a string containing 6 characters. Each character should only be one of 3 possibilities: '?' for null, '0' for false or '1' for true. The problem sounded easy enough to generate a whole bunch of unit tests for.

Read More

string.Join Extension Method

April 19, 2018 .net c# linq

Normally if want to do a string.Join on the result of a Linq query you end up with somthing looking like this:

Console.WriteLine(string.Join(", ", numbers.Where(x => x % 2 == 0)));

The call to string.Join has to come first and then the Linq query. I've always felt this breaks the flow of the code and would be easier to read if the string.Join was at the end of the Linq query:

Console.WriteLine(numbers.Where(x => x % 2 == 0).JoinString(", "));

This can be implemented with the following extension methods:

public static class JoinStringExtensions
{
    public static string JoinString<T>(this IEnumerable<T> source, string seperator) =>
        string.Join(seperator, source.Select(x => x.ToString()));

    public static string JoinString(this IEnumerable<string> source, string seperator) =>
        string.Join(seperator, source);
}

The specialization for IEnumerable<string> is just mirroring the implementation from string.Join.

Run a program directly after building in VS Code

Whenever I have a program that is just a generator of some kind I like to have that program execute directly after having built the program successfully. I'm going to show how to run the program via the dotnet cli but this trick can easily be applied to regular .NET programs or any program which is built using MSBuild.

Read More