Start program directly from memory

G

Guest

This is my code:
FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_WindowsForm\LoadAssembly\CaptureControl.exe",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byteload = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

Assembly asm = Assembly.Load(byteload);
object o = asm.CreateInstance("CaptureControl");
MethodInfo mi = asm.EntryPoint;
if(mi != null)
{
mi.Invoke(o,null);
}

It doesn'work.
"capturecontrol" is the name of my assembly and the namespce of my class
I don't understand the parameter of mi.Invoke(?,?);
Thank you for your help and patience!

wavemill
 
N

Nicholas Paldino [.NET/C# MVP]

Wavemill,

Here is what the code should look like:

// Bhe bytes in the assembly.
byte[] byteload = null;

// Open the file stream and read the bytes.
using (FileStream fs = new FileStream(@"C:\Documents and
Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_WindowsForm\LoadAssembly\CaptureControl.exe",FileMode.Open))
{
// Create the byte array with the length.
byteload = new byte[fs.Length];

// Read the bytes.
fs.Read(byteload, 0, byteload.Length);
}

// Load the assembly.
Assembly asm = Assembly.Load(byteload);

// You don't need this, the entry point is static, so you don't need to
create an object.
// object o = asm.CreateInstance("Fully qualified type name");

// Get the method info.
MethodInfo mi = asm.EntryPoint;
if(mi != null)
{
// Pass null for the first parameter, since it is static.
// The second parameter, that's the args[] string array with the comand
line parameters.
mi.Invoke(null,null);
}

Now, you really shouldn't be doing this. Rather, you should separate
the logic from that executable into a class library and reference that
library in your project.

Hope this helps.
 
P

Pete Davis

First of all, you'll want to use the overload of Invoke that passes the
BindingFlags (I believe). I'm fairly certain you need to specify that the
method is static (because "Main()", the entry point, will be static).

Second of all, I doubt you need to create an instance of it since it's a
static method and therefore not part of an instance. I believe simply
passing null as the first parameter will be sufficient.

I may be wrong. You may be able to simply call my.Invoke(null, params)

params is an array of objects that holds the parameters to the method. If
your "Main()" method takes arguments (it may, check), you'll need to supply
those.

Pete
 
G

Guest

Hello!

But how can i do to load an assembly in memory and launch it directly from
memory?
Is it possible?

best regards,

wavemill

Pete Davis said:
First of all, you'll want to use the overload of Invoke that passes the
BindingFlags (I believe). I'm fairly certain you need to specify that the
method is static (because "Main()", the entry point, will be static).

Second of all, I doubt you need to create an instance of it since it's a
static method and therefore not part of an instance. I believe simply
passing null as the first parameter will be sufficient.

I may be wrong. You may be able to simply call my.Invoke(null, params)

params is an array of objects that holds the parameters to the method. If
your "Main()" method takes arguments (it may, check), you'll need to supply
those.

Pete

wavemill said:
This is my code:
FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_WindowsForm\LoadAssembly\CaptureControl.exe",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byteload = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

Assembly asm = Assembly.Load(byteload);
object o = asm.CreateInstance("CaptureControl");
MethodInfo mi = asm.EntryPoint;
if(mi != null)
{
mi.Invoke(o,null);
}

It doesn'work.
"capturecontrol" is the name of my assembly and the namespce of my class
I don't understand the parameter of mi.Invoke(?,?);
Thank you for your help and patience!

wavemill
 
P

Pete Davis

I think the answer Nicholas gave pretty much descibes the specifics of it.

Pete

wavemill said:
Hello!

But how can i do to load an assembly in memory and launch it directly from
memory?
Is it possible?

best regards,

wavemill

Pete Davis said:
First of all, you'll want to use the overload of Invoke that passes the
BindingFlags (I believe). I'm fairly certain you need to specify that the
method is static (because "Main()", the entry point, will be static).

Second of all, I doubt you need to create an instance of it since it's a
static method and therefore not part of an instance. I believe simply
passing null as the first parameter will be sufficient.

I may be wrong. You may be able to simply call my.Invoke(null, params)

params is an array of objects that holds the parameters to the method. If
your "Main()" method takes arguments (it may, check), you'll need to
supply
those.

Pete

wavemill said:
This is my code:
FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_WindowsForm\LoadAssembly\CaptureControl.exe",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byteload = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

Assembly asm = Assembly.Load(byteload);
object o = asm.CreateInstance("CaptureControl");
MethodInfo mi = asm.EntryPoint;
if(mi != null)
{
mi.Invoke(o,null);
}

It doesn'work.
"capturecontrol" is the name of my assembly and the namespce of my
class
I don't understand the parameter of mi.Invoke(?,?);
Thank you for your help and patience!

wavemill
 
G

Guest

Thank you pete for your answer.

But i have try the Nicholas code and i have an error:
System.Reflexion.TargetInvocationExeption

I don't understand why is not run.

Sorry for my bad english

Tank you for your help!

wavemill

Pete Davis said:
I think the answer Nicholas gave pretty much descibes the specifics of it.

Pete

wavemill said:
Hello!

But how can i do to load an assembly in memory and launch it directly from
memory?
Is it possible?

best regards,

wavemill

Pete Davis said:
First of all, you'll want to use the overload of Invoke that passes the
BindingFlags (I believe). I'm fairly certain you need to specify that the
method is static (because "Main()", the entry point, will be static).

Second of all, I doubt you need to create an instance of it since it's a
static method and therefore not part of an instance. I believe simply
passing null as the first parameter will be sufficient.

I may be wrong. You may be able to simply call my.Invoke(null, params)

params is an array of objects that holds the parameters to the method. If
your "Main()" method takes arguments (it may, check), you'll need to
supply
those.

Pete

This is my code:
FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_WindowsForm\LoadAssembly\CaptureControl.exe",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byteload = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

Assembly asm = Assembly.Load(byteload);
object o = asm.CreateInstance("CaptureControl");
MethodInfo mi = asm.EntryPoint;
if(mi != null)
{
mi.Invoke(o,null);
}

It doesn'work.
"capturecontrol" is the name of my assembly and the namespce of my
class
I don't understand the parameter of mi.Invoke(?,?);
Thank you for your help and patience!

wavemill
 
P

Pete Davis

Where are you getting the exception? What is the message in the exception?

Pete

wavemill said:
Thank you pete for your answer.

But i have try the Nicholas code and i have an error:
System.Reflexion.TargetInvocationExeption

I don't understand why is not run.

Sorry for my bad english

Tank you for your help!

wavemill

Pete Davis said:
I think the answer Nicholas gave pretty much descibes the specifics of
it.

Pete

wavemill said:
Hello!

But how can i do to load an assembly in memory and launch it directly
from
memory?
Is it possible?

best regards,

wavemill

:

First of all, you'll want to use the overload of Invoke that passes
the
BindingFlags (I believe). I'm fairly certain you need to specify that
the
method is static (because "Main()", the entry point, will be static).

Second of all, I doubt you need to create an instance of it since it's
a
static method and therefore not part of an instance. I believe simply
passing null as the first parameter will be sufficient.

I may be wrong. You may be able to simply call my.Invoke(null, params)

params is an array of objects that holds the parameters to the method.
If
your "Main()" method takes arguments (it may, check), you'll need to
supply
those.

Pete

This is my code:
FileStream fs = new FileStream(@"C:\Documents and
Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_WindowsForm\LoadAssembly\CaptureControl.exe",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byteload = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

Assembly asm = Assembly.Load(byteload);
object o = asm.CreateInstance("CaptureControl");
MethodInfo mi = asm.EntryPoint;
if(mi != null)
{
mi.Invoke(o,null);
}

It doesn'work.
"capturecontrol" is the name of my assembly and the namespce of my
class
I don't understand the parameter of mi.Invoke(?,?);
Thank you for your help and patience!

wavemill
 
G

Guest

Hello!

My exception :
System.Reflexion.Targetinvocationexception in mscorlib.dll

I have translate it because i have a french version:
an exception has ete raised by the target of a call

You can't start a second buckle message in an alone thread.Use
application.RunDialog or Form.ShowDialog.

Thank you!

Wavemill

Pete Davis said:
Where are you getting the exception? What is the message in the exception?

Pete

wavemill said:
Thank you pete for your answer.

But i have try the Nicholas code and i have an error:
System.Reflexion.TargetInvocationExeption

I don't understand why is not run.

Sorry for my bad english

Tank you for your help!

wavemill

Pete Davis said:
I think the answer Nicholas gave pretty much descibes the specifics of
it.

Pete

Hello!

But how can i do to load an assembly in memory and launch it directly
from
memory?
Is it possible?

best regards,

wavemill

:

First of all, you'll want to use the overload of Invoke that passes
the
BindingFlags (I believe). I'm fairly certain you need to specify that
the
method is static (because "Main()", the entry point, will be static).

Second of all, I doubt you need to create an instance of it since it's
a
static method and therefore not part of an instance. I believe simply
passing null as the first parameter will be sufficient.

I may be wrong. You may be able to simply call my.Invoke(null, params)

params is an array of objects that holds the parameters to the method.
If
your "Main()" method takes arguments (it may, check), you'll need to
supply
those.

Pete

This is my code:
FileStream fs = new FileStream(@"C:\Documents and
Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_WindowsForm\LoadAssembly\CaptureControl.exe",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byteload = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

Assembly asm = Assembly.Load(byteload);
object o = asm.CreateInstance("CaptureControl");
MethodInfo mi = asm.EntryPoint;
if(mi != null)
{
mi.Invoke(o,null);
}

It doesn'work.
"capturecontrol" is the name of my assembly and the namespce of my
class
I don't understand the parameter of mi.Invoke(?,?);
Thank you for your help and patience!

wavemill
 
P

Pete Davis

It sounds like the problem is the second application message. I've never
done this before, so I'm not aware of what issues may arise. Maybe you ought
to look at starting it up in a separate AppDomain. Take a look at the
AppDomain class and try that.

You can probably just use the AppDomain.ExecuteAssembly() method.

Pete

wavemill said:
Hello!

My exception :
System.Reflexion.Targetinvocationexception in mscorlib.dll

I have translate it because i have a french version:
an exception has ete raised by the target of a call

You can't start a second buckle message in an alone thread.Use
application.RunDialog or Form.ShowDialog.

Thank you!

Wavemill

Pete Davis said:
Where are you getting the exception? What is the message in the
exception?

Pete

wavemill said:
Thank you pete for your answer.

But i have try the Nicholas code and i have an error:
System.Reflexion.TargetInvocationExeption

I don't understand why is not run.

Sorry for my bad english

Tank you for your help!

wavemill

:

I think the answer Nicholas gave pretty much descibes the specifics of
it.

Pete

Hello!

But how can i do to load an assembly in memory and launch it
directly
from
memory?
Is it possible?

best regards,

wavemill

:

First of all, you'll want to use the overload of Invoke that passes
the
BindingFlags (I believe). I'm fairly certain you need to specify
that
the
method is static (because "Main()", the entry point, will be
static).

Second of all, I doubt you need to create an instance of it since
it's
a
static method and therefore not part of an instance. I believe
simply
passing null as the first parameter will be sufficient.

I may be wrong. You may be able to simply call my.Invoke(null,
params)

params is an array of objects that holds the parameters to the
method.
If
your "Main()" method takes arguments (it may, check), you'll need
to
supply
those.

Pete

This is my code:
FileStream fs = new FileStream(@"C:\Documents and
Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_WindowsForm\LoadAssembly\CaptureControl.exe",FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] byteload = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();

Assembly asm = Assembly.Load(byteload);
object o = asm.CreateInstance("CaptureControl");
MethodInfo mi = asm.EntryPoint;
if(mi != null)
{
mi.Invoke(o,null);
}

It doesn'work.
"capturecontrol" is the name of my assembly and the namespce of
my
class
I don't understand the parameter of mi.Invoke(?,?);
Thank you for your help and patience!

wavemill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top