VB.NET资源exe在内存中带参数

时间:2017-07-07 17:32:49

标签: vb.net memory resources arguments

好吧所以我对内存执行中的任何事情都很陌生。我通常只是将嵌入式资源中的字节写入硬盘驱动器上的文件,但是对于我正在为工作项目编写的程序,它无法将exe写入磁盘。

所以我从Load a .NET assembly from the application's resources and run It from memory, but without terminating the main/host application获取了代码 我修改了一些代码,我认为可以用一个参数运行它,它除了崩溃之外什么也没做,除了windows错误报告之外没有真正列出任何崩溃细节。 这是代码:

 Dim ass As Assembly = Assembly.Load(My.Resources.bbinst)
            Dim method As MethodInfo = ass.EntryPoint
            Dim parametersArray As Object() = New Object() {"/q /SERIAL=xxx-xxx"}

            If (method IsNot Nothing) Then
                Dim instance As Object = ass.CreateInstance(method.Name)
                method.Invoke(instance, parametersArray)
                If (instance IsNot Nothing) AndAlso (instance.GetType().GetInterfaces.Contains(GetType(IDisposable))) Then
                    DirectCast(instance, IDisposable).Dispose()
                End If
                instance = Nothing
                method = Nothing
                ass = Nothing

            Else
                Throw New EntryPointNotFoundException("Entrypoint not found in the specified resource. Are you sure it is a .NET assembly?")
            End If

bbinst是作为bbinst.exe嵌入的exe名称 parametersArray是我想要运行的参数,我从C#样本转换,我发现其他地方。

有人可以帮助我解决为什么程序崩溃和错误报告后弹出,我不好调试。我也尝试在没有参数的情况下运行它,并且它也以同样的方式崩溃。

任何帮助都很棒,有时我在工作中有这些随机项目我不知道为什么他们会给我lol

2 个答案:

答案 0 :(得分:0)

当您尝试加载错误位的程序集时,或者当您尝试加载当前运行时无法读取,运行或编译的程序集时,将引发BadImageFormatException

关于位数: 32位(x86)应用程序无法加载64位代码,而64位(x64)应用程序无法加载32位代码。就这么简单。

至于无法读取,运行或编译程序集:如果您尝试加载的程序集是用完全不同的语言编写的(更准确地说:没有.NET的语言)支持)然后它也会导致抛出上述异常。

执行嵌入式应用程序的方法仅适用于使用.NET语言编写的程序集(C#,VB.NET,F#,C ++ / CLR等)。这是因为它不仅运行应用程序,还尝试将其加载为链接到Assembly和/或AppDomain类的.NET应用程序,以便您可以对其进行控制并调用其中的方法。

您的问题:我敢打赌您的问题是由于您的嵌入式应用程序与主机的位数不同造成的。确保将两个应用程序编译为x86x64AnyCPU

但是,如果事实证明您的嵌入式程序甚至不是用.NET语言编写的,那么这就是您的问题的原因。你的代码只能 运行.NET程序/ DLL。

答案 1 :(得分:0)

您可以通过在程序集中加载 EntryPoint 来调用它:

 Public Shared Sub RunSearch(ByVal pPath As String, ByVal pText As String)
    'Get assembly
    Dim assembly As Reflection.Assembly = Reflection.Assembly.GetExecutingAssembly()

    'Get the resource stream
    Dim resourceStream As Stream = assembly.GetManifestResourceStream("path.executable name")

    If resourceStream Is Nothing Then
         MsgBox("Unexisting Path","Error")
    End If

    'Read the raw bytes of the resource
    Dim resourcesBuffer(CInt(resourceStream.Length) - 1) As Byte

    resourceStream.Read(resourcesBuffer, 0, resourcesBuffer.Length)
    resourceStream.Close()

    'Load the assembly
    Dim exeAssembly As Reflection.Assembly = Reflection.Assembly.Load(resourcesBuffer)
    Dim args() As String = {pPath, pText}
    Dim parameters = New Object() {args}
    Try
        exeAssembly.EntryPoint.Invoke(Nothing, parameters)
    Catch
        MsgBox("Error during assembly executing","Error")
    End Try
End Sub