vb.net在内存中运行exe

vb.net在内存中运行exe,vb.net,memory,Vb.net,Memory,我试图在内存上运行应用程序,但调用时出错。我做错了什么 我试图这样做,但在vb.net中 代码C# 在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.Clo

我试图在内存上运行应用程序,但调用时出错。我做错了什么

我试图这样做,但在vb.net中

代码C#

在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
更新:

我找到了答案。我像控制台应用程序一样创建了“test.exe”,而不是在模块I代码中创建

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
然后我从ConsoleApplication更改为Windowsform并创建了我的Form1。还有这个

metodo.Invoke(o, Nothing)
为此:

metodo.Invoke(Nothing, New Object() {})

谢谢你们的支持

Main方法希望您将
args
参数传递给它。您当前的呼叫没有传递任何参数,因此我希望您在到达该行时会收到以下错误:

System.Reflection.TargetParameterCountException:参数计数不匹配

要修复它,只需传递一个单元素对象数组作为
方法的第二个参数。Invoke
。另外,由于
Main
方法是一个静态方法,因此在调用该方法之前不需要执行
CreateInstance

因此,您所需要的是:

metodo.Invoke(Nothing, New Object() {Nothing})
如果出于某种原因,您实际上需要将值传递给main的
args
参数,您可以这样做:

metodo.Invoke(Nothing, New Object() {New String() {"param1", "param2"}})
假设c#代码起作用,你就误译了空校验

vb.net中“if(method!=null)”的等效项为

If method IsNot Nothing Then
    Dim o = a.CreateInstance(method.Name)
    method.Invoke(o, Nothing)
End If

Main
不是静态的吗?在这种情况下,您不需要
o
的实例。还有,你得到了什么错误?异常方法是什么?我添加了一些代码(c#),这是我想在vbException中执行的操作:“必须在应用程序创建中的第一个IWin32Window对象之前调用SetCompatibleTextRenderingDefault。”这样会显示错误System.Reflection.TargetParameterCountException:参数计数不匹配。
If method IsNot Nothing Then
    Dim o = a.CreateInstance(method.Name)
    method.Invoke(o, Nothing)
End If