Create an upgrading .EXE with an embedded DLL to overwrite existing deployed DLL

P

phobos7

I am trying to create a single .EXE that will upgrade a product by the
doing the following:

1) Detect the location of the current installation (DONE)
2) Upgrade the existing config file (DONE)
3) Overwrite a DLL in the program files folder with a new DLL.

For 3) I was trying to embed a DLL as a resource and then write it out
as a file over the existing DLL. I just can't work out how to do this.

How can I embed a DLL as a simple file resource that is built into the
..EXE at runtime?

How do I then extract the DLL and output it as a file at runtime?

I would also like the DLL to be the most recent version so I would like
to reference it and then it just be built in when I build the patch.

Thanks for your time.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Frankly I have never embed a DLL, I have done it for access, excel, etc ,
the code below will do it.
Tentatively what you have to do is add the dll to your project, mark it as
embedded resource ( IIRC it's marked as content by default) and that should
do it

static string ExtractResource( string resourceName)
{
//look for the resource name
foreach( string currentResource in
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
)
if ( currentResource.LastIndexOf( resourceName) != -1 )
{
string fqnTempFile = System.IO.Path.GetTempFileName();
string path = System.IO.Path.GetDirectoryName( fqnTempFile);
string rootName= System.IO.Path.GetFileNameWithoutExtension(
fqnTempFile);
string destFile = path + @"\" + rootName + "." +
System.IO.Path.GetExtension( currentResource);

System.IO.Stream fs =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
currentResource);

byte[] buff = new byte[ fs.Length ];
fs.Read( buff, 0, (int)fs.Length);
fs.Close();

System.IO.FileStream destStream = new System.IO.FileStream ( destFile,
FileMode.Create);
destStream.Write( buff, 0, buff.Length);
destStream.Close();

return destFile;
}

throw new Exception("Resource not found : " + resourceName);

}


cheers,
 
N

Nuno Magalhaes

In the sequence of this message, how can I extract, read, change and
save again into the .exe file? Because I want to do a timer that would
expire after some hours, and I want to keep track of that timer
everytime I run the program.

Thank you very much,
Nuno Magalhaes.
Hi,


Frankly I have never embed a DLL, I have done it for access, excel, etc ,
the code below will do it.
Tentatively what you have to do is add the dll to your project, mark it as
embedded resource ( IIRC it's marked as content by default) and that should
do it

static string ExtractResource( string resourceName)
{
//look for the resource name
foreach( string currentResource in
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
)
if ( currentResource.LastIndexOf( resourceName) != -1 )
{
string fqnTempFile = System.IO.Path.GetTempFileName();
string path = System.IO.Path.GetDirectoryName( fqnTempFile);
string rootName= System.IO.Path.GetFileNameWithoutExtension(
fqnTempFile);
string destFile = path + @"\" + rootName + "." +
System.IO.Path.GetExtension( currentResource);

System.IO.Stream fs =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
currentResource);

byte[] buff = new byte[ fs.Length ];
fs.Read( buff, 0, (int)fs.Length);
fs.Close();

System.IO.FileStream destStream = new System.IO.FileStream ( destFile,
FileMode.Create);
destStream.Write( buff, 0, buff.Length);
destStream.Close();

return destFile;
}

throw new Exception("Resource not found : " + resourceName);

}


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



phobos7 said:
I am trying to create a single .EXE that will upgrade a product by the
doing the following:

1) Detect the location of the current installation (DONE)
2) Upgrade the existing config file (DONE)
3) Overwrite a DLL in the program files folder with a new DLL.

For 3) I was trying to embed a DLL as a resource and then write it out
as a file over the existing DLL. I just can't work out how to do this.

How can I embed a DLL as a simple file resource that is built into the
.EXE at runtime?

How do I then extract the DLL and output it as a file at runtime?

I would also like the DLL to be the most recent version so I would like
to reference it and then it just be built in when I build the patch.

Thanks for your time.
 
N

Nuno Magalhaes

I think there is a problem also. The user can make a backup copy of the
application and use it whenever the old one expires.

How would you suggest me to make the timer irreversible... using the
Windows Registry? How can I do this... it seems that all solutions are
crackable.

Thanks for any reply,
Nuno Magalhaes.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I think I missunderstand you, I though you were doing an "upgrader" exe, one
that detect it the app to be upgraded does exist, perform the upgrade and
finish, not that the application could upgrade itself.

