mirror of
https://github.com/chibicitiberiu/rainmeter-studio.git
synced 2024-02-24 04:33:31 +00:00
54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
namespace RainmeterEditor.Interop
|
|
{
|
|
public class NativeLibrary : IDisposable
|
|
{
|
|
#region Imports
|
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern IntPtr LoadLibrary(string libname);
|
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
|
|
private static extern bool FreeLibrary(IntPtr hModule);
|
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
|
|
private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
|
|
|
|
#endregion
|
|
|
|
public string DllName { get; private set; }
|
|
private IntPtr _handle;
|
|
|
|
public NativeLibrary(string dllName)
|
|
{
|
|
// Set properties
|
|
DllName = dllName;
|
|
|
|
// Load library
|
|
_handle = LoadLibrary(DllName);
|
|
if (_handle == IntPtr.Zero)
|
|
{
|
|
int errorCode = Marshal.GetLastWin32Error();
|
|
throw new BadImageFormatException(string.Format("Failed to load library (ErrorCode: {0})", errorCode));
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (_handle != IntPtr.Zero)
|
|
FreeLibrary(_handle);
|
|
}
|
|
|
|
public Delegate GetFunctionByName(string name, Type delegateType)
|
|
{
|
|
IntPtr addr = GetProcAddress(_handle, name);
|
|
return Marshal.GetDelegateForFunctionPointer(addr, delegateType);
|
|
}
|
|
}
|
|
}
|