vb.net在内存中运行一个exe [英] vb.net running a exe in memory

查看:280
本文介绍了vb.net在内存中运行一个exe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在内存上运行应用程序,但是在调用时出错.我在做什么错了?

I'm trying to run a app on memory but I'm having error at Invoke. What am I doing wrong?

我正在尝试这样做,但是在vb.net中

I'm trying to do this but in vb.net

代码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);
}

在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

更新:

我找到了答案.我创建了类似于ConsoleAplication的"test.exe",而不是在我编写的模块中

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

然后,我从ConsoleApplication更改为Windowsform并创建我的Form1.还有这个

Then I changed from ConsoleApplication to Windowsform And create my Form1. And this

metodo.Invoke(o, Nothing)

对此:

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

谢谢你们的支持!

推荐答案

Main方法期望您将其传递给args参数.您当前的调用未传递任何参数,因此,如果到达此行,我希望您收到以下错误:

The Main method expects that you pass it the args 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:

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

System.Reflection.TargetParameterCountException: Parameter count mismatch.

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

To fix it, just pass a one-element object array as the second parameter of the method.Invoke. Also, because the Main method is a static method, you don't need to do a CreateInstance before invoking the method.

所以您需要的是这个

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

如果由于某种原因您实际上需要将值传递给主体的args参数,则可以这样做:

If, for some reason, you actually need pass values to the main's args parameter, you can do it like this:

metodo.Invoke(Nothing, New Object() {New String() {"param1", "param2"}})

这篇关于vb.net在内存中运行一个exe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