473,394 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Start program directly from memory

This is my code:
FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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

Nov 17 '05 #1
8 1599
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_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"wavemill" <wa******@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
This is my code:
FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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

Nov 17 '05 #2
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" <wa******@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
This is my code:
FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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

Nov 17 '05 #3
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" wrote:
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" <wa******@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
This is my code:
FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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


Nov 17 '05 #4
I think the answer Nicholas gave pretty much descibes the specifics of it.

Pete

"wavemill" <wa******@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
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" wrote:
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" <wa******@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
> This is my code:
> FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
> documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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
>


Nov 17 '05 #5
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" wrote:
I think the answer Nicholas gave pretty much descibes the specifics of it.

Pete

"wavemill" <wa******@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
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" wrote:
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" <wa******@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
> This is my code:
> FileStream fs = new FileStream(@"C:\Documents and Settings\Berdoues\Mes
> documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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
>


Nov 17 '05 #6
Where are you getting the exception? What is the message in the exception?

Pete

"wavemill" <wa******@discussions.microsoft.com> wrote in message
news:FD**********************************@microsof t.com...
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" wrote:
I think the answer Nicholas gave pretty much descibes the specifics of
it.

Pete

"wavemill" <wa******@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
> 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" wrote:
>
>> 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" <wa******@discussions.microsoft.com> wrote in message
>> news:E8**********************************@microsof t.com...
>> > This is my code:
>> > FileStream fs = new FileStream(@"C:\Documents and
>> > Settings\Berdoues\Mes
>> > documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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
>> >
>>
>>
>>


Nov 17 '05 #7
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" wrote:
Where are you getting the exception? What is the message in the exception?

Pete

"wavemill" <wa******@discussions.microsoft.com> wrote in message
news:FD**********************************@microsof t.com...
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" wrote:
I think the answer Nicholas gave pretty much descibes the specifics of
it.

Pete

"wavemill" <wa******@discussions.microsoft.com> wrote in message
news:7D**********************************@microsof t.com...
> 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" wrote:
>
>> 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" <wa******@discussions.microsoft.com> wrote in message
>> news:E8**********************************@microsof t.com...
>> > This is my code:
>> > FileStream fs = new FileStream(@"C:\Documents and
>> > Settings\Berdoues\Mes
>> > documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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
>> >
>>
>>
>>


Nov 17 '05 #8
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" <wa******@discussions.microsoft.com> wrote in message
news:3F**********************************@microsof t.com...
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" wrote:
Where are you getting the exception? What is the message in the
exception?

Pete

"wavemill" <wa******@discussions.microsoft.com> wrote in message
news:FD**********************************@microsof t.com...
> 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" wrote:
>
>> I think the answer Nicholas gave pretty much descibes the specifics of
>> it.
>>
>> Pete
>>
>> "wavemill" <wa******@discussions.microsoft.com> wrote in message
>> news:7D**********************************@microsof t.com...
>> > 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" wrote:
>> >
>> >> 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" <wa******@discussions.microsoft.com> wrote in message
>> >> news:E8**********************************@microsof t.com...
>> >> > This is my code:
>> >> > FileStream fs = new FileStream(@"C:\Documents and
>> >> > Settings\Berdoues\Mes
>> >> > documents\VisualStudioProjects\Application_Test_Wi ndowsForm\LoadAssembly\CaptureControl.exe",FileMod e.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
>> >> >
>> >>
>> >>
>> >>
>>
>>
>>


Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Nimit | last post by:
Hi, I wasn't sure which forum this post belongs to, so I've posted it to a couple forums that I thought may be appropriate. In giving me advice, please consider me a beginner. Below is a synopsis...
75
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or...
4
by: Casual Reader | last post by:
Hi, I have a C# program that in principle should only use a constant amount of memory to execute (with periodic garbage collection) and in fact does so when executed in Mono under either Windows...
5
by: wavemill | last post by:
Hello! I would like load teh data(it's my program decrypted) in memory and run it directly from my memory. If you have an idea? best regards, wavemill
2
by: holysmokes99 | last post by:
I am developing a component in .Net 1.1, and want to debug it using the "start external program" of the debugger in the IDE. The program I want to start references both 1.1 and 2.0 components. The...
63
by: biyubi | last post by:
Hi, a year ago I won the 2005 Best Game categoryof the International Obfuscated C Code Contestwith a chess program. http://www.ioccc.org/whowon2005.html...
4
by: carson | last post by:
I have written two windows services: - service A does some crunching of local data files and uploads them to a central processing computer via http. - service B monitors a manifest file on a...
1
by: Jim Langston | last post by:
Windows. Situation: Using a Python program called OpenRPG. I have a program that displays form data (a character sheet) in C++. I am able in the C++ program to build a string and copy it into the...
4
by: =?Utf-8?B?U3ZlbiBXLg==?= | last post by:
Hi Newsgroup, I am developing a C# Windows Forms application that launches processes within a background worker.. The processes seem to have a memory limit of about 278mb. Some proccesses...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.