-Wince programming (3rd edition)-1.3 The first Windows CE Program

Source: Internet
Author: User

The first Windows CE Program
Let's take a look at your first Windows CE program. List 1-1 shows hello1-a simple hello World application written for Windows CE.

Listing 1-1: hello1, a simple Windows Application

Hello1.cpp
// ================================================ ======================================
// Hello1-a simple application for Windows CE
//
// Written for the book programming Windows CE
// Copyright (c) 2003 Douglas boling
// ================================================ ======================================
# Include "windows. H"

//
// Program entry point
//
Int winapi winmain (hinstance, hinstance hprevinstance,
Lpwstr lpcmdline, int ncmdshow ){

Printf ("Hello World/N ");
Return 0;
}
 
As you can see, in addition to the program entry point, the code looks very similar to the classic kernighan and Ritchie programs. After the annotation, there is a line of code

# Include "windows. H"

This file contains a group of files that define windows APIs and their structures and constants.

The entry point of the program is the biggest difference between the program and the Standard C program. As the c Standard portal
Int main (char ** argv, int argc)
For Windows CE, use the standard Windows entry point [NOTE 1]
Int winapi winmain (hinstance, hinstance hprevinstance,
Lpwstr lpcmdline, int ncmdshow );

Build a program environment.
Windows CE is different from windows on the desktop. 1st parameters. hinstance indicates a program instance for other applications, and provides a program instance for Windows API functions that need to verify this exe. The hprevinstance parameter is left over from the old Win16 API (Windows 3.1 and earlier. In all Win32 operating systems, including Windows CE, hprevinstance is always 0 and can be ignored.

The parameter lpcmdline points to a unicode string that contains the command line text. Applications started from the Microsoft Windows CE browser do not have command line parameters, but in some cases, for example, when the system automatically starts a program, the system contains a command line parameter to explain why the program was started. The parameter lpcmdline is one of the differences between Windows CE and XP. In Windows CE, the command line string is a unicode string. In other versions of Windows, the command line string is always ASCII.

The last parameter, ncmdshow, specifies the initial status of the main window of the program. The parameter is usually passed to the program by the browser by the parent process. It describes how to configure the main window of an application. This parameter may indicate that the window is initially displayed as an icon (sw_showminimize), or the display is maximized (sw_showmaximized) to overwrite the entire desktop, or the display is normal (sw_restore) size indicates that the window is displayed on the screen in a standard variable size mode. Other values indicate that the initial status of the window is invisible to the user, or that the window is visible but cannot be an activity window. In Windows CE, the value of this parameter is limited to three States: normal (sw_show), hidden (sw_hide), and non-active display (sw_shownoactivate ). Unless the application needs to force its window to be in a predetermined state, after the main window of the program is created, this value is not modified, but simply passed to the showwindow function.

The next line is the only functional line of the application:
Printf ("Hello World/N ");
Windows CE supports most standard C libraries, including printf and getchar. This line is interesting because it is not like any other part of Windows CE. This string is not Unicode but ANSI. The following is a reasonable explanation of this phenomenon: For the C standard library compiled using the ANSI standard, printf and other string library functions such as strcpy use ANSI strings. Of course, Windows CE supports Unicode versions of these standard functions, such as wprintf, getwchar, and wcscpy.

Finally, use return 0; to end the program.
Other processes can use the Win32 API function getexitcodeprocess to obtain the return value.

Build the first application
To create hello1 from scratch on your system, you can start Ms EVC ++ and select [new] to create a new project on the [file] menu. When you see the dialog box for the new project, you can see the changes in wince programming and standard Win32 programming. You have the opportunity to choose the platforms shown in Figure 1-1. For non-MFC or ATL projects, the first choice is wce Pocket PC applications (for Pocket PC) and wce applications (for other Windows CE systems ). You can also select an appropriate target CPU. For example, select Win32 (wce mipi) to compile the program for the Windows CE platform that uses mips cpu. Regardless of the target device, make sure to select the wce simulator. In this way, you can run the example program in an XP simulator.
Figure 1-1 The Platform List enables EVC ++ to target different Windows CE platforms.

Next, EVC ++ will ask if you want to create an empty project, a simple program, or a hello World application. All examples in this book are empty projects. This is done to prevent the code wizard from adding any additional code to the example. In the [file] menu, select [new] And hello1.cpp to create a new file.

When a proper source file is created for Hello 1 or copied from Cd, select Win32 (wce x86em) debug as the target platform, and then build the program. This step will compile the source code. If there is no compilation error, the system automatically starts the simulator and puts the EXE program into the simulator file system. Then you can start hello1. If you are in Windows 98 or me, the simulator system will display an error message, because the simulator can only run in Win2000 or XP.

If you have a Windows CE system, such as a Pocket PC (PPC), you can connect PPC to a PC in the same way as synchronizing PC content to PPC. Open Microsoft ActiveSync and establish a connection between PPC and PC. For Windows CE devices to run, although the ActiveSync link is not necessary, I found that it can be run to create a more stable link between the development environment and the Windows CE system.

1. The connection between the PC and Windows CE device is successful and runs. switch back to EVC ++ and select the appropriate target device (for example, Win32 [wce arm] Debug for ipaq Pocket PC) compile and rebuild. Like creating a program for a simulator, EVC ++ automatically downloads the compiled program to a remote device if there is no error. Programs are either stored in the root directory of OSS or saved in the/Windows/Start Menu example.

Run this program
To run hello1 on the embedded winodows ce device H/PC, click the my computer icon of the handheld PC, find the file in the root directory, and double-click the application icon to start the program.

To run the program on the Pocket PC, select the program from the Start menu of the device. Because EVC ++ has downloaded the program to the directory where the/Windows/Start Menu is located. In this way, the downloaded application is automatically displayed in the Start Menu.

Is there a problem?
If you click the icon on the Pocket PC or select the hello1 program from the Start menu, there seems to be no response. On a hand-held PC, the program just seems to have refreshed the screen. This is because the program is started, written to the console, and ended. Unless you start a program from a created console, Windows CE will create a console window when hello1 executes the printf statement and automatically close the console when hello1 ends.

On the Pocket PC, applications can run, but the Pocket PC does not provide console display for outputs such as printf. By placing the driver console. dll in the Windows directory of the Pocket PC, you can add console support for the Pocket PC. The driver obtains the input from the driver interface, creates a screen window, and prints the output string. The console Startup Program can be obtained from the handheld PC and embedded Windows CE versions.

Note 1: although the prototype of the entry point can be changed technically to match the standard entry point of C, it is generally not worth this effort.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.