converting a string to uppercase or lowercase using the API

Posted on February 27th, 2008 in Visual Basic by admin

converting a string to uppercase or lowercase using the API

Start up Visual Basic as usual and enter the code below in the general declarations section of the form.

Option Explicit

Private Declare Function CharLower Lib “user32″ Alias “CharLowerA” (ByVal lpsz As String) As Long
Private Declare Function CharUpper Lib “user32″ Alias “CharUpperA” (ByVal lpsz As String) As Long

Now enter the following in the Form_Load event procedure

Private Sub Form_Load()
Dim strTest As String
Me.AutoRedraw = True
‘test string
strTest = “This is a test string Which is Mixed Case”
Me.Print strTest
‘Convert all the characters to uppercase
CharUpper strTest
Me.Print strTest
‘Convert all the characters to lowercase
CharLower strTest
Me.Print strTest
End Sub

Now run the program (Press F5) and all going well you should see the date and time of your system. Here is what was displayed
on the test system.

Analysis

This uses the CharUpper and CharLower API commands which take a pointer to astring, the string will be hardcoded in and then
converted.

This could be useful for ensuring that a user used only lowercase when entering a username / password combo in an app.

Displaying the Date and time using the API

Posted on February 27th, 2008 in Visual Basic by admin

Windows API example in Visual Basic number 2

Displaying the Date and time using the API

This simple API example we are going to get the date and time

Start up Visual Basic as usual and enter the code below in the general declarations section of the form.

Option Explicit

Private Declare Sub GetSystemTime Lib “kernel32″ (lpSystemTime As SYSTEMTIME)
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type

Now enter the following in the Form_Load event procedure

Private Sub Form_Load()
Dim SysTime As SYSTEMTIME
Form1.AutoRedraw = True
GetSystemTime SysTime
Me.Print “The System Date is:” & SysTime.wMonth & “-” & SysTime.wDay & “-” & SysTime.wYear
Me.Print “The System Time is:” & SysTime.wHour & “:” & SysTime.wMinute & “:” & SysTime.wSecond
End Sub

Now run the program (Press F5) and all going well you should see the date and time of your system. Here is what was displayed
on the test system.

Analysis

The first thing is a user defined type SYSTEMTIME, this contains eight members all of which are integers. As you can see from the code it is
fairly easy to see due tothe naming used what these members are used for
Next we insert the GetSystemTime API function which is used to retrieve the memory information.

Now in the Form_Load event we declare various code, the first snippet declares a variable SysTime of type SYSTEMTIME. The SysTime
variable is passed to the GetSystemTime API function which fills in the information in to the SYSTEMTIME user defined type.
We then display the date and time on the form.

Further Projects

Using a timer build a digital clock.
Put the time in atext box,on the Form’s caption and on the status bar.

A windows API example

Posted on February 27th, 2008 in Visual Basic by admin

windows API example, findmemory available on your system
Windows API example in Visual Basic

Displaying the total memory on the system.

This simple API example we are going to get the total memory available on the system.

Start up Visual Basic as usual and enter the code below in the general declarations section of the form.

Option Explicit

Private Type MEMORYSTATUS
dwLength As Long
dwMemoryLoad As Long
dwTotalPhys As Long
dwAvailPhys As Long
dwTotalPageFile As Long
dwAvailPageFile As Long
dwTotalVirtual As Long
dwAvailVirtual As Long
End Type

Private Declare Sub GlobalMemoryStatus Lib “kernel32″ (lpBuffer As MEMORYSTATUS)

Now enter the following in the Form_Load event procedure

Private Sub Form_Load()
Dim memStat As MEMORYSTATUS
GlobalMemoryStatus memStat

MsgBox “Total memory : ” & memStat.dwTotalPhys / 1024 / 1024 & “megabytes”
End Sub

You should now have a layout like this

Now run the program (Press F5) and all going well you should see the total memory of your system. Here is what was displayed
on the test system.

Analysis

The first thing is a user defined type MEMORYSTATUS, this contains eight members all of which are Longs. The member we are interested in
is dwTotalPhys.

Next we insert the GlobalMemoryStatus API function which is used to retrieve the memory information.

Now in the Form_Load event we declare various code, the first snippet declares a variable memStat of type MEMORYSTATUS. The MEMSTAT
variable is passed to the GlobalMemoryStatus API function which fills in the information in to the MEMORYSTATUS user defined type.
We then display the memory size in a msgbox . The result is in bytes so we divide it twice by 1024 to first convert to kilobytes and then megabytes,
you could of course do this again if you have lots of memory in your system.

Further Projects

Display all of the MEMORYSTATUS members on a form.
Build a resource meter which shows features such as memory load and page file usage.