February 1999

Creating Windows CE Apps with Visual Basic

by Scott B. Lewis

Windows CE is the next generation operating system from Microsoft. It's a 32-bit OS that visually resembles the Windows 95 interface, as seen in Figure A, but was built from scratch.

Figure A: Windows CE shares the familiar Windows 95 interface, while sporting a brand new code base.
[ Figure A ]

It also has a rich subset of Win32 architecture built in, which provides developers with very familiar API calls.

Currently, Windows CE machines come in a variety of devices, including PalmTops, HandHelds, Auto-PCs, and cell-phones. In this article, we'll discuss the power and limitations of the new SDK and toolkit for developing Windows CE applications with Visual Basic. Along the way, we'll build a useful notepad-type application for Windows CE.

Writing apps for Windows CE

Currently, there are three serious tools available for Windows CE software development from Microsoft. These include Visual J++, Visual C++, and Visual Basic.

Visual J++ is powerful, but has a major drawback when it comes to distributing the final code. The runtime files are around 4MBs; that takes up quite a lot of room, even before your application starts.

Visual C++ comes with all the power, prestige, and maturity of the Win32 development arena, and new workspaces have been added for Windows CE projects. However, although Visual C++ apps are the smallest in distribution size and the fastest in speed, C++ comes with a steep learning curve, which few are determined to conquer.

That leaves us with Visual Basic. Since more than 50 percent of Windows developers in Information Systems are using Visual Basic, it seems that Microsoft's new Windows CE Software Development Kit (SDK) for Visual Basic 5.0 (VBCE) is a very logical choice for Windows CE development.

The VBCE toolkit

The Windows CE SDK includes the underlining emulation environment, sample code, utilities, and documentation for Windows CE. There is also a VBCE toolkit that enables VB developers to easily create CE applications using the VB Integrated Development Environment. Before we start looking at the VBCE toolkit and some of it's new forms and features, let's talk about some of the necessities for using this VBCE add-in. If you intend to perform debugging, either in emulation mode or remotely on the actual CE device, Windows NT (Workstation or Server) is needed for UNICODE support. Serious developers will definitely want these features. So, to play it safe, if you want to develop Windows CE applications using Visual Basic, stick with NT 4.0.

In addition, the VBCE toolkit only works with Visual Basic 5.0. Be aware that the add-in doesn't work with the recently released VB 6.0 version. Microsoft has plans to release an updated version for compatibility with Visual Basic 6.0 in the near future.

Some VBCE limitations

Although VBCE uses the Visual Basic 5.0 shell for development, it doesn't use the same VBA-type code. VBCE uses a combination of VBA, modules, and VBScript, which is a subset of the VB language and is interpreted. One big drawback of using VBScript is getting used to the fact that your only variable type is Variant. Check out msdn.microsoft.com/scripting/ for more information.

VBCE projects can include only one basic module, but more than one form is supported. Be aware, though, that each form you put in your project slows down performance and increases the size of your project and code base. Class modules are not supported.

Not until recently did VBCE have database support; originally, the only type of external file format supported was text files. To correct this, Microsoft has recently released an ActiveX control for ADO (Active Data Objects) for VBCE development. This allows developers to create sophisticated SQL queries and relationships. You can find more information at www.microsoft.com/windowsce/downloads/pccompanions/adosdk.asp.

Win32 OCX controls don't work in Windows CE; special compiled ActiveX controls for Windows CE are needed. Check out Microsoft's CE Web site and download the ActiveX control pack for VBCE. You can find more information at http://www.microsoft.com/windowsce/downloads/pccompanions/actxconpak1.asp. This control pack adds valuable controls such as a Tab, Listview, Common Dialog, and enhanced file controls.

Installing VBCE

Installing the VBCE Software Development Kit is quite simple: just running the setup from the CD will install it. The VBCE toolkit will automatically be installed for you. In addition, Windows CE services will be installed which allow you to connect and manage the handheld device as if it were on your computer.

During the installation of the SDK for Windows CE, many utilities also get installed. Among these are CE Zoom, CE Control Manager (more on that later), Spy, Registry Editor, and Process Viewer. CE Zoom allows screen snapshots to be taken of the CE device from the connected laptop/desktop machine. The Registry and Process Viewers allow you to view and manipulate data on the remote device. Be sure to learn each of the tools that come with the SDK, as they will become an integral part of your Windows CE development life cycle.

I want my VB IDE!

After successfully installing the Windows CE toolkit for Visual Basic 5.0, you'll notice that your Integrated Development Environment in VB has changed slightly. Launch VB5, and we'll take a look at the first of many changes.

Notice in Figure B that a new project type has been added called Windows CE Project.

Figure B: Once the VBCE toolkit is installed, a new project type will be available--Windows CE Project.
[ Figure B ]

Choosing this project type will configure aspects of the new IDE to support CE development. Double-clicking on the Windows CE Project will bring you to the condensed Project Properties dialog box, as shown in Figure C.

Figure C: The Project Properties dialog box for CE projects allows you to set target mode, local path, and remote path for applications.
[ Figure C ]

The General tab of the Project Properties dialog box allows you to set the local path for the compilation of the CE application. The remote path is where the application will reside on the CE device. The Run On Target frame allows you to set whether you want the application to run on the desktop machine in emulation mode or on the actual CE device. In the interest of speed, most of your testing/debugging should be performed in the emulation environment. Copy the settings in Figure C, substituting your own information where appropriate.

Creating a notepad application

Now let's start creating a form and VB code for re-creating the well-known notepad application for Windows CE. First, rename the default Form1 to frmNotepad using the Project Properties dialog box. In order to use the file controls for CE, you must add the controls to your project. Under the Project menu, choose Components and add the Microsoft CE file System Control to your project. Then add the settings shown in Table A and the menu structure in Table B to create the form shown in Figure D.

