Click here to Skip to main content
13,249,813 members (86,764 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked
Posted 20 Mar 2011

Download a Google Map with Win32, C++ and WinInet

, 19 Apr 2011
Rate this:
Please Sign up or sign in to vote.
Using pure Win32, WinInet, and STL Vectors, we open an Internet connection, download, and display a Google map on a Windows Mobile phone.


While working recently on an embedded Windows CE project (no, it’s not dead, at least not yet), a colleague was tasked to read JPEG and PNG images from the file system, but he needed to convert these to pure Win32 HBITMAP images to match the signature of a 3rd party library function he needed to call. I suggested he use the COM IImagingFactory to load pictures into an IImage object, then simply draw them into a GDI device context and extract the resulting bitmaps from that. He was unsure how to do this, so over the course of 4 hours and a few bottles of Belgian ale, I wrote a little sample for him.


To make the project a bit more interesting and to show him the code in working form, I decided to write the sample for a Windows Mobile 6.5 phone I could bring into the office. Rather than simply read the images from the file system or as an embedded resource, I extended the sample a bit to solve a common problem, namely, reading Google maps over the wireless Internet.

Here are some screen shots from the completed application. The first simply shows the program’s instructions, which are to select a city to display from the Help->GetMap menu, and the second shows the retrieved Google map of Seattle displayed on the screen. Note that these screen shots were taken from the Windows Mobile 6.5 QVGA emulator, launched from Visual Studio, and that you must have network connectivity routed through the emulator in order to actually download and display a map.

This is easily accomplished by selecting the Configure choice from the emulator’s File menu, clicking on the Network tab, and checking the “Enable NE2000 PCMCIA network adapter and bind to:” checkbox, with a valid network adapter selected in the comboBox. There are plenty of articles on the net that show how to configure your Windows Mobile emulator for Internet connectivity, so I won’t really delve into that here.

Figure 1 - Start Screen
Figure 2 - Seattle Map

Using the Code

The code is very well documented internally and illustrates a number of interesting and useful techniques such as using a std::vector to hold pointers to chunks of dynamically allocated memory used to read a Google map image from the Internet, using the IImagingFactory to convert this memory into an IImage, then drawing the subsequent image in a device context and returning the HBITMAP through a C++ reference.

Other useful bits are creating and destroying fonts, centering and drawing text and images on the screen, and registering/responding to a Windows CE SH_UIMETRIC_CHANGE message used to signal the user’s desire to change the default system font size.

For convenience, we use the ATL CString object, which is very much like MFC’s CString, in the DisplayInstructions function and I show how to use Win32’s GetTextExtentPoint32 to compute the pixel size of the string so we can display it in the center of the screen.

The most interesting function is GetGoogleMap, which shows how to establish an Internet connection on the phone, connect to, and retrieve the map image.

Points of Interest

Even though Windows Mobile 6 has been eclipsed by Windows Phone 7, I find it amusing that at the time of writing (March, 2011), the older Windows Mobile models are still outselling Windows Phone 7 by a factor of 2 to 1. Microsoft’s decision to destroy their Windows Mobile developer ecosystem by banning native code development from their new platform hasn’t yet translated into anything resembling success, but has simply motivated most of the experienced C++ developers to move to Android, iPhone, or RIM. But, Microsoft has no choice but to continue their mobile efforts as the size of the smartphone market grows and the use of desktop and laptop platforms continues its slow decline.


  • 20th March, 2011: Initial post
  • 24th March, 2011: Updated the GetGoogleMap function to release the screen device context once the compatible bitmap has been created
  • 14th April, 2011: The GetGoogleMap function was changed. Previously, we allocated a new IOVEC structure for each chunk of a file read from the Internet and pushed it into the readBufferArray vector. However, when you push_back a struct or object on to a vector, you are actually making a copy so there is no need to dynamically allocate a new IOVEC structure for each block as we were doing before. This caused us to leak IOVEC structs that were never deleted. Now, we use a single IOVEC struct created on the stack to hold a pointer to a chunk of dynamically allocated memory and this single struct is pushed onto the readBufferArray vector each time a chunk is read, eliminating the leak of dynamically allocated IOVEC structs.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Paula Scholz
Software Developer (Senior)
United States United States
I write WPF and Universal Windows Platform software.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
code_junkie26-Apr-11 7:24
membercode_junkie26-Apr-11 7:24 
GeneralMy vote of 5 Pin
Mihai MOGA14-Apr-11 3:54
memberMihai MOGA14-Apr-11 3:54 
Generalgood Pin
vjedlicka6-Apr-11 23:37
membervjedlicka6-Apr-11 23:37 
GeneralMy vote of 1 Pin
Garth J Lancaster25-Mar-11 16:47
memberGarth J Lancaster25-Mar-11 16:47 
GeneralRe: My vote of 1 Pin
Paul Scholz26-Mar-11 5:40
memberPaul Scholz26-Mar-11 5:40 
GeneralMy vote of 5 Pin
yafan21-Mar-11 10:41
memberyafan21-Mar-11 10:41 
GeneralMy vote of 1 Pin
imagiro21-Mar-11 9:11
memberimagiro21-Mar-11 9:11 
GeneralRe: My vote of 1 Pin
Paul Scholz21-Mar-11 9:15
memberPaul Scholz21-Mar-11 9:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 19 Apr 2011
Article Copyright 2011 by Paula Scholz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid