从字节数组加载 .NET 程序集

从字节数组加载 .NET 程序集

问题描述:

当我尝试在内存中加载程序时出现此错误

I'm getting this error when I try to load a program in memory

错误:

GAC    Version        Location
---    -------        --------
False  v2.0.50727

这里是我的代码:

$Path = "D:calc.exe"
$bytes = [System.IO.File]::ReadAllBytes($Path)

$string = [System.Convert]::ToBase64String($bytes)

$bytees = [System.Convert]::FromBase64String($string)
[System.Reflection.Assembly]::Load($bytees)

正如 Maximilian Burszley 指出的那样,您所看到的不是错误.

As Maximilian Burszley points out, what you're seeing is not an error.

实际上,它表明您的程序集已成功从您的字节数组中加载:

In fact, it indicates that your assembly was successfully loaded from your byte array:

System.Reflection.Assembly.Load 方法返回一个 System.Reflection.Assembly 表示加载的程序集的实例,并且由于您没有将该返回值分配给变量,PowerShell 会隐式地将对象的友好表示打印到控制台,这就是你所看到的;如果你附加 |Format-List 到您的 [System.Reflection.Assembly]::Load($bytees) 调用,您将看到有关新加载的程序集的更多详细信息.

The System.Reflection.Assembly.Load method returns a System.Reflection.Assembly instance representing the loaded assembly, and since you're not assigning that return value to a variable, PowerShell implicitly prints a friendly representation of the object to the console, and this is what you're seeing; if you append | Format-List to your [System.Reflection.Assembly]::Load($bytees) call, you'll see more detailed information about the newly loaded assembly.

程序集中定义的任何公共类型现在都应该在您的 PowerShell 会话中可用;但是,鉴于您将 *.exe 文件指定为源,也许您想像执行原始可执行文件一样执行程序集,可能带有命令行参数.

Any public types defined in the assembly should now be available in your PowerShell session; however, given that you're indicating an *.exe files as the source, perhaps you want to execute the assembly as you would the original executable, possibly with command-line arguments.

提供一个完整的例子:

# Note: This must be an executable or DLL compiled for .NET
$Path = "D:calc.exe"

# Get Base64-encoded representation of the bytes that make up the assembly.
$bytes = [System.IO.File]::ReadAllBytes($Path)
$string = [System.Convert]::ToBase64String($bytes)

# ...

# Convert the Base64-encoded string back to a byte array, load
# the byte array as an assembly, and save the object representing the
# loaded assembly for later use.
$bytes = [System.Convert]::FromBase64String($string)
$assembly = [System.Reflection.Assembly]::Load($bytes)


# Get the static method that is the executable's entry point.
# Note: 
#   * Assumes 'Program' as the class name, 
#     and a static method named 'Main' as the entry point.
#   * Should there be several classes by that name, the *first* 
#     - public or non-public - type returned is used.
#     If you know the desired type's namespace, use, e.g.
#     $assembly.GetType('MyNameSpace.Program').GetMethod(...)
$entryPointMethod = 
 $assembly.GetTypes().Where({ $_.Name -eq 'Program' }, 'First').
   GetMethod('Main', [Reflection.BindingFlags] 'Static, Public, NonPublic')

# Now you can call the entry point.
# This example passes two arguments, 'foo' and 'bar'
$entryPointMethod.Invoke($null, (, [string[]] ('foo', 'bar')))