Table A: Form settings
Control Name
Textbox txtNotepad
Textbox txtFilename
Label lblFilename
CE File fsNotepad

Table B: Menu structure for the form
Caption Name
File mnuFile
New mnuFileNew
Open mnuFileOpen
Save mnuFileSave
Exit mnuFileExit
Help mnuHelp
About mnuAbout

Figure D: We'll use this form for our Notepad application.
[ Figure D ]

Up to this point, there hasn't been too much difference between creating forms in Windows CE and in VB 5.0. Now, add the code in Listing A to frmNotepad.

Listing A: Code for frmNotepad


Private Sub mnuFileExit_Click()
	'-- Proper way to end a VBCE application -
	'-- UNLOAD forms does not exist in VBCE.
	 App.End
End Sub

Private Sub mnuFileNew_Click()
	 Dim iRet
	'-- Question user if they want to clear text box.
	iRet = MsgBox("Clear document?", vbQuestion + _ 
		vbYesNo, "New")
  
	'-- If user clicked YES, then clear text box.
	If iRet = vbYes Then
	'-- Clear text box.
		txtNotepad.Text = ""
	End If
End Sub

Private Sub mnuFileOpen_Click()
	On Error Resume Next  
	'-- Open file.
	fsNotepad.Open txtFilename.Text, 1
  
	'-- If file opened successfully, write data to 
	`-- file.
	If Err.Number = 0 Then
		'-- Clear text box.
		txtNotepad.Text = ""
    
	'-- Loop through file.
		While fsNotepad.Loc < fsNotepad.LOF
			txtNotepad.Text = txtNotepad.Text _
			&	fsNotepad.LineInputString _
			& Chr(13) & Chr(10)
		Wend

	End If  
  '-- Close file.
	fsNotepad.Close
End Sub

Private Sub mnuFileSave_Click()
	On Error Resume Next 
  '-- Create file.
	fsNotepad.Open txtFilename.Text, 2
  
	'-- If file created successfully, write 
	`-- data to file.
	If Err.Number = 0 Then
	'-- Write data to file.
		fsNotepad.LinePrint (txtNotepad.Text)
	End If
  
	'-- Close file.
	fsNotepad.Close
End Sub

Private Sub mnuHelpAbout_Click()
	'-- Display Message Box for About information.
	MsgBox "ceNotepad" & Chr(13) & Chr(13) & _
	"http://www.vcce.com", vbOKOnly + vbInformation, _
	"About ceNotepad"
End Sub

Preparing to run your application

Now that all the code is entered, your application is almost ready to go. You must now prepare either your emulation environment, or the remote CE device, to run your application. There are two things that need to be performed for your applications to work: download the Visual Basic runtime files for Windows CE, and download the necessary controls for your application.

As in any typical VB application, VBCE apps come with their share of runtime DLLs. Under the new Windows CE menu, choose the Download Runtime Files command. VBCE will download various DLLs to the target environment selected in the Project Properties dialog box. Note that the first time you run your application, VBCE will first check if all necessary runtime files are on the CE device. If there are any runtime files missing, they'll be downloaded automatically.

In order to confirm that all controls are downloaded, use the Control Manager application that comes with the Windows CE SDK, shown in Figure E.

Figure E: The Windows CE Control Manager allows you download and install the controls you need for your applications.
[ Figure E ]

Choose Control Manager from the Windows CE menu, and then select the File Class control. Now, choose either the Emulation or Device menu, depending on your target system. Choose Install to download the control to your system. If everything goes right, and your installation is a success, you should see the message shown in Figure F.

Figure F: Here's the message you'll get when your installation is a success.
[ Figure F ]

Tip: One of the most common errors in VBCE development is forgetting to download the controls you need for your application. Be sure to check the Control Manager each time you try to download an application.

Debugging

Writing software naturally introduces bugs during your development cycle. Standard VB 5.0 Win32 applications come with a professional, full-featured debugger. VBCE applications are written using VBScript and, therefore, can't use the standard VB debugger. However, the VBCE debugger comes with many useful features including breakpoints, watches, a step-into, and an Intermediate window.

In order to create a debugging session in VBCE, go into the Make tab of the Project Properties dialog box and select Build Debug. Note that, unlike VB, in the VBCE debugging environment you can't make changes to the code as it executes.

Where's Windows CE going?

The VBCE toolkit has a lot to offer, but it's still in its infancy. The current version of VBCE, 1.0, has many drawbacks and limitations. Although the Visual Basic 5.0 add-in makes developing Windows CE applications easy, the lack of features can seriously dampen your development effort. Although VBCE has much promise, the lack of Win32 CE API support makes writing large professional applications quite complicated without the use of many third-party controls. Hopefully, by the time the next version of VBCE is released, many important features will be added to the environment, including VB 6.0 support and access to the CE Win32 API set. These additional features will make VB a sensible platform for large-scale Windows CE development.

Conclusion

Windows CE is an up-and-coming operating system, and it's starting to appear in more and more places. Soon enough, cable boxes will be running CE; maybe even washing machines and refrigerators will soon include this new operating system. Microsoft has big plans for Windows CE as it applies itself to handheld devices and the future of Windows.

Just as all software products mature through new versions, the Visual Basic toolkit will soon rival other professional toolkits for Windows CE. If you're a VB, VBScript, or VBA developer, get ready to jump on the new, smaller, CE device bandwagon.

Copyright © 1999, ZD Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD Inc. Reproduction in whole or in part in any form or medium without express written permission of ZD Inc. is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners.