Is it possible to include a dll in the exe

  • Thread starter Thread starter ORC
  • Start date Start date
O

ORC

HI,

It is possible to include a custom control dll in the exe file so that the
dll isn't a standalone file that will have to be copied to the PDA along
with the exe file?

Thanks,
Ole
 
..NET does not support static linking so no.

You could sort of do it on the desktop with multi-module assemblies (even
then you would have to resort to the command line as VS doesn't support it).

CF 1.0 does not support multi-module assemblies though so you are out of
luck.

Cheers
Daniel
 
(I'm not sure I understand what multi-module assemblies are so maybe my next
question is exactly that). Would it be possible to create a design-time dll
to use at designtime, but when generating the runtime code the compiler
compiles the code from the source code class? This way it would be possible
to place the controls at designtime and still have the control implemented
in the exe file? - hope you understand this explanation - I'm not sure if it
is clear.

thanks,
Ole
 
May I suggest using google for terms you are not familiar with (e.g. multi
module assemblies OR multi file assemblies). In a nutshell, you can compile
code into a module (nothing to do with VB's module) and then combine a
number of modules into an assembly. One of the advantages is that each
module can be written in a different language. The unit of deployment
remains the assembly (dll or exe).

For your specific scenario, if the code for both the control and the client
are in the same language there is nothing stopping you from putting all the
code in the same project. Use the designer first to get the client code
generated and then ditch it. I don't know if there is a way to keep the
designer and the control code in the same project as the client code (I have
stopped using the designer in my projects anyway).

Cheers
Daniel
 
You can add DLL to the project, mark it as embedded resource, then, upon
loading your application, check for the dll existence in the file system and
if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}
 
That is fine on the desktop and for the benefit of the archives the code
snippet needs to close the stream before using types from the assembly (and
should check if the file already exists before creating it).

On the CF it seems the dependencies are checked before any user code gets a
chance to execute, so I cannot get it working. If you have a working CF
example please send it to me (I am interested in it from a POC point of view
:-).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Alex Feinman said:
You can add DLL to the project, mark it as embedded resource, then, upon
loading your application, check for the dll existence in the file system
and if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
ORC said:
HI,

It is possible to include a custom control dll in the exe file so that
the
dll isn't a standalone file that will have to be copied to the PDA along
with the exe file?

Thanks,
Ole
 
Hi Daniel,

I've done what Alex is talking about, You can even inherit your main form
from a custom form in the dll. I did it by.

1. creating a new class in the application
2. creating static constructor for that class ( this is where you would put
the code to extract your dll, you just need to extract it to the same
location as your .exe, the clr will look for it there)
3. moving static void main() function out of the form and into the new class.

This will allow you to extract the dll before the clr tries to load it.

In fact I include all of the resources as embedded resources. This way I
don't need to create a .cab to install the app. We can send the same .exe to
a customer to test on their desktop as the one we put on their device, with
no install. I also had problems debugging with the emulator with images
built as content that were stored in folders. The images would be deployed
to the emulator with a flat directory structure so the app couldn't locate
them. Embedding them into the project and extracting them in the same manor
as the custom control dll fixed this.

I was working on an example that I was going to post at some point but never
got around to it, I will go ahead and send it to you

Kind Regards,
Jerron

Daniel Moth said:
That is fine on the desktop and for the benefit of the archives the code
snippet needs to close the stream before using types from the assembly (and
should check if the file already exists before creating it).

On the CF it seems the dependencies are checked before any user code gets a
chance to execute, so I cannot get it working. If you have a working CF
example please send it to me (I am interested in it from a POC point of view
:-).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Alex Feinman said:
You can add DLL to the project, mark it as embedded resource, then, upon
loading your application, check for the dll existence in the file system
and if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
ORC said:
HI,

It is possible to include a custom control dll in the exe file so that
the
dll isn't a standalone file that will have to be copied to the PDA along
with the exe file?

Thanks,
Ole
 
Great option! Thanks.

Ole


Alex Feinman said:
You can add DLL to the project, mark it as embedded resource, then, upon
loading your application, check for the dll existence in the file system and
if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll
..dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQ
ualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
ORC said:
HI,

It is possible to include a custom control dll in the exe file so that the
dll isn't a standalone file that will have to be copied to the PDA along
with the exe file?

Thanks,
Ole
 
Don't you love it when you learn something new? :-)

Jerron thanks for the offer. No need to send me one I got it working. I
started a new solution from scratch and it just worked (initially I thought
maybe it was a language thing so I tried both VB and C# and they both
worked.). I still have absolutely no idea why my original project does not
work (it still doesn't!), while it works on the desktop fine, but I think I
am going to have to let it go.

BTW, you don't need to move the Main function to its own class and you don't
need to create a static constructor in a separate one (in fact static ctors
are not guaranteed to run unless needed); I dropped the code in just before
Application.Run.

Searching the web I found other places that discuss this technique... if
anybody knows where it originated from (for future reference) feel free to
let me know

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jer_m said:
Hi Daniel,

I've done what Alex is talking about, You can even inherit your main form
from a custom form in the dll. I did it by.

1. creating a new class in the application
2. creating static constructor for that class ( this is where you would
put
the code to extract your dll, you just need to extract it to the same
location as your .exe, the clr will look for it there)
3. moving static void main() function out of the form and into the new
class.

This will allow you to extract the dll before the clr tries to load it.

In fact I include all of the resources as embedded resources. This way I
don't need to create a .cab to install the app. We can send the same .exe
to
a customer to test on their desktop as the one we put on their device,
with
no install. I also had problems debugging with the emulator with images
built as content that were stored in folders. The images would be
deployed
to the emulator with a flat directory structure so the app couldn't locate
them. Embedding them into the project and extracting them in the same
manor
as the custom control dll fixed this.

I was working on an example that I was going to post at some point but
never
got around to it, I will go ahead and send it to you

Kind Regards,
Jerron

Daniel Moth said:
That is fine on the desktop and for the benefit of the archives the code
snippet needs to close the stream before using types from the assembly
(and
should check if the file already exists before creating it).

On the CF it seems the dependencies are checked before any user code gets
a
chance to execute, so I cannot get it working. If you have a working CF
example please send it to me (I am interested in it from a POC point of
view
:-).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Alex Feinman said:
You can add DLL to the project, mark it as embedded resource, then,
upon
loading your application, check for the dll existence in the file
system
and if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
HI,

It is possible to include a custom control dll in the exe file so that
the
dll isn't a standalone file that will have to be copied to the PDA
along
with the exe file?

Thanks,
Ole
 
You don't have to use VS but if that is all you have:
Create a new icon file and then from the Image menu look at the 3 image type
menuitems.

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jer_m said:
Hi Daniel,

I've done what Alex is talking about, You can even inherit your main form
from a custom form in the dll. I did it by.

1. creating a new class in the application
2. creating static constructor for that class ( this is where you would
put
the code to extract your dll, you just need to extract it to the same
location as your .exe, the clr will look for it there)
3. moving static void main() function out of the form and into the new
class.

This will allow you to extract the dll before the clr tries to load it.

In fact I include all of the resources as embedded resources. This way I
don't need to create a .cab to install the app. We can send the same .exe
to
a customer to test on their desktop as the one we put on their device,
with
no install. I also had problems debugging with the emulator with images
built as content that were stored in folders. The images would be
deployed
to the emulator with a flat directory structure so the app couldn't locate
them. Embedding them into the project and extracting them in the same
manor
as the custom control dll fixed this.

I was working on an example that I was going to post at some point but
never
got around to it, I will go ahead and send it to you

Kind Regards,
Jerron

Daniel Moth said:
That is fine on the desktop and for the benefit of the archives the code
snippet needs to close the stream before using types from the assembly
(and
should check if the file already exists before creating it).

On the CF it seems the dependencies are checked before any user code gets
a
chance to execute, so I cannot get it working. If you have a working CF
example please send it to me (I am interested in it from a POC point of
view
:-).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Alex Feinman said:
You can add DLL to the project, mark it as embedded resource, then,
upon
loading your application, check for the dll existence in the file
system
and if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
HI,

It is possible to include a custom control dll in the exe file so that
the
dll isn't a standalone file that will have to be copied to the PDA
along
with the exe file?

Thanks,
Ole
 
Thanks!

Ole

Alex Feinman said:
You can add DLL to the project, mark it as embedded resource, then, upon
loading your application, check for the dll existence in the file system and
if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll
..dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQ
ualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
ORC said:
HI,

It is possible to include a custom control dll in the exe file so that the
dll isn't a standalone file that will have to be copied to the PDA along
with the exe file?

Thanks,
Ole
 
Hi Daniel,

I'm sorry I should have clarified more, your right in most scenarios you
dont need to move the main function to the other class and create a static
constructor. However if your form that contains main() derives from a class
in the embedded dll you need to do this...

Another note, when embedding the dll in the executable, you should embed it
in a sub folder of the project. The reason for this is, once the dll exists
in the project where it would be copied if you set the reference to copy
local, the reference in visual studio will automatically set itself to
local. So when you change the dll its more of a pain.

Additionally what I do is auto update the dll, in the designer code for my
custom form I get a stream to the runtime version of the dll and compare it
with the embedded version in the solution. If its not the same or doesn't
exist I place/replace it in the solution. It's in the example I sent to you
( sorry I emailed it before you responded ).

As far as where the technique came from I haven't seen it posted anywhere,
but I wouldn't be surprised if I reinvented the wheel, so I can't help you
there. I'm interested to see the other places you saw it though, I was
kicking myself a while ago when I was trying to doing this.

Kind Regards,

Jerron Marshall

Daniel Moth said:
Don't you love it when you learn something new? :-)

Jerron thanks for the offer. No need to send me one I got it working. I
started a new solution from scratch and it just worked (initially I thought
maybe it was a language thing so I tried both VB and C# and they both
worked.). I still have absolutely no idea why my original project does not
work (it still doesn't!), while it works on the desktop fine, but I think I
am going to have to let it go.

BTW, you don't need to move the Main function to its own class and you don't
need to create a static constructor in a separate one (in fact static ctors
are not guaranteed to run unless needed); I dropped the code in just before
Application.Run.

Searching the web I found other places that discuss this technique... if
anybody knows where it originated from (for future reference) feel free to
let me know

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jer_m said:
Hi Daniel,

I've done what Alex is talking about, You can even inherit your main form
from a custom form in the dll. I did it by.

1. creating a new class in the application
2. creating static constructor for that class ( this is where you would
put
the code to extract your dll, you just need to extract it to the same
location as your .exe, the clr will look for it there)
3. moving static void main() function out of the form and into the new
class.

This will allow you to extract the dll before the clr tries to load it.

In fact I include all of the resources as embedded resources. This way I
don't need to create a .cab to install the app. We can send the same .exe
to
a customer to test on their desktop as the one we put on their device,
with
no install. I also had problems debugging with the emulator with images
built as content that were stored in folders. The images would be
deployed
to the emulator with a flat directory structure so the app couldn't locate
them. Embedding them into the project and extracting them in the same
manor
as the custom control dll fixed this.

I was working on an example that I was going to post at some point but
never
got around to it, I will go ahead and send it to you

Kind Regards,
Jerron

Daniel Moth said:
That is fine on the desktop and for the benefit of the archives the code
snippet needs to close the stream before using types from the assembly
(and
should check if the file already exists before creating it).

On the CF it seems the dependencies are checked before any user code gets
a
chance to execute, so I cannot get it working. If you have a working CF
example please send it to me (I am interested in it from a POC point of
view
:-).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


You can add DLL to the project, mark it as embedded resource, then,
upon
loading your application, check for the dll existence in the file
system
and if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
HI,

It is possible to include a custom control dll in the exe file so that
the
dll isn't a standalone file that will have to be copied to the PDA
along
with the exe file?

Thanks,
Ole
 
Additionally I think you have to use a static constructor if any newly
created classes derive from anything thats in the embedded dll.

Kind Regards,
Jerron Marshall

jer_m said:
Hi Daniel,

I'm sorry I should have clarified more, your right in most scenarios you
dont need to move the main function to the other class and create a static
constructor. However if your form that contains main() derives from a class
in the embedded dll you need to do this...

Another note, when embedding the dll in the executable, you should embed it
in a sub folder of the project. The reason for this is, once the dll exists
in the project where it would be copied if you set the reference to copy
local, the reference in visual studio will automatically set itself to
local. So when you change the dll its more of a pain.

Additionally what I do is auto update the dll, in the designer code for my
custom form I get a stream to the runtime version of the dll and compare it
with the embedded version in the solution. If its not the same or doesn't
exist I place/replace it in the solution. It's in the example I sent to you
( sorry I emailed it before you responded ).

As far as where the technique came from I haven't seen it posted anywhere,
but I wouldn't be surprised if I reinvented the wheel, so I can't help you
there. I'm interested to see the other places you saw it though, I was
kicking myself a while ago when I was trying to doing this.

Kind Regards,

Jerron Marshall

Daniel Moth said:
Don't you love it when you learn something new? :-)

Jerron thanks for the offer. No need to send me one I got it working. I
started a new solution from scratch and it just worked (initially I thought
maybe it was a language thing so I tried both VB and C# and they both
worked.). I still have absolutely no idea why my original project does not
work (it still doesn't!), while it works on the desktop fine, but I think I
am going to have to let it go.

BTW, you don't need to move the Main function to its own class and you don't
need to create a static constructor in a separate one (in fact static ctors
are not guaranteed to run unless needed); I dropped the code in just before
Application.Run.

Searching the web I found other places that discuss this technique... if
anybody knows where it originated from (for future reference) feel free to
let me know

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jer_m said:
Hi Daniel,

I've done what Alex is talking about, You can even inherit your main form
from a custom form in the dll. I did it by.

1. creating a new class in the application
2. creating static constructor for that class ( this is where you would
put
the code to extract your dll, you just need to extract it to the same
location as your .exe, the clr will look for it there)
3. moving static void main() function out of the form and into the new
class.

This will allow you to extract the dll before the clr tries to load it.

In fact I include all of the resources as embedded resources. This way I
don't need to create a .cab to install the app. We can send the same .exe
to
a customer to test on their desktop as the one we put on their device,
with
no install. I also had problems debugging with the emulator with images
built as content that were stored in folders. The images would be
deployed
to the emulator with a flat directory structure so the app couldn't locate
them. Embedding them into the project and extracting them in the same
manor
as the custom control dll fixed this.

I was working on an example that I was going to post at some point but
never
got around to it, I will go ahead and send it to you

Kind Regards,
Jerron

:

That is fine on the desktop and for the benefit of the archives the code
snippet needs to close the stream before using types from the assembly
(and
should check if the file already exists before creating it).

On the CF it seems the dependencies are checked before any user code gets
a
chance to execute, so I cannot get it working. If you have a working CF
example please send it to me (I am interested in it from a POC point of
view
:-).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


You can add DLL to the project, mark it as embedded resource, then,
upon
loading your application, check for the dll existence in the file
system
and if it's not there, simply copy it there from the resource stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
HI,

It is possible to include a custom control dll in the exe file so that
the
dll isn't a standalone file that will have to be copied to the PDA
along
with the exe file?

Thanks,
Ole
 
Hi Jerron

Thanks for your comments
there. I'm interested to see the other places you saw it though, I was
kicking myself a while ago when I was trying to doing this.

For example, a similar technique is described here:
http://weblogs.asp.net/jdennany/archive/2003/09/24/29035.aspx

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jer_m said:
Hi Daniel,

I'm sorry I should have clarified more, your right in most scenarios you
dont need to move the main function to the other class and create a static
constructor. However if your form that contains main() derives from a
class
in the embedded dll you need to do this...

Another note, when embedding the dll in the executable, you should embed
it
in a sub folder of the project. The reason for this is, once the dll
exists
in the project where it would be copied if you set the reference to copy
local, the reference in visual studio will automatically set itself to
local. So when you change the dll its more of a pain.

Additionally what I do is auto update the dll, in the designer code for my
custom form I get a stream to the runtime version of the dll and compare
it
with the embedded version in the solution. If its not the same or doesn't
exist I place/replace it in the solution. It's in the example I sent to
you
( sorry I emailed it before you responded ).

As far as where the technique came from I haven't seen it posted anywhere,
but I wouldn't be surprised if I reinvented the wheel, so I can't help you
there. I'm interested to see the other places you saw it though, I was
kicking myself a while ago when I was trying to doing this.

Kind Regards,

Jerron Marshall

Daniel Moth said:
Don't you love it when you learn something new? :-)

Jerron thanks for the offer. No need to send me one I got it working. I
started a new solution from scratch and it just worked (initially I
thought
maybe it was a language thing so I tried both VB and C# and they both
worked.). I still have absolutely no idea why my original project does
not
work (it still doesn't!), while it works on the desktop fine, but I think
I
am going to have to let it go.

BTW, you don't need to move the Main function to its own class and you
don't
need to create a static constructor in a separate one (in fact static
ctors
are not guaranteed to run unless needed); I dropped the code in just
before
Application.Run.

Searching the web I found other places that discuss this technique... if
anybody knows where it originated from (for future reference) feel free
to
let me know

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jer_m said:
Hi Daniel,

I've done what Alex is talking about, You can even inherit your main
form
from a custom form in the dll. I did it by.

1. creating a new class in the application
2. creating static constructor for that class ( this is where you would
put
the code to extract your dll, you just need to extract it to the same
location as your .exe, the clr will look for it there)
3. moving static void main() function out of the form and into the new
class.

This will allow you to extract the dll before the clr tries to load it.

In fact I include all of the resources as embedded resources. This way
I
don't need to create a .cab to install the app. We can send the same
.exe
to
a customer to test on their desktop as the one we put on their device,
with
no install. I also had problems debugging with the emulator with
images
built as content that were stored in folders. The images would be
deployed
to the emulator with a flat directory structure so the app couldn't
locate
them. Embedding them into the project and extracting them in the same
manor
as the custom control dll fixed this.

I was working on an example that I was going to post at some point but
never
got around to it, I will go ahead and send it to you

Kind Regards,
Jerron

:

That is fine on the desktop and for the benefit of the archives the
code
snippet needs to close the stream before using types from the assembly
(and
should check if the file already exists before creating it).

On the CF it seems the dependencies are checked before any user code
gets
a
chance to execute, so I cannot get it working. If you have a working
CF
example please send it to me (I am interested in it from a POC point
of
view
:-).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


You can add DLL to the project, mark it as embedded resource, then,
upon
loading your application, check for the dll existence in the file
system
and if it's not there, simply copy it there from the resource
stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
HI,

It is possible to include a custom control dll in the exe file so
that
the
dll isn't a standalone file that will have to be copied to the PDA
along
with the exe file?

Thanks,
Ole
 
Hi Daniel,

Awesome article, thanks for the link. I'm adding that site to my favorites.
I definitely had some aspects of reinventing the wheel there, now I'm
kicking myself some more :)

Although I'm debating whether or not I should post my example anyways
because there were a number of differences, hopefully it may be of value to
someone.

Kind regards,
Jerron

Daniel Moth said:
Hi Jerron

Thanks for your comments
there. I'm interested to see the other places you saw it though, I was
kicking myself a while ago when I was trying to doing this.

For example, a similar technique is described here:
http://weblogs.asp.net/jdennany/archive/2003/09/24/29035.aspx

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


jer_m said:
Hi Daniel,

I'm sorry I should have clarified more, your right in most scenarios you
dont need to move the main function to the other class and create a static
constructor. However if your form that contains main() derives from a
class
in the embedded dll you need to do this...

Another note, when embedding the dll in the executable, you should embed
it
in a sub folder of the project. The reason for this is, once the dll
exists
in the project where it would be copied if you set the reference to copy
local, the reference in visual studio will automatically set itself to
local. So when you change the dll its more of a pain.

Additionally what I do is auto update the dll, in the designer code for my
custom form I get a stream to the runtime version of the dll and compare
it
with the embedded version in the solution. If its not the same or doesn't
exist I place/replace it in the solution. It's in the example I sent to
you
( sorry I emailed it before you responded ).

As far as where the technique came from I haven't seen it posted anywhere,
but I wouldn't be surprised if I reinvented the wheel, so I can't help you
there. I'm interested to see the other places you saw it though, I was
kicking myself a while ago when I was trying to doing this.

Kind Regards,

Jerron Marshall

Daniel Moth said:
Don't you love it when you learn something new? :-)

Jerron thanks for the offer. No need to send me one I got it working. I
started a new solution from scratch and it just worked (initially I
thought
maybe it was a language thing so I tried both VB and C# and they both
worked.). I still have absolutely no idea why my original project does
not
work (it still doesn't!), while it works on the desktop fine, but I think
I
am going to have to let it go.

BTW, you don't need to move the Main function to its own class and you
don't
need to create a static constructor in a separate one (in fact static
ctors
are not guaranteed to run unless needed); I dropped the code in just
before
Application.Run.

Searching the web I found other places that discuss this technique... if
anybody knows where it originated from (for future reference) feel free
to
let me know

Cheers
Daniel
--
http://www.danielmoth.com/Blog/



Hi Daniel,

I've done what Alex is talking about, You can even inherit your main
form
from a custom form in the dll. I did it by.

1. creating a new class in the application
2. creating static constructor for that class ( this is where you would
put
the code to extract your dll, you just need to extract it to the same
location as your .exe, the clr will look for it there)
3. moving static void main() function out of the form and into the new
class.

This will allow you to extract the dll before the clr tries to load it.

In fact I include all of the resources as embedded resources. This way
I
don't need to create a .cab to install the app. We can send the same
.exe
to
a customer to test on their desktop as the one we put on their device,
with
no install. I also had problems debugging with the emulator with
images
built as content that were stored in folders. The images would be
deployed
to the emulator with a flat directory structure so the app couldn't
locate
them. Embedding them into the project and extracting them in the same
manor
as the custom control dll fixed this.

I was working on an example that I was going to post at some point but
never
got around to it, I will go ahead and send it to you

Kind Regards,
Jerron

:

That is fine on the desktop and for the benefit of the archives the
code
snippet needs to close the stream before using types from the assembly
(and
should check if the file already exists before creating it).

On the CF it seems the dependencies are checked before any user code
gets
a
chance to execute, so I cannot get it working. If you have a working
CF
example please send it to me (I am interested in it from a POC point
of
view
:-).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


You can add DLL to the project, mark it as embedded resource, then,
upon
loading your application, check for the dll existence in the file
system
and if it's not there, simply copy it there from the resource
stream.

Stream strDLL =
Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNameSpace.MyDll.dll");
//Case sensitive
string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
FileStream fs = File.Create(path + "\\MyDLL.dll");
int nRead = -1;
byte[] buffer = new byte[4096];
while(nRead != 0)
{
nRead = strDLL.Read(buffer, 0, buffer.Length);
if ( nRead > 0 )
fs.Write(buffer, 0, nRead);
}

--
Alex Feinman
---
Visit http://www.opennetcf.org
HI,

It is possible to include a custom control dll in the exe file so
that
the
dll isn't a standalone file that will have to be copied to the PDA
along
with the exe file?

Thanks,
Ole
 
Back
Top