Why does System.Reflection.TargetInvocationException occur?

Maxwell175

Member
Joined
Feb 11, 2012
Messages
12
Location
Pennsylvania, USA
Programming Experience
1-3
I am making a program that is sort of a frontend to all my other apps. :smash: I put the EXEs of those apps into the resources of my frontend. The problem occurs when I try to run the EXEs from memory.
I keep getting System.Reflection.TargetInvocationException on the line: :kaioken:

entryPoint.Invoke(RuntimeHelpers.GetObjectValue(objectValue), New Object() {New String() {"1"}})



Public Sub RunExe(ByVal ResourcesBuffer As Byte())
        Dim Resource As String = String.Empty
        Dim assembly As Assembly = assembly.Load(ResourcesBuffer)
        Dim entryPoint As MethodInfo = [assembly].EntryPoint
        Dim objectValue As Object = RuntimeHelpers.GetObjectValue([assembly].CreateInstance(entryPoint.Name))
        entryPoint.Invoke(RuntimeHelpers.GetObjectValue(objectValue), New Object() {New String() {"1"}})
End Sub




I got the above code from: [RESOLVED] how can i run exe file from embedded files(Resource)? - VBForums

P.S. To run this code you need to import the following:

Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Runtime.CompilerServices
 
Last edited:
TargetInvocationException Class (System.Reflection)
TargetInvocationException said:
The exception that is thrown by methods invoked through reflection.

When created, the TargetInvocationException is passed a reference to the exception thrown by the method invoked through reflection. The InnerException property holds the underlying exception.
When you know what the problem actually is you may be able to resolve it.
 
I attached all the project files for the frontend and all of the other projects that make up the backend.

Please tell me what is the problem?
 

Attachments

  • Projects.zip
    132.4 KB · Views: 13
Last edited by a moderator:
Look at the InnerException.
 
By the way, thanks for the replies, John.

ok, here are the message boxes I get when I run the following code:

Public Sub RunExe(ByVal ResourcesBuffer As Byte())
        Dim Resource As String = String.Empty
        Dim assembly As Assembly = assembly.Load(ResourcesBuffer)
        Dim entryPoint As MethodInfo = [assembly].EntryPoint
        Dim objectValue As Object = RuntimeHelpers.GetObjectValue([assembly].CreateInstance(entryPoint.Name))
        Try

            entryPoint.Invoke(RuntimeHelpers.GetObjectValue(objectValue), New Object() {New String() {"1"}})
        Catch e As Exception
            MsgBox("In Main catch block. Caught: " & e.Message)
            MsgBox("Inner Exception is " & e.InnerException.ToString)
        End Try
    End Sub



2-11-2012 4-40-48 PM.jpg2-11-2012 4-41-13 PM.jpg
 
If you disable application framework (in project properties) for the compiled resource applications you can use this code:
        Dim asm As Assembly = Assembly.Load(My.Resources.Percents)
        Using frm As Form = Activator.CreateInstance(asm.EntryPoint.DeclaringType)
            frm.ShowDialog()
        End Using

With application framework enabled the EntryPoint type is MyApplication, so a similar approach would be to create an instance of this, get and invoke its OnCreateMainForm method, then get MainForm property and its value - which is the main form instance to be shown.
 
If you disable application framework (in project properties) for the compiled resource applications you can use this code:
        Dim asm As Assembly = Assembly.Load(My.Resources.Percents)
        Using frm As Form = Activator.CreateInstance(asm.EntryPoint.DeclaringType)
            frm.ShowDialog()
        End Using

With application framework enabled the EntryPoint type is MyApplication, so a similar approach would be to create an instance of this, get and invoke its OnCreateMainForm method, then get MainForm property and its value - which is the main form instance to be shown.

When I try to run that code I get an error on the line: :(

Using frm As Form = Activator.CreateInstance(asm.EntryPoint.DeclaringType)


Error:

Unable to cast object of type 'Percents.My.MyApplication' to type 'System.Windows.Forms.Form'.
 
When I try to run that code I get an error on the line: :(

Using frm As Form = Activator.CreateInstance(asm.EntryPoint.DeclaringType)


Error:

Unable to cast object of type 'Percents.My.MyApplication' to type 'System.Windows.Forms.Form'.

Never mind everything works!! I just forgot to disable application framework.


Also, just a question, what does the Application Framework option do?
 
With application framework enabled the EntryPoint type is MyApplication, so a similar approach would be to create an instance of this, get and invoke its OnCreateMainForm method, then get MainForm property and its value - which is the main form instance to be shown.
Here's the code for that too:
Dim asm As Assembly = Assembly.Load(My.Resources.Percents)
Dim app = Activator.CreateInstance(asm.EntryPoint.DeclaringType)
Dim flags = BindingFlags.Instance Or BindingFlags.NonPublic
entry.GetMethod("OnCreateMainForm", flags).Invoke(app, Nothing)
Using frm As Form = entry.GetProperty("MainForm", flags).GetValue(app, Nothing)
    frm.ShowDialog()
End Using

or combined to handle both those cases:
    Public Sub ShowExeForm(ByVal ResourcesBuffer As Byte())
        Dim asm As Assembly = Assembly.Load(ResourcesBuffer)
        Dim entry = asm.EntryPoint.DeclaringType
        Dim frm As Form
        If entry.Name = "MyApplication" Then
            Dim app = Activator.CreateInstance(entry)
            Dim flags = BindingFlags.Instance Or BindingFlags.NonPublic
            entry.GetMethod("OnCreateMainForm", flags).Invoke(app, Nothing)
            frm = entry.GetProperty("MainForm", flags).GetValue(app, Nothing)
        Else
            frm = Activator.CreateInstance(entry)
        End If
        frm.ShowDialog()
        frm.Dispose()
    End Sub
 
Back
Top