Guide to Visual Basic 6

Posted by

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.

Part4 – Extra Goodies

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(ByRef intNumber as integer)

if intnumber= 3 then

else

if intNumber = 4 then

end if

end if

End Sub

Good Example:

sub ProcessNumber(ByRef 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 line 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.

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 Statment:

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: Reverse Engineering Visual Basic VB (5/6)

For the last couple of months this has been the area I have been working on.

For Visual Basic 6 exe’s there are two methods of compiling either Native or P-code.

P-Code is pretty easy to understand since all the opcodes are known and it is just

matter of figuring out which opcode does what and linking the imports of the exe.

Native on the other hand is tricky it involves converting assembly to visual basic code.

check out [http://decompiler.theautomaters.com] – Visual Basic Decompiler forum

for more information.

Section E: 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.

Section F: Debugging/Decompiling

Tips I like to use Debug.print or use a msgbox statement to see what’s going on in my code.

SmartCheck – excellent tool for debugging a compiled exe.

Visual Basic Decompiler list:

Semi VB Decompiler – http://www.visualbasiczone.com

VB Parser 1.2

Race

VB Reformer

VBDE

VB Editor

VBREZQ

EXDEC

Section G: Choosing your compiling type.

Visual Basic Offers two ways to compile your application either Native or P-Code

Native-

Is faster

Harder to Decompile

Is larger

P-Code

Slower

Easier to Decompiler

Smaller exe’s

Part 4: Extra Goodies

Section A: Links!

http://www.pscode.com – Best place to look for visual basic code.

http://www.vbgamer.com – dealing with Visual Basic Games

[http://decompiler.theautomaters.com] – Visual Basic Decompiler forum

http://www.visualbasiczone.com – my site for VB

http://www.vbcity.com – Excellent community of knowledgeable people

Leave a Reply

Your email address will not be published. Required fields are marked *