STEP 1. Create a VB.NET project as "Class Library".
Write this code as example (change accordingly). You will get Class1 there just need add a fuction here vikas_add (adding 2 numbers & returning result). DLL will have same name as your project name.
-----------------------------------
Public Class Class1
Public Function vikas_add(ByVal a As Integer, ByVal b As Integer) As Integer
Return a + b
End Function
End Class
-------------------------------------
STEP 2. Save and Build the project. This will create DLL. (prob. in Release folder).
STEP 3. Register your DLL.
For this i follow these steps--
1) Start -> Run -> Regedit --- This will open Registry Editor
2) Locate HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders
3) Right click AssemblyFolders and select New -> Key
4) Give any name. (i gave MyDLLs)
5) Select MyDLLs, double click on default in right pane, give path like (c:\MyDLL) which i have given.
6) Close Registry editor
7) Create a folder c:\MyDLL and copy you dll created above in this folder.
STEP 3. Create a new project as "Windows Application".
Add Reference as--> Project --> Add reference.
In .NET Tab you will get Component name as ClassLibrary1 and Path as your folder and DLL. Select that and click on OK. You can check in All Files in Project Explorer, in references.
CODE eg
---------------------------------------------------
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim obj As New ClassLibrary1.Class1 'Here classLibrary1.class1 is your DLL class which have vikas_add function
Dim txt1, txt2, res As Integer
txt1 = TextBox1.Text
txt2 = TextBox2.Text
'MessageBox.Show(obj.vikas_add(txt1, txt2))
res = obj.vikas_add(txt1, txt2)
Label2.Text = "Result of Numbers=" & res.ToString
End Sub
End Class
---------------------------------------------------
STEP 4. Save and Run........
----------------------------------------------------------------------------
There is also a way in VB.NET to use DLL, by add these 2 statemets, but dint work for me, every time i m getting entry point error. But with above i able to use DLL's function.
Whts tht?
can anybody comments?
Imports System.Runtime.InteropServices
Public Shared Function vikas_add(ByVal aa As Integer, ByVal bb As Integer) As Integer
End Function
Also it can be done without using REGISTRY.
ReplyDeleteIn project "Windows Application"
use Project -> Add Reference and Browse the DLL created in "Class Library" project in its Release folder(prob).
Now use name of root namespace of your "class library" project in "window application" project.
i.e
Here root namespace is classlibrary1
so create obj like
dim obj as new classlibrary1.[all classes]
now use obj and call function/sub
BUT DllImport ????????????