Going Mobile with eMbedded Visual Basic
May 9, 2001
Download Hello World.exe.
This month I'm coming to you from the Silicon Village, the epicenter of mobile application development. For those of you that are uninformed, the Silicon Village sits in southeast Michigan and offers what every roaming code hack is in search of—11-MB wireless network connectivity on the street. Yes, you heard me right, mobile, 11-MB networking available everywhere. I'm not talking about within the buildings. That's for those eVC++ mobile wannabes. I'm talking about on the street, where the eVB crowd hangs. Walking over to Subway for lunch? Hey, take your iPaq, a PC card sleeve and a Lucent wireless card along and you've got smokin' Internet access in the palm of your hand. Worried, that you might miss an iPaq sale on eBay? That is not a problem in the Silicon Village where you are always on the 'net.
But, enough of the travel guide. Okay, so you have decided to cast aside your comfortable corporate day job to laugh heartily in the face of danger, and embrace the lifestyle of a mobile application developer. I'm proud, and at the same time, worried for you. Ahead lies the frontier traveled by nomadic coders, a group that resembles extras from a Mad Max sequel only with way too many electronic gizmos hanging from their belts.
Your development tool of choice is Microsoft's eMbedded Visual Basic® (eVB), the gateway to creating software masterpieces that fit into the palm of your hand.
If you are an experienced Visual Basic developer, eVB is going to be a piece of cake, well maybe a cookie, or one of those small biscuit things, but certainly something sweet.
In this article, I'll give you the guided tour through the process of creating your first eVB application. You will learn first hand everything you need to know to rush headlong into creating, coding, and testing applications using eVB.
The 2001 eVB Tour
As a seasoned Visual Basic developer, upon firing up eVB you will find yourself in an interface that makes you feel right at home. With the exception of some changes to the menu and a few missing tools on the toolbar, everything else is where you would expect it to be.
There are a few key things that I need to point out, though. First is the Project menu. You will probably notice right away that there are a whole lot of things missing. In fact, it is missing most of what you would expect to find under this menu in Visual Basic. There are no class modules, user controls, data environments, or anything else other than forms and standard modules. You may be asking, "What's up with that?" The answer is simple—they aren't supported under eVB.
At this point I'm going to suggest something that brings chills to the heart of even the most experienced coders—read the documentation that comes with eVB. I know, you're thinking "Whoa, take a step back Roof. Don't get all radical on us. I wouldn't read documentation even if it meant that I would have to hang out with users in my spare time." You're going to have to trust me on this one. There are enough differences between eVB and Visual Basic that if you don't read the documentation you are going to feel like you have George Bush as your only lifeline on Who Wants To Be A Millionaire.
As long as we are talking about menus, let me point out the Tools menu. First, you will notice that it looks nothing like the Visual Basic Tools menu. Once you get past that though, you will find a set of helpful utilities with which you will want to familiarize yourself. Take some time to check out the registry editor, control manager, file viewer, and zoom. The remaining tools are more for those bit-twiddling, "I don't care how many lines of code I need to write," C++ wonks. You know, the type that read my partner John's columns.
SDK Is Not a Four Letter Word
If you are going to work with eVB, you've got to know a bit about SDKs. Along with installing eVB, you will want to install one or more Windows® CE platform system developer kits (SDK). Now these sound a lot worse than they are. Each SDK provides the runtimes, ActiveX® controls, and emulator for use with specific Windows CE platforms.
Without doubt, the most interesting component of an SDK is the emulator. Having an emulator means that you don't have to have a device. You can do all of your development and testing within the comfort of your own PC. That said, get a device. How cool of a mobile dude, or dudette, are you going to be lugging around your desktop PC?
The First Stop on the Mobile Tour
Well enough of the small talk, it's time to get to work on your first eVB application. For the remainder of this article, I will lead you through the process of building your first eVB application and show you how to test it in an emulator and on a device.
The "Hello, Isn't eVB cool" Application
The application you will be building in this article is a hybrid of the popular and grossly overused "Hello World" application. What makes this application special is that it doesn't just display the text "Hello World," it scrolls the message like a little, portable marquee board. Now, I know what you are thinking. Unlike the traditional "Hello World" application, this one has commercial potential. Simply allow the user to enter in a message and they will have their own handheld advertising mechanism. And if you can sell this software for only, say 10 bucks, you only need to move 100,000 copies, and yes, you will be a millionaire. But I get ahead of myself. Let's continue with the application.
Starting a New Project
The first step to perform is to create a new project. You can do this by performing the following steps:
- Start eVB. The New Project dialog will be displayed.
- Select the Windows CE for the Pocket PC template and click the Open button.
A new project will be created and loaded into the eVB design environment.
Building the Interface
Using the Form window and the Toolbox construct the user interface comprised of a text box, command button, and a timer control as shown in the figure below.
Figure 1. The Initial Hello World form
Configure the properties of the form and controls using the following table.
|- Caption||Hello World|
|- Font||Tahoma 22|
|- Text||(empty string)|
|- Caption||Change Direction|
Adding the Code
In the Project window, select Project1. In the Properties window change the Name to "HelloWorld."
Open the Code window for the form module. Add the following lines of code to the General Declarations section of the module:
Dim strDirection As String Dim strMessage As String Dim strTemp As String
The variable strDirection will be used to track which direction the marquee is scrolling. The variable strMessage will be used to hold the message that will be displayed in the scrolling marquee display. The variable strTemp is used to manipulate the message that is being displayed in the marquee.
Insert the following code in the Form Load event procedure:
Private Sub Form_Load() ' Initialize the operating variables. strDirection = "forwards" strMessage = "Hello, isn't eVB cool?" strTemp = String(30, Asc(" ")) & strMessage ' Start the marquee. ceTimer.Enabled = True End Sub
This code first initializes the three string variables used throughout the program and then turns on the Timer control, which in turn runs the marquee.
Insert the following code in the Timer control's Timer event procedure:
Private Sub ceTimer_Timer() ' Depending upon what direction we are scrolling, take ' either the left or right-most character off of the ' string and append it to the opposite end. If (strDirection = "forwards") Then strTemp = Right(strTemp, Len(strTemp) - 1) & _ Mid(strTemp, 1, 1) Else strTemp = Right(strTemp, 1) & Mid(strTemp, 1, _ Len(strTemp) - 1) End If ' Display the string, giving the feeling that the ' text is scrolling. txtDisplay.Text = strTemp End Sub
This code starts by determining which direction the marquee is scrolling. It then makes use of the eVB Right, Len, and Mid functions to manipulate the string that is displayed, which gives the scrolling marquee look to this application.
Insert the following code in the Click event of cmdChangeDirection control.
Private Sub cmdChangeDirection_Click() ' Toggle the directional flag. Select Case strDirection Case "forwards" strDirection = "backwards" Case "backwards" strDirection = "forwards" End Select End Sub
These few lines of code handle toggling the flag used to track which direction the marquee is scrolling.
That's it. The construction of your first application is complete.
Testing Your Application in the Emulator
eVB provides two methods for testing—on the device or in an emulator. Emulators, which run on your development PC, are installed along with the Pocket PC and H/PC Pro SDKs. These emulators allow you to test your applications without requiring you to have a device. Emulators do have a downside. They can mislead you as to how your application is going to work on a device. Your PC is faster than most devices and it has a keyboard for data entry. Another thing is that the emulators only run under Windows NT® and Windows 2000.
Whether your application runs in the emulator or on a device is controlled through the Project Properties dialog box. To configure your application to run in the emulator, perform the steps below.
Under the Project menu click HelloWorld properties. If you don't have the HelloWorld properties selection at the bottom of the Project menu, it is because you did not set the project name as mentioned earlier in this article.
The Project Properties dialog will be displayed as shown in the figure below.
Figure 2. Project Properties dialog box
Using the Run on Target combo box, select Pocket PC Emulation.
The Remote Path configuration defines where your application will be loaded on either the emulator or attached device. The configuration of \Windows\Start Menu\ causes your application to show up in the Start menu of the target environment.
Note Paths for Windows CE are similar to those under Windows, with the exception that they do not contain a drive letter.
Click the OK button to close the Project Properties dialog.
You are now ready to run your application in the emulator. To start your program, perform the following steps:
From the Run menu click Execute. After a brief pause the Pocket PC emulator will start, then your application will load and run.
Tap the Change Direction button to switch the direction the marquee is scrolling.
Tap the OK button in the upper-right corner of your application running in the emulator. Your application will stop.
To close the emulator, click the round, black button on the lower-left corner of the emulator.
Note If you are going to be performing repetitive tests in the emulator (yea, like we build and test only once), it is quicker and easier to leave the emulator running between tests.
Testing Your Application on a Device
To test your application on a device, perform the following steps:
- Confirm that your Pocket PC is connected to your desktop PC and is communicating.
- Under the Project menu, select HelloWorld properties. The Project Properties dialog will be displayed.
- Using the Run on Target combo box, select Pocket PC (Default Device).
- From the Run menu, click Execute. After a brief pause, the Pocket PC will display a series of dialog boxes as eVB components that are copied to your device. Your application will load and then run.
- Tap the Change Direction button to switch the direction the marquee is scrolling.
- Tap the OK button in the upper-right corner of your application running on the device. Your application will stop.
Creating an Installation Routine
Okay, if you are going to mass market your new crankin', kicked-up, scrollable Hello World application, you better think about creating an installation routine. Lucky for you, eVB comes with the Application Install Wizard that walks you through the process of creating setup routines.
I'll walk you through the process of creating an installation routine for your application.
- From within eVB, under the Tools menu, select Remote Tools and then Application Install Wizard. The initial page of the wizard will be displayed. Read it if you like. If you are like the rest of us, just go on to step 2.
- Click the Next button. On the next page, enter the path to your eVB project. Your project file has an extension of .ebp.
- Click the Next button. On the next page, enter the path to your eVB application file. Your application file is your program. In eVB they have the extension of .vb. If you haven't already done so, you can create an application file from within the eVB environment by selecting Make under the File menu.
- Click the Next button. On the next page, enter the path to the directory that you would like to use to hold your setup files. You may not want to put this in the same folder as your application as setup can create multiple files and folders.
- Click the Next button. On the next page, select the processor types that you plan to support with your application. The items that appear in this list are dependent upon what project type you specified when you started the project. For example, if you started with the Pocket PC template, you should see four processors listed on this page.
- Click the Next button. On the next page, select any additional ActiveX controls or References that you want installed with your application. Typically, you will not have to select anything on this page of the wizard unless you are using an ActiveX control or Reference that does not provide a desktop component. The wizard is smart enough to glean the rest from your project file.
- Click the Next button. On the next page, add any additional files that will be used with your application. Typical files that you may want to include are readme documents, empty database files, and other auxiliary files.
- You can choose to deselect the option to include the Device Runtime files if your target devices already has these files. This will cut down on the size of your setup. All Pocket PCs come with the eVB runtime in ROM.
- Click the Next button. On the next page, enter installation information as is appropriate for your application.
- Click the Next button. On the next page, note that the end user can override the installation directory you provide here during the installation process. This directory will be created under the Program Files folder on the target device.
- At this point, you are ready to create your installation. Click the Create Install button. A number of command windows will be displayed as the installations are created. When they are completed, the interface to this wizard page will change and the Finish button will be enabled.
- Click the Finish button to complete the installation process. The wizard will close automatically.
What the Application Install Wizard Creates
Upon completion of the Application Install Wizard, you will find a number of files and supporting folders in the path you specified the wizard to use. Most of these files were used in the generation of the installation routine and do not need to be sent to the end user.
The only files that you need to send to the end user of your application are located in the CD1 directory. You will need to send both Setup.exe and Setup.ini, along with the appropriate .CAB file that matches the user's device processor type.
Your eVB application file is processor independent. That simply means that the same .vb file can be run on any of the target devices. Other files, like the eVB runtime files and ActiveX controls, are processor dependent. That is why a separate .CAB file is created for each unique processor type.
You should test each installation routine on the appropriate target device to confirm that it correctly installs your application. If you are testing on devices that you use for development, you should first perform a hard reset on the device to restore each to its original factory setting. That way you know that your installation will work on any device of that type.
There you have it; the complete process of building and testing an eVB application. As you can seen, with the exception of working with the emulator and a device, the process of creating applications using the eVB is very similar to creating desktop applications in Visual Basic.
Where to Go from Here
If at this point you are too jazzed up to sleep try checking out the following:
- deVBuzz (www.deVBuzz.com): The place to get started if you are just starting out on the road with eVB.
- The VBCE newsgroup (microsoft.public.vb.vbce): Where the eVB developers hang out to swap ideas, solutions, and war stories.
Back on the Road
That's it for this month. Get out there and build some cool eVB applications. Tell me about your latest creations and your slickest eVB tricks at firstname.lastname@example.org. Don't forget to tune into John's column, right back here in a couple of weeks. Ask him to show you how many lines of code it takes to create the mobile marquee Hello World application in eVC++. Don't be surprised that you'll have to teach your grandkids how to code so that they can finish it up after you're gone.
That's it. I've got my sunglasses on, a six-pack of Coke, and I'm back on the road, heading for the surf in sunny California, where next month I'll show you how to use ADOCE with Pocket Access and SQL Server CE.
John Kennedy is a Technical Writer/Programmer in the Visual C++ group by day and leads a secret life as a Pocket PC developer by night.
Larry Roof is a partner at tonked, a firm that specializes in the development of mobile solutions and training. He's the author of Professional Visual Basic Windows CE, available from Wrox Press.