I'm trying to run a app on memory but I'm having error at Invoke. What am I doing wrong?
I'm trying to do this but in vb.net
Code C#
// read the bytes from the application exe file
FileStream fs = new FileStream(filePath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
// load the bytes into Assembly
Assembly a = Assembly.Load(bin);
// search for the Entry Point
MethodInfo method = a.EntryPoint;
if (method != null)
{
// create an istance of the Startup form Main method
object o = a.CreateInstance(method.Name);
// invoke the application starting point
method.Invoke(o, null);
}
in vb:
Dim instance As FileStream = File.Open("teste.exe", FileMode.Open)
Dim br As New BinaryReader(instance)
Dim bin As Byte() = br.ReadBytes(Convert.ToInt32(instance.Length))
instance.Close()
br.Close()
Dim a As Assembly = Assembly.Load(bin)
Dim metodo As MethodInfo = a.EntryPoint
If (IsDBNull(metodo) = False) Then
'create an istance of the Startup form Main method
Dim o As Object = a.CreateInstance(metodo.Name)
'invoke the application starting point
metodo.Invoke(o, Nothing)
Else
MessageBox.Show("Nao encontrado")
End If
UPDATE:
I found the answer. I created the "test.exe" like a ConsoleAplication, than in the module I code
Imports System.Windows.Forms
Module Module1
Sub Main()
Dim Form1 As New Form1
Form1.Show()
Do Until Form1.Visible = False
Application.DoEvents()
Loop
End Sub
End Module
Then I changed from ConsoleApplication to Windowsform And create my Form1. And this
metodo.Invoke(o, Nothing)
to this:
metodo.Invoke(Nothing, New Object() {})
Thank you guys for the support!
Assuming the c# code works you mistranslated the null check.
The equivalent of 'if (method != null)' in vb.net is
The
Main
method expects that you pass it theargs
parameter. Your current call is not passing any parameters, so I'm expecting that you are getting the following error if it ever reaches that line:To fix it, just pass a one-element object array as the second parameter of the
method.Invoke
. Also, because theMain
method is a static method, you don't need to do aCreateInstance
before invoking the method.So all you need is this:
If, for some reason, you actually need pass values to the main's
args
parameter, you can do it like this: