LegacyWrapper 3.0 released

Category: Programming - 03.02.2019
Tags: .NET C# LegacyWrapper

Today, I released version 3.0 of my nuget package LegacyWrapper. LegacyWrapper 3.0 supports calling methods from legacy DLLs with a convenient managed method call.

There’s no need to hard-code the method name any more:

// Create new Wrapper client providing the proxy interface
// Remember to ensure a call to the Dispose()-Method!
using (var client = WrapperProxyFactory<IUser32Dll>.GetInstance(configuration))
{
    // Make calls - it's that simple!
    int x = client.GetSystemMetrics(0);
    int y = client.GetSystemMetrics(1);
}

Compared to the old LegacyWrapper interface, this looks much better, doesn’t it? Well, at first, you have to provide some configuration:

IWrapperConfig configuration = WrapperConfigBuilder.Create()
        .TargetArchitecture(TargetArchitecture.X86)
        .Build();

Also, the target interface has to be defined as a native C# interface that inherits from IDisposable:

[LegacyDllImport("User32.dll")]
public interface IUser32Dll : IDisposable
{
    [LegacyDllMethod(CallingConvention = CallingConvention.Winapi)]
    int GetSystemMetrics(int nIndex);
}

See the attributes on the interface and the method? That’s how you can control DLL loading now.

But that’s it. Really!

Internals

LegacyWrapper now uses Castle to generate dynamic proxies. This makes the wrapper client look like a native method call. On the plus side, there are no strings any more - that means, method names can now easily be refactored.

Therefore, there is no longer the need to:

Other things that changed:

As always, if you notice anything odd regarding LegacyWrapper, you can get in touch on GitHub.

Click here for the original blog post explaining LegacyWrapper.

Click here for the project at GitHub.