Skip to content

Instantly share code, notes, and snippets.

@nylyst
Last active August 1, 2020 16:38
Show Gist options
  • Save nylyst/5f109777892483aa9a334f86ce930854 to your computer and use it in GitHub Desktop.
Save nylyst/5f109777892483aa9a334f86ce930854 to your computer and use it in GitHub Desktop.
Powershell to register a dll without regsvr32.exe
$registerDLL = @"
namespace RegisterDLL {
using System;
using System.Runtime.InteropServices;
public static class Kernel32 {
[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.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool FreeLibrary(IntPtr hModule);
}
public static class User32 {
//[DllImport("user32.dll")]
//public static extern IntPtr CallWindowProc(WndProcDelegate lpPrevWndFunc, IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr CallWindowProc(IntPtr wndProc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
}
}
"@
Add-Type -TypeDefinition $registerDLL
$dllPath = "C:\Full\Path\To.dll"
[System.IntPtr]$module = [RegisterDLL.Kernel32]::LoadLibrary($dllPath)
Write-Verbose "module = $module"
if(Test-Path -LiteralPath $dllPath)
{
Write-Verbose "Found $dllPath, attempting to register"
try
{
[System.IntPtr]$address = [RegisterDLL.Kernel32]::GetProcAddress($module, "DllRegisterServer")
write-Verbose "address = $address"
$finalResult = [RegisterDLL.User32]::CallWindowProc($address, $(Get-Process -Name powershell).Handle, 0, 0, 0)
write-Verbose "finalResult = $finalResult"
}
finally
{
[RegisterDLL.Kernel32]::FreeLibrary($module) | Out-Null
}
}
else
{
Write-Verbose "Failed to find $dllPath"
}
@haloweenhamster
Copy link

PS H:> C:\Users\w\Downloads\RegisterDLL.ps1
Get-Process : Cannot find a process with the name "powershell". Verify the process name and call the cmdlet
again.
At C:\Users\w\Downloads\RegisterDLL.ps1:40 char:73

  • ... r32]::CallWindowProc($address, $(Get-Process -Name powershell).Handle ...
  •                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (powershell:String) [Get-Process], ProcessCommandException
    • FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

Cannot convert argument "hWnd", with value: "", for "CallWindowProc" to type "System.IntPtr": "Cannot
convert null to type "System.IntPtr"."
At C:\Users\w\Downloads\RegisterDLL.ps1:40 char:73

  • ... r32]::CallWindowProc($address, $(Get-Process -Name powershell).Handle ...
  •                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [], MethodException
    • FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment