VB.NET ListView Tutorial. Add, Edit and Delete


I was searching for some tutorial on how to use ListView in VB .NET a few minutes ago and this was the solution that I gotten from Google search engine.  I have to complete my project of a Parental Control Software in 40 days, I wonder whether I can complete them in the remaining amount of time.  Although the project looks easy, it is not something I am coding for my own interest which makes me feel lazy everytime I turn on Visual Studio 2008.  But whatever, let us hope I can get the things done at the right time!

Link: http://www.fryan0911.com/2009/06/vbnet-listview-tutorial-add-edit-and.html

Advertisements

Creating application with a single instance in C and VB .NET


Most of us prefer to have single instances application to make it look professional or probably some other personal reason especially making malicious applications too.  Here is the source code for the 2 languages that has been mentioned.

C

#include <windows.h>

int main()
{
	char *mutex = "some name here";

	HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, mutex);
	if(hMutex == NULL)
	{
		hMutex = CreateMutex(NULL, FALSE, mutex);
	}
	else
	{
		MessageBox(0, "Instance Exists!", 0, 0);
		return 0;
	}

	return 0;
}

VB .NET

Function PrevInstance() As Boolean
	If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
            Return True
        Else
            Return False
        End If
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
	If PrevInstance() = True Then
            MsgBox("Instance Exists!")
            End
        End If
End Sub

Sorting Registries in VB .NET


So there’s this guy from http://leetcoders.org that needed some help getting a function created for him to breakup a single string that contains Name, Registry Key and Registry Data and appends the strings into a richtextbox.

AutoCAD 2010|Serial=HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R18.0\ACAD-8001:409\=SerialNumber

to

RichTextBox1.AppendText(“Autocad 2010” & vbNewLine)
Dim ammSteal01 As String = Registry.GetValue(“HKEY_LOCAL_MACHINE\SOFTWARE\Autodesk\AutoCAD\R18.0\ACAD-8001:409”, “SerialNumber”, Nothing)
RichTextBox1.AppendText(ammSteal01)

So i created a simple function called
Private Sub AppendStringToRichTextBox2(ByVal str As String, ByVal txtbox As RichTextBox)

which does

        Dim title As String = ""
        Dim key As String = ""
        Dim data As String = ""

        Dim str_arr As String() = str.Split("=")

        key = str_arr(1)
        data = str_arr(2)

        For i = 0 To str_arr(0).Length - 1
            If str_arr(0)(i) = "|" Then
                Exit For
            End If

            title += str_arr(0)(i)
        Next

        txtbox.AppendText(title & vbNewLine)
        txtbox.AppendText(Microsoft.Win32.Registry.GetValue(key, data, Nothing))

The function generally appends the name of the application and the value of the key that have been called via Registry.GetValue. Well it’s nothing hard so port it to your own use if you need it.

.NET and its vulnerabilities


Well i continued programming in VB .NET for the last 2 months and i finally got my own masterpiece! I’m able to sell the product but i have many doubts in mind especially crackers. I googled and searched various methods to get it safely encrypted as well as decompiling it (working both ways is so what gray hat does). Well i had found a variety of method of doing so and i’m pretty satisfied with the outcome! I never knew decompiling was so fun as well as injecting codes into it. Now’s the time to do some nasty art! Oh well.. luckily i’m a 90% most of the time C person… leave the 10% to VB .NET

Working around VB .NET


Do you know how amazing VB .NET is?  When i was programming in C it took me so much time to google and figure out solution.  VB .NET was the other way round!  Google “what do you want” + “in vb .net” and you got what you want instantly.

I wanted to check for the file extension so i googled and i got the result and towards my amazement the outcome came out pretty well!

Back when i was programming in C i have to use

if(strstr(filename, ".exe") != 0)

or probably you’ll use this!

if(filename[strlen(filename) - 1] != 'e' &&
filename[strlen(filename) - 2] != 'x' &&
filename[strlen(filename) - 3] != 'e' &&
filename[strlen(filename) - 4] != '.')

but in VB .NET (oh the wonders in the world), you’ll just

if Path.GetExtension(filename) = ".exe"

damn it… how i wished i learnt VB .NET in depth! – Not. Well although VB .NET provides us with so much simplicity i still prefer C as it was my first language and the one i mastered well the most. However i’m learning VB .NET for the sake of its GUI 🙂 Big thanks to those that brought VB .NET to how it is today!

Interaction between VB .NET and C/++


So you have some trouble building your application in C as the GUI is taking you a lot of time. You turn on your C/++ compiler and open up your project and thinks that it is very time consuming and you drop the idea and open up Just Cause 2 instead. Well i have a solution for you but you might think that it is far more complicated than the chicken and egg ideology.  Let us proceed to what i have in mind first.  

Solution

– By using VB .NET as our primary structure and C/++ as our backbone.  This can be done when you build your functions via C/++ in a dynamic link library (.dll) and your GUI that is built in VB .NET loads them.

Requirements

– Knowledge in C/++ and VB .NET
– Microsoft Visual C++ 6.0 and Microsoft Visual Studio 2008 (i’m using 1 for C and 2 for VB)
– some knowledge in building dynamic link library (.dll)  

Sample/ prototype

– i have built a simple calculator in VB .NET that performs 4 function which are (1.) Addition, (2.) Subtraction, (3.) Multiplication and (4.) Division.  However the functions are built in C form and it is placed in a .dll file which i have built as math.dll
  

Steps

Assuming that you already know how to build projects in Microsoft Visual Studio 2008, create a windows form project for VB .NET and drag the necessary controls as shown above. I’m not entirely good with the features provided in wordpress so i’ll be explaining the codes in detail rather than pasting the whole thing in a [ code][ /code] tag because numbers would show beside the codes and i think it’s a little messy this way.  

a
b
c

see what i mean?  

If you’ve already prepared the GUI in VB .NET lets place the respective codes for each controls.  

First place the codes below above all functions in VB .NET under Public Class Form1 

Declare Function Addition Lib “math.dll” (ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Declare Function Subtraction Lib “math.dll” (ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Declare Function Multiplication Lib “math.dll” (ByVal num1 As Integer, ByVal num2 As Integer) As Integer
Declare Function Division Lib “math.dll” (ByVal num1 As Integer, ByVal num2 As Integer) As Integer

Addition (RadioButton)
Private Sub rdAddition_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdAddition.CheckedChanged
lblEquate.Text = “+” ‘take note that lblEquate is the Label control between the textboxes on the left side
End Sub

Subtraction (RadioButton)
Private Sub rdSubtraction_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdSubtraction.CheckedChanged
lblEquate.Text = “-”
End Sub

Multiplication (RadioButton)
Private Sub rdMultiplication_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdMultiplication.CheckedChanged
lblEquate.Text = “*”
End Sub

Division (RadioButton)
Private Sub rdDivision_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdDivision.CheckedChanged
lblEquate.Text = “/”
End Sub

> (Button)
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
If rdAddition.Checked = True Then
txtResult.Text = Addition(Val(txtNum1.Text), Val(txtNum2.Text))
ElseIf rdSubtraction.Checked = True Then
txtResult.Text = Subtraction(Val(txtNum1.Text), Val(txtNum2.Text))
ElseIf rdMultiplication.Checked = True Then
txtResult.Text = Multiplication(Val(txtNum1.Text), Val(txtNum2.Text))
ElseIf rdDivision.Checked = True Then
txtResult.Text = Division(Val(txtNum1.Text), Val(txtNum2.Text))
End If
End Sub

(TextBox)
as you can see there’s 3 textboxes. From left will be txtNum1, txtNum2 and txtResult

I think everything so far is pretty much understandable except for the declaration part.  What each does is that it loads the function from a .dll file called math.dll and “Addition” for example is the function name.  As you can see there are 2 parameters which are Integer num1 and Integer num2 and it returns an Integer.

Try running the executable file you built (and not from the compiler) instead. Generally you’ll find error occur when you placed 2 numbers in the txtNum1 and txtNum2 and hit the button “>” because it couldn’t find math.dll

The C/++ part

Now open up your C/++ compiler and create a Win32 Dynamic-Link Library using Microsoft Visual C++ 6.0 (i’m not sure where do i create one from Microsoft Visual Studio 2008)

#define WIN32_LEAN_AND_MEAN
#include

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

As you can see from the above, it is the primary strucutre for a .dll, while we use main for basic console programming we are required to use DllMain for .dll.  I hope you get what i’m trying to say here.

__declspec(dllexport) short Addition(short num1, short num2){
return num1 + num2;
}

__declspec(dllexport) short Subtraction(short num1, short num2){
return num1 – num2;
}

__declspec(dllexport) short Multiplication(short num1, short num2){
return num1 * num2;
}

__declspec(dllexport) short Division(short num1, short num2){
return num1 / num2;
}

The above code is the rest of the required codes.  Find __declspec under msdn homepage and you’ll find what it is about.  Once you have built the project, place the math.dll that you have created in the same folder as your VB .NET’s executable (debug or release).  Try and run the VB .NET executable and use the “>” button and you’ll find it calculating fine.

Enhancement

By using this method, i am pretty sure that you can unravel more mysteries between VB .NET and C/++.  With this concept applied, you are not required to build a GUI from scratch.  Although there may be a gap while building the 2 programs seperately as you may find it easily exploited – you might want to consider on protecting your .dll.

Support

Check out http://www.flipcode.com/archives/Interfacing_Visual_Basic_And_C.shtml for more information on interacting VB .NET and C/++.  As you might be wondering why did i use Integer datatype in VB .NET and short in C you could find the answer pretty much all of it in this site including __declspec.