Export (0) Print
Expand All

Some Very Smart Software

John Kennedy
Microsoft Corporation

April 23, 2002

Great news, fellow Pocket PC developers! No, Drew Barrymore hasn't dropped the restraining order against me—it's even better than that. The Smart Device Extensions plug-in for Visual Studio® .NET has just been released as a beta, and is now available for immediate download.

To refresh your memory, the Smart Device Extensions (SDE) is a plug-in that brings the power of .NET to the Pocket PC. In other words, it's a version of the .NET Frameworks that runs in the limited memory space of a device like a Compaq iPAQ or Hewlett-Packard Jornada. For more of an introduction, take a look at my earlier column Smart Thinking.

The beta release is freely available from www.betaplace.com, and I urge you to go visit and download it before you get to the end of this paragraph. However, there is a catch. You'll also need a copy of Microsoft Visual Studio .NET in order to use the SDE plug-in. You cannot use the Visual eMbedded Toolkit with the Smart Device Extensions. This mightn't be as painful as you might think, for although Visual Studio .NET is not free, you don't need to buy the full on, wallet-wincing version. Microsoft has very kindly made language specific versions available for something of a bargain price. There are three language releases available: C++, C#, and Visual Basic. Unfortunately C++ won't support SDE at this time, so pick from C# or Visual Basic. I hope you'll go for C# so you don't defect to Larry's column and the dark side of the force!

.NET and How You Can Use It

If you're anything like me, you are probably thinking, "So this .NET stuff, what can I really do with it that I couldn't do before?" You might also be thinking, "Why won't my so-called friend Sean get me Drew's autograph like he said he could?" And maybe even, "I wonder if I should visit the doctor about this uncomfortable itching?" Of course if you were thinking these things, it would be really spooky.

That still leaves the question, "What good is .NET to a Pocket PC developer?" Well, I hope to answer that question in this month's column by providing a simple demonstration.

As you might expect if you know even a little about .NET, using the SDE will bring with it the strange new world of Managed Code. Developing in a language like Visual C#™ or Visual Basic using the SDE brings the joy of automatic garbage collection and a hundred-and-one other funky things designed to make your code more reliable, which is always a good idea on a Pocket PC device.

However, even more exciting is the availability of the .NET Frameworks, or more accurately, the Compact Frameworks. The Frameworks is a comprehensive Class library stuffed full of timesaving powerful features, such as Web services.

Do you know what a Web service is? Don't answer that—it's mostly a rhetorical question, and if you really are like me, you're not supposed to talk to the computer out loud anymore as part of that work settlement we don't talk about any longer.

If I had to describe a Web service in one word, it would be cool. I've always been intrigued by the idea of combining what little programming skills I posses with the World Wide Web, and somehow becoming incredibly rich. Creating Web sites that are smart and make use of real heavy-duty programming in the background are always in demand. "You can't make a Web site that's too fast or too smart," my wise old mother would say if she were here. Passing the storage and processing buck from the Pocket PC to a considerably more powerful desktop or server PC also opens up some intriguing possibilities.

Now imagine if you could write those kinds of Web sites in a language such as C# or Visual Basic, and yes, even Visual C++. Neat, huh? But it gets better. Imagine you could write an application that could use Web protocols to interact with that Web site—and by interact I mean work in such a way that your client simply calls functions stored on the server. Now we're talking, right? Those server functions could be doing all kinds of things from accessing SQL databases to generating images on the fly while simultaneously talking to a thousand other clients programs.

I hope you're starting to get the idea because this is exactly what the Compact Frameworks does for your Pocket PC. With the SDE, you can write applications in Visual C# or Visual Basic that can make talking to these Web services remarkably easy. Not just smart Web pages, but applications that can query databases, sync information, and all that lovely stuff that gets IT departments all warm and fuzzy.

If you haven't got dollar signs in your eyes yet, picture yourself walking into your favorite corporation, whipping out your iPAQ, linking it over Bluetooth to your mobile phone, and showing them how your Pocket PC can query their massive inventory database—all the while knowing the core of code was written in less than one afternoon.

Is it really that easy? Yes it is! How do I know? Because I've written a Web service and a Client before my double latte even cooled enough to down in one gulp. Am I going to show you how? Definitely! Will I stop asking these irritating questions? Well, all right then.

