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 anymore:
1// Create new Wrapper client providing the proxy interface
2// Remember to ensure a call to the Dispose()-Method!
3using (var client = WrapperProxyFactory<IUser32Dll>.GetInstance(configuration))
4{
5 // Make calls - it's that simple!
6 int x = client.GetSystemMetrics(0);
7 int y = client.GetSystemMetrics(1);
8}
Compared to the old LegacyWrapper interface, this looks much better, doesn’t it? Well, at first, you have to provide some configuration:
1IWrapperConfig configuration = WrapperConfigBuilder.Create()
2 .TargetArchitecture(TargetArchitecture.X86)
3 .Build();
Also, the target interface has to be defined as a native C# interface that inherits from IDisposable
:
1[LegacyDllImport("User32.dll")]
2public interface IUser32Dll : IDisposable
3{
4 [LegacyDllMethod(CallingConvention = CallingConvention.Winapi)]
5 int GetSystemMetrics(int nIndex);
6}
See the attributes on the interface and the method? That’s how you can control DLL loading now.
But that’s it. Really!
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 anymore - that means, method names can now easily be refactored.
Therefore, there is no longer the need to:
delegate
object
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.