Created
August 16, 2016 23:51
-
-
Save silverjam/3787f61d4d6579aae72b5064adf3168d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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