Skip to content

Instantly share code, notes, and snippets.

@Luiz-Monad
Last active August 18, 2019 02:14
Show Gist options
  • Save Luiz-Monad/db6f45d95c4496ef807e97bae82a436d to your computer and use it in GitHub Desktop.
Save Luiz-Monad/db6f45d95c4496ef807e97bae82a436d to your computer and use it in GitHub Desktop.
inject resources into an assembly
$strings = @( "file1.bin", "file2.bin", "file3.bin" )
# Compile the code
function escape() { process { "@`"$_`"" } }
$source = @"
using System.Resources;
public class MakeResource
{
public static void Main(string[] args)
{
var strt = new [] { $(($strings | escape) -join ", ") };
// Define a resource file.
using (ResXResourceWriter resx = new ResXResourceWriter(args[0]))
{
resx.AddResource(`"StringTable`", strt);
}
}
}
"@
$assemblies = @("System", "System.Windows.Forms")
Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source -Language CSharp
# Create the ResX
[MakeResource]::Main((Join-Path (Get-Item .) "resources.resx"))
# Compile the ResX
# cinst netfx-4.7.2-devpack to get it, or install the Visual Studio
$resgen = 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools\resgen.exe'
& $resgen /compile 'resources.resx'
# Create the test program, this can be a normal PE exe produced by Visual Studio
# example based on https://docs.microsoft.com/en-us/dotnet/api/system.resources.resourcemanager?view=netframework-4.8
$stub = @"
using System;
using System.Resources;
public class TestStub
{
public static void Main(string[] args)
{
var rm = new ResourceManager("resources", typeof(TestStub).Assembly);
var strs = (string[])rm.GetObject("StringTable");
foreach (var str in strs) {
Console.WriteLine("{0}.\n", str);
}
}
}
"@
$stub | Out-File "stub.cs"
$csharp = 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe'
& $csharp "stub.cs"
#Final step link everything together.
$al = 'C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7.2 Tools\al.exe'
& $al -embed:resources.resources -out:resource.dll
# al can only create satelitte assemblies
#wget https://download.microsoft.com/download/1/3/4/1347C99E-9DFB-4252-8F6D-A3129A069F79/ILMerge.msi -out ilmerge.msi
#cinst ilmerge
#please note that you can reuse the exe made in the previous step only regenerating the resource.dll and calling this ilmerge.
ilmerge stub.exe resource.dll -out:stub-final.exe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment