Your source for computer information
Topic Categories   Links   Contact
Website
-Website Design
-Domain Names
-Advertising Tips
-Hosting
-DNS
-IIS
-HTML
-XHTML
-PHP
-MySQL
-Search Engine Optimization
-Forum Software
Programming
-Assembly
-C/C++
-C#
-Delphi
-Java
-Visual Basic
-VB.net
-Programming Books
General Computer
-Windows
-Linux
-Unix
-Computer Hardware
-Computer Vocabulary
-System Maintenance

Other Software
-MailEnable
-XVI32
-Olly Debugger
-TextPad


Visual Basic

Visual Basic is a Rapid Application Development language and my RAD language of choice for windows applications.  It is easy to use, yet very powerful with full access to windows api's you can create almost any windows based application you can imagine. Even Microsoft is still using Visual Basic for their current AntiSpyware software.

Creating a new Visual Basic Program is easy.  To create a Visual Basic Exe file use the Standard Exe option.

Click the image below see a picture of the default VB6 Integrated Development Enviroment

Setting the project Properties dialog.  On this page you can select the startup type, either one of the forms, or using Sub Main.

Here you can set the application title, icon, and version information.

You can also set the version number to auto increment each time you build a new program.

This is the  components dialog, which allows you to add additional components to the toolbox. This is found under the Project Menu then the Components Menu.

This is the references Dialog, here you can references to dll's and type libraries.

This is a picutre of the Visual Basic Menu Editor

This is a picture of the Add-In manger where you can toggle what addins are loaded.

Decompiling Visual Basic

Decompiling VB is not an easy task, the only really successful VB Decompilers was DoDi's decompiler for VB 2 and 3.

For Visual Basic 5/6 - Visual Basic has two compiling options default Native assembly code, or P-Code which is interpreted, P-Code is easier to turn back to the original code, while native is hard and there currently is no ideal product that can recover it.

Semi VB Decompiler for VB 5/6 - is a semi visual basic decompiler which can recover the forms, api's, and P-Code information for events and procedures.

For VB.net is very easy to decompile and there are numerous decompilers for it, and you can even convert the VB.net code to C# because that is how the .Net framework is designed.

My Old Visual Basic 6 Guide that I started to write never finished it so here it is.
Introduction:
what are you going to learn out of this guide?
You are going to learn how to code correctly in Visual Basic.
And maybe pickup a thing or two you did not know before.
I am assuming that you are able to pick a control from the toolbox
and placing it on the form and setting its properties.
I broke up the guide into four parts.

Part1 - The Basics involving writing good code and formats.
Part2 - The Code basic visual basic commands.
Part3 - Advanced Visual Basic - Dealing with Api's and a lot of cool things.


Part 1A: The Basics
An important of being a good Visual Basic programmer is using good names for your files
and your controls. It may seem like extra work but in the long run, it will enable you to
code faster and allow yourself and others to understand your own code better.

A rule I use for prefixes is to keep the prefix in lowercase then uppercase the first letter
of the name of the control or file.

Prefixes for files:

prj = Project
frm = Form
mod = Module
cls = class
usr = user control


Prefixes to Common Controls
lbl = Label control
cmd = Command button
img = Image control
pic = Picture box
tmr = Timer
shp = Shape
chk = Checkbox
lst = Listbox
txt = Textbox
opt = Option button (Radio button)
hs = Horizontal scrollbar
vs = Vertical scrollbar
cbo = Combo box
mnu = Menu
sck = Winsock

Bad Names do not use!
Form1
Command1
Timer1
Picture1

Good Names
frmMain
cmdClose
TmrLoop
picView


Prefixes can also be very useful to you when you are writing out code.

str = String
lng = Long
int = Integer
sng = Single
bln = Boolean
var = Variant

Bad Example:
dim stuff as string

stuff=inputbox("Please enter your name")

Good example:

dim strName as String

strName = inputbox("Please enter your name")

Part 1B. Making your code readable part 2.
You have already learned how prefixes can help making coding faster and easier.
Now lets move on to comments and indenting!

One of the worst things around is finding code that is not indented and or no white space.

Bad Example

sub ProcessNumber(intNumber as integer)
if intnumber= 3 then
else
if intNumber = 4 then
end if
end if
End Sub

Good Example:

sub ProcessNumber(intNumber as integer)
If intnumber= 3 then

Else
If intNumber = 4 then

End If
End If
End Sub

When do you indent?
You also tab once when you begin to code in an event or subroutine.
Then for each if statement, loop, you also indent.

Comments:
Visual Basic comments begin with ' and can go anywhere that you want
They are useful in explaining your code to yourself and others.
I really wish vb had multi comments like in C++ but it is not too bad.

White Space:
Leaving white space makes your code easier to read and makes it look cleaner

Part 2: The Code
The fun stuff begins now.


The very first line in all your forms and modules should be
Option Explicit

What does Option Explicit do?
Well it forces you to declare all your variables with otherwise would make them
all the variant type.
example so you can not do
for i=0 to 100
next i

You have to first have i declared
Dim i as byte
for i=0 to 100
next i


Variables
What are Variables? They hold data that may change when you run your program

Byte = holds numbers from 0 to 255
String = holds characters or letters such as "Hello World! and numbers too 123456789"
Integer= numbers no decimals from -32,768 to 32,767
Long = numbers no decimals from -2,147,483,647 to 2,147,483,647
Single = Can hold decimal numbers 32bit.
Boolean = holds either True or False

Constants
What are constants? They hold data that does not change.
An example of a constant would be
private const Pi = 3.14

Private/Public/Global

What does Private mean?
Private means it can only be accessed in the current form, module, or class.

What does Public and Global mean?
Public means it can be accessed from any form, module, or class


Static
If you make a variable static it will save its value the next time
the sub is run. I suggest you do NOT use this.



IF Then statements


One line if then statement

If condition=True then blnSomething=true

Multi Line if then statement
If condition=true Then
'Your code goes here

End If


Complex IF then else statements

If condition=True Then

Else
'Condition=false

End If


If condition=true Then

ElseIf contition2 = True Then

End If


Select Case Statement:
Like the switch statement in c++
What is it good for?
It is good for instances when you have one variable and
do not want to have a million if then statements.

Example
Dim intNumber as integer

select case intNumber

case 1:

case 2:

case else:

end select



Loops:

For Next Loop
Syntax for

For i = 0 to 100 step 100

next


Do While


Do Until


Subs and Functions overview of ByVal and ByRef
By default all visual basic parameters are passed by reference which means you are passing
the memory address and not the value.
sub Test()
dim strReturnValue as string

call Test2(strReturnValue)

msgbox strReturnValue

end sub
sub Test2(strRef as String)
strRef="Hello World"

end sub


What is a Function? A function is a special kind of procedure that returns a value for instance
sub Main

msgbox AddNumbers(3,4)

end sub
private function AddNumbers(ByVal intNumber1 as integer, ByVal intNumber2 as integer) as integer
AddNumbers = (intNumber1 + intNumber2)
end function

In the example above it shows a message box with the result of 7


You can specify how you want to pass a parameter either by reference (byRef)
or by value (byVal)
You want to use pass by value when you are not going to change the parameter.
sub Example(ByRef strRef as string, ByVal intNumber as integer)

end sub



Some Quick Reference:
All Visual Basic functions and commands are listed in the object browser.
It looks like a box with things coming out of it in the toolbar.

Use App.path instead of hardcoded paths.
App.path returns the path to your application


You can use app.previstance to detect if your application is already running.


Part 3: Advanced Visual Basic Information

Section A: Understanding API's
Your guide for Visual Basic Api's is http://www.allapi.net
Once you vist allapi.net
Get Api Viewer 2004 lists tons of VB Api's
Get Api Guide has many Api's and examples of how to use them.


Converting C++ Api's to Visual Basic Api's
VB Integers = C++ Short
VB Longs = C++ Integers


Section B: Pointers
There is a common myth going around that Visual Basic does not support pointers.
This is untrue they are just hidden.

VarPtr
ObjPtr
StrPtr

AddressOf - Used for finding addresses of procedures


Section C: Understanding VB's binary data.
Visual Basic stores a string in the following format
Length as integer
Text as String

Bytes take up 1 byte of data.
Boolean takes up two bytes either 00 00 or FF FF
Integer's take up two bytes.
Long takes up four bytes.
Single takes up four bytes.
Double takes up eight bytes.

Section D: Compiling real Dll's in Visual Basic like C++
By default Visual Basic does not make dll's with exports, they just make active x dll's.
But you can make them in Visual Basic if you can intercept the compiling process.
You can make your own c2.exe to intercept the parameters being passed to the real c2.exe then pass that
information plus the def file to create a normal dll with real exports.

Sites to check out:

VisualBasicZone.com - a place to post your Visual Basic Articles, Source Code, Game Projects and Links.

Copyright TheProgrammer.org