Class UnmanagedLibrary
Utility class to wrap an unmanaged DLL and be responsible for freeing it
Inheritance
Namespace: WizardWrx.Core
Assembly: WizardWrx.Core.dll
Syntax
public sealed class UnmanagedLibrary : IDisposable
Remarks
This is a managed wrapper over the native LoadLibrary, GetProcAddress, and FreeLibrary calls.
I didn't immediately notice the fact that this class is sealed, but not static; hence, public UnmanagedLibrary ( string fileName ) is its one and only public constructor. As insurance against accidentally creating a useless, uninitialized instance, I added a default constructor, marked as private.
Constructors
| Improve this Doc View SourceUnmanagedLibrary(String)
Constructor to load a DLL and be responsible for freeing it
Declaration
public UnmanagedLibrary(string fileName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fileName | full path name of DLL to load |
Remarks
Throws exceptions on failure. Most common failure would be file-not-found, or that the file is not a loadable image.
Methods
| Improve this Doc View SourceDispose()
Call FreeLibrary on the unmanaged DLL. All function pointers handed out from this class become invalid after this.
Declaration
public void Dispose()
Remarks
This is very dangerous because it suddenly invalidate everything retrieved from this DLL. This includes any functions handed out via GetProcAddress, and potentially any objects returned from those functions (which may have an implementation in the DLL).
GetUnmanagedFunction<TDelegate>(String)
Dynamically lookup a function via kernel32!GetProcAddress.
Declaration
public TDelegate GetUnmanagedFunction<TDelegate>(string functionName)
where TDelegate : class
Parameters
Type | Name | Description |
---|---|---|
System.String | functionName | raw name of the function in the export table |
Returns
Type | Description |
---|---|
TDelegate | null if function is not found, else a delegate to the unmanaged function |
Type Parameters
Name | Description |
---|---|
TDelegate |
Remarks
GetProcAddress results are valid as long as the DLL is loaded. This is very, very dangerous to use since you need to ensure that the DLL is not unloaded until after you’re done with any objects implemented by the DLL. For example, if you get a delegate that then gets an IUnknown implemented by this DLL, you can not dispose this library until that IUnknown is collected. Else, you may free the library, and then the CLR may call release on that IUnknown, leading to a crash.
Declare your delegate pretty much as you would in C or C++; if you happen to have a working typedef, use it as the pattern for your delegate declaration, which must have class cope.
Once you have your delegate declared, instantiate it by assigning it the value returned by calling this method on an instance that was constructed using the name of the DLL that exports the desired Win32 (or Win64) function.
Since the Win64 API generally uses the same names, signatures, and DLL names (e. g., it's still Kernel32.dll, and GetProcAddress is still GetProcAddress), most of your existing Windows API calls work unchanged in Win64. The magic that makes this possible is that Win32 code looks in %SystemRoot%\SysWOW64, while Win64 code looks in %SystemRoot%\System32 for system libraries. The clear goal of this initially confusing scheme was to make porting code to 64 bits as painless as possible.