Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Module Module1

    Public Sub main()
        Dim a As System.Reflection.Assembly = System.Reflection.Assembly.Load(IO.File.ReadAllBytes("C:\myProgram.exe"))
        Dim method As System.Reflection.MethodInfo = a.EntryPoint
        Dim o As Object = a.CreateInstance(method.Name)
        method.Invoke(o, New Object() {New String() {"1"}})
    End Sub
End Module

VB
Public Class Form1

Sub start()
Module.main()
End Sub

The Problem is when i make a console application and start the main() sub in module1 it works but trying from Form1 it throws error like;
HTML
Exception has been thrown by the target of an invocation.
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

So far i think this code works only in a console application, how do customize this code to be ran in a form (windows application)
Any help appreciated, thanks
Posted
Updated 20-Sep-12 13:39pm
v3
Comments
[no name] 21-Sep-12 12:12pm    
So you think i can this run only on a Console Application not from a Windows Form, then the only 1 solution is to embed the Console App in my.resources and to executer it from a windows application am i right?
Sorry for the inconvienence

1 solution

It simply means you have an exception in the method method, and the exception information looks so strange and incomplete, because of the invocation mechanism. Identify the method where it is implemented (this is the entry point of the loaded assembly), put a break point on it (you should have its source code; and its project should be in the same solution), and run the application which you show. You should be able to execute the code responsible for this problem and locate it.

[EDIT]

By the way, this is a very strange way of loading of the assembly, once you have the file on the disc, not just bytes. You don't have to read the file. Instead, use the method System.Reflection.Assembly.LoadFrom:
http://msdn.microsoft.com/en-us/library/1009fa28.aspx[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900