Walking Through a Web Service, Part 1

Let's start with the Web Server part. Using Visual Studio .NET, we're free to pick the language we're most comfortable using. Let's go with Visual C# just to get the practice. I've the feeling we'll all be at least dabbling in C# sooner rather than later.

Starting couldn't be easier, as there's a project type all ready for us to use—the ASP .NET Web service. We can, therefore, create a new project and let Visual Studio do all the hard work. That's what I like—software that does the thinking for me.

Figure 1. Creating the Web service gets off to an easy start with the New Project wizard in Visual Studio .NET

The function we're going to add to the Web service resides in a file called Service1.asmx, and when you switch to the code view you'll see something like Figure 2 below. It's surprisingly non-scary code for the degree of magic that it performs.

Figure 2. All the code for a working Web service is on this page. It's written in C#, but it's surprising how quickly this new language loses its scary image and becomes legible.

Let's be lazy and stick to the built-in Hello World example by simply removing the comments in this wizard-generated code. All the code does is define a single function that returns the string "Hello World." While hardly state-of-the-art, it's more than enough for us to understand how things work.

Once the comments hiding the function are deleted, we can even compile and run the Web service on the development machine. All we have to do is hit F5 and the code will be compiled for us. Visual Studio will then launch a browser window that describes the function, and makes it possible to invoke it with a click. As you can see from Figure 3, it also reminds us to change the namespace to that of our computer, a trivial change that only requires the addition of a single line of code.

Figure 3. Visual Studio makes it easy to test the Web service by compiling and launching a Web browser window

Amazingly, that's all there is to making a Web service! I couldn't believe it myself when I first tried it, as I'd always thought it would be complicated with lots of configuration files and other spells to take care of.

Walking Through a Web Service, Part 2

Now let's get on with the client program. This is the part we're going to write with the SDE plug-in, and again, we'll use Visual C# as the development language. Again, the New Project wizard will do all the hard work for us. We only need select C# Projects, and then Smart Device Application from the available project types, as shown in Figure 4.

Figure 4. Creating the client program

We then select a Pocket PC Windows Application, and the wizard creates a skeleton project for us, complete with empty form. The form-based application model is still pretty new to us Visual C++ programmers, but it's growing on me.

For this example, all we need is a button and a text label dragged onto the form, as shown below.

Figure 5. Adding UI elements to SDE programs is a drag-and-drop affair

You can probably guess that we're going to link the button with that function we defined on the Web service side. However, at this point, our client program has never heard of that Web service so we need to introduce them. We can do this quite easily using the Add Web Reference wizard. Tell the wizard the name of the Web service, and it will go looking for it, and pull in all the details itself. It will then generate the source code required to make it straightforward to add reference to that remote function.

Figure 6. The Add Web Reference will talk to the Web service and discover any available functions. It'll then generate the source code we need.

Now we can use that function from the button. Double-click the button in the Forms Designer, and Visual Studio will generate the function. Then add the following C# code:

Private void button1_Click(object sender, System.EventArgs e)
   Service1 myService = new Service1();
   label1.text = myService.HelloWorld();

The code creates an instance of the Web service, and then calls the HelloWorld function. The string of text it returns is used to set the value of the text label on the form. The end result is that when you tap the button, the Pocket PC talks to the Web service using standard Web protocols (HTTP, using some more magic called SOAP), and gets the string, and then displays it. And it only took two lines of code in the client program!

Figure 7. The working Client programone tap and the Web service is called. That's the power of .NET and the Compact Frameworks!

The End of the Beginning

I hope that quick walkthrough impressed you as much as it did me. The fact that it is so easy to create a potentially very smart client program for your Pocket PC device should get you excited enough to download the beta and give it a try for yourself.

Bear in mind that this release is still a beta. I've already tripped over a few little bugs and found some implantation details missing. However, that's the point of a beta, so make sure you let Microsoft know what features you need to make your favorite applications work. For example, at the moment I'm experimenting with the MapPoint .NET Web services and the Compact Frameworks. I'm thinking of the possibilities when you combine a GPS receiver and wireless Internet access. Real-time street maps on your Pocket PC are only the start of what's possible.

This is only the beginning of a great new way of developing Pocket PC applications. Sadly, it's also the end of this column. See you next time!

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.

© 2014 Microsoft