2

Hello i have this code:

# Note: This must be an executable or DLL compiled for .NET
$Path = "C:\Users\sadettin\desktop\tok.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')))

That code works for .NET framework console projects. Somehow it doesn't work for .NET framework Form applications, And i added this:

$entryPointMethod.Invoke($null, $null)

For now, it works both console and form application versions.

But it doesn't loads when i try put another .net programs to $path i think its because of $entryPointMethod. so we need to modify it to make it worked for all programs.

Is there a way to make this system universal??? and How todo? thanks

NoobCoding
  • 43
  • 4

1 Answers1

1

The entry point does not need to be in a Program class (and technically does not even need to be called Main, although that is enforced in C#).

The correct way to get the entry point function is to use the assembly's EntryPoint property

# This example passes two arguments, 'foo' and 'bar'
$assembly.EntryPoint.Invoke($null, @('foo', 'bar'));

This only works in an executable .exe. If it's a library .dll then it won't have an entry point.

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • Hello, firstly thank you for your answer but is there more powerful way to make this code for works in the widest possible field? I mean can we execute c++ or other exe files? and all of c# versions (not only .NET) – NoobCoding Apr 26 '23 at 23:16
  • C++/CLI will work, native C++ will not. All C# applications use .NET. Non .NET applications are not designed to be loaded into the address space of another application, so I don't think this is possible (or at least not supported without using dangerous hacks, see https://stackoverflow.com/questions/305203/createprocess-from-memory-buffer) Probably just best to write the file and execute it. – Charlieface Apr 26 '23 at 23:19
  • Hello i added "$assembly.EntryPoint.Invoke($null, @('foo', 'bar'));" to my code but it doesn't work :( – NoobCoding Apr 27 '23 at 20:48
  • What is the result of `$assembly.EntryPoint` does it actually have an entrypoint? – Charlieface Apr 27 '23 at 22:04