Skip to content

Instantly share code, notes, and snippets.

@silverjam
Created August 16, 2016 23:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silverjam/3787f61d4d6579aae72b5064adf3168d to your computer and use it in GitHub Desktop.
Save silverjam/3787f61d4d6579aae72b5064adf3168d to your computer and use it in GitHub Desktop.
Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.ComponentModel;
public static class Kernel32c
{
[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Ansi)]
public static extern IntPtr LoadLibrary(
[MarshalAs(UnmanagedType.LPStr)]string lpFileName);
[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
public static extern IntPtr GetProcAddress(
IntPtr hModule,
string procName);
[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
public static extern uint GetLastError();
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);
public static void LoadWin32Library(string libPath)
{
System.IntPtr moduleHandle = LoadLibraryEx(libPath, IntPtr.Zero, 0);
if (moduleHandle == IntPtr.Zero) {
int error = Marshal.GetLastWin32Error();
System.Console.WriteLine(error);
throw new Win32Exception(error);
}
}
}
"@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment