LegacyWrapper 3.0 released

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:

  • Create a delegate
  • Cast result values
  • Pass function parameters as array of object
  • Supply the method name as magic string

Other things that changed:

  • Documentation on classes and methods has been improved
  • There are more unit tests now (especially edge cases)

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.

GitHub Twitter

Codefoundry