article

Embed and extract DLLs or any other file

Email
Submitted on: 1/4/2015 4:40:00 AM
By: kicktd (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: VB.NET
Views: 2393
 
     This is my first tutorial here on Planet Source Code. This is to show a user how to add a file to the program such as a DLL that can be extracted to the folder with the program in it or anywhere the DLL needs to be placed.


 
				

After sitting and wondering just exactly how I could extract needed DLL files into
the program directory and several searches of finding people saying it could not
be done, I found a way to do it.

The first step is to find the DLL or resource you want/need to include in your project.
Find the file and drag it into your VB.NET Solution or right click on your form in the
solution explorer -> Add... -> Add Existing Item... and in the Files of type drop down
box select All Files (*.*) and navigate to the file you want to include, highlight it and
click on Open.

You will now see the file in your Solution Explorer. Click on the file and in the properties
window select Embedded Resource in the Build Action drop down box.

Ok so now we have the file included in our project, but just how do we get it out before
the program uses the needed resource? Well all we have to do is have the file output
to the directory before it is called upon, and this usually has to be done before the program
fully loads up
. First before we get into details, you must know the forms name. You can
find this out by looking at the Solution explorer and the name in bold text is your Form name
such as Form or MyProject. You will need to know this in the upcoming section.

Click the + next to Windows Form Designer generated code to expand the code.

You should see a section called:

Public Sub New()

For example here my Project is called Embed and I'm wanting to output a needed Winsock dll:

Public Sub New()
MyBase.New()
'Get our needed .DLL
GetResource(Application.StartupPath & "\", "AxInterop.MSWinsockLib.dll", "Embed.AxInterop.MSWinsockLib.dll")
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub

I made a function for use of doing this which you place normally in your project after the windows form designer code:

Private Function GetResource(ByVal Dir As String, ByVal StrFile As String, ByVal Resource As String)
If IO.File.Exists(Dir & StrFile) = False Then
Dim output As New IO.FileStream(Dir & StrFile, IO.FileMode.Create, IO.FileAccess.Write)
Dim buffer(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Length - 1) As Byte
System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Read(buffer, 0, System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Length)
output.Write(buffer, 0, System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Length)
System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(Resource).Close()
output.Close()
End If
End Function

And it's as easy as that when using the call to the function. I hope this tutorial will help many
when needing to include DLL's with their files or to make sure the file is there when the program
starts like if a user was to delete the DLL this would check if it is there and extract it if need be.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.