In my escenario, the to be upgraded app has the dll as an independend file.
so what you do in your upgrader is make sure the app is not running, extract
the dll, copy it and nothing more.

For that the code I sent you serve you, the only part missing is detecting
where the app is installed, that depend of the method you used to keep track
of that.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nuno Magalhaes said:
In the sequence of this message, how can I extract, read, change and
save again into the .exe file? Because I want to do a timer that would
expire after some hours, and I want to keep track of that timer
everytime I run the program.

Thank you very much,
Nuno Magalhaes.
Hi,


Frankly I have never embed a DLL, I have done it for access, excel, etc ,
the code below will do it.
Tentatively what you have to do is add the dll to your project, mark it
as
embedded resource ( IIRC it's marked as content by default) and that
should
do it

static string ExtractResource( string resourceName)
{
//look for the resource name
foreach( string currentResource in
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
)
if ( currentResource.LastIndexOf( resourceName) != -1 )
{
string fqnTempFile = System.IO.Path.GetTempFileName();
string path = System.IO.Path.GetDirectoryName( fqnTempFile);
string rootName= System.IO.Path.GetFileNameWithoutExtension(
fqnTempFile);
string destFile = path + @"\" + rootName + "." +
System.IO.Path.GetExtension( currentResource);

System.IO.Stream fs =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
currentResource);

byte[] buff = new byte[ fs.Length ];
fs.Read( buff, 0, (int)fs.Length);
fs.Close();

System.IO.FileStream destStream = new System.IO.FileStream (
destFile,
FileMode.Create);
destStream.Write( buff, 0, buff.Length);
destStream.Close();

return destFile;
}

throw new Exception("Resource not found : " + resourceName);

}


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



phobos7 said:
I am trying to create a single .EXE that will upgrade a product by the
doing the following:

1) Detect the location of the current installation (DONE)
2) Upgrade the existing config file (DONE)
3) Overwrite a DLL in the program files folder with a new DLL.

For 3) I was trying to embed a DLL as a resource and then write it out
as a file over the existing DLL. I just can't work out how to do this.

How can I embed a DLL as a simple file resource that is built into the
.EXE at runtime?

How do I then extract the DLL and output it as a file at runtime?

I would also like the DLL to be the most recent version so I would like
to reference it and then it just be built in when I build the patch.

Thanks for your time.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Now you have me confused, what is exactly what you want?
Upgrade an already deployed app or make a security mechanism?


And yes, all the possible solutions are crackeables

cheers,
 
N

Nuno Magalhaes

My question now is: "What is the best solution to make an irreversible
timer using C#?". Because I want to create a trial version of my
application that is guided by a maximum number of time "online".

How can I use the Windows registry in C#? How to read and write from
the Windows registry in order to make a simple timer updated from
second to second in a specific windows registry folder?

Is there some sample code in C# on how to access the registry? Sorry
for the confusion but this is another question that comes in sequence
of the first question.

Thanks again,
Nuno Magalhaes.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I suggest you doing a search in google groups regarding trial versiosn,
this has been discussed here a lot of time before, IMHO the best way to give
a demo version is give one with limited functionality.

Regarding read/write the registry you have a set of classes under
System.Win32 that deal with that, take a look at System.Win32.Registry class

cheers,
 
P

phobos7

Ignacio,

There are three of us in this thread :)

I wanted the installer option. I'm now making good headway on this.
Thanks! The main problem I had was converting the resource into a file
- which you solved. The second was having the embedded resource (DLL)
updated when I built its project. I did this by adding the resource as
a "linked" resource. If you add it in the standard way (or drag the
file into the project in VS.NET) it copies the .DLL (resource) locally.

Thanks for your help.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You are right :)

Sorry, I though it was nuno the OP :)

Yes, a link is what solve that, but IMO you would have more control if you
just copy it, while you are developing the dll you will not compile or send
the upgrader ( at least that's my case) so I manually copy the dll.

I have taken this into a greater extend :) , not only to replace a dll but
for replace/add several files, change config , etc.

I pack a bunch of files in the .exe , extract them as I show you and
interprete one of them (it's a text file with commands), there I instruct
what to copy/delete/replace, etc

I have find it much easier to just have a .exe that the remote client can
execute and upgrade the app.


cheers,
 

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

Top