In this activity a Win32 Forms (Winforms) "Hello World" application is developed as a Platform Builder subproject. Note that a correction is made to generated project with respect to resources compilation.
To create a native code Windows Form application in Platform Builder as a subproject:
1. Again action Add New Subproject to the OS project but this time choose the first option, WCE Application. Give it a name, say HelloWinForm and press [Next].
Figure 14.13: Win32 Form Application Options
2. From the options choose the Hello World option again, and generate the project.
3. Build and test run it using methods covered in the previous section.
Figure 14.14: HelloWinForm application output on the target device, displaying ”garbage”.
Note: The form when it runs is supposed to display “Hello World!” at the top but it displays garbage. This is because the auto generated project does not call the resource generator for the resource file HelloWinForm.rc which contains the “Hello World!” string. You might expect that the application will crash if an error occurs.
Let’s examine what happens in the application as it doesn’t crash with this error. This will involve:
4. Drill into the project source tree and open the resource file.
5. In its tree expand the String Table until you see the list of the application’s strings. There you will see that IDS_HELLO is the string that is meant to be display.
6. Close the resource file and open code source file HelloWinForm.cpp
7. Search for where that text is actually written (painted) by searching for WM_PAINT in the file. It is in WinProc() method.
8. The string value gets read in in the line of code LoadString().
9. Put a breakpoint on the DrawText() LOC so you can check the value of the string variable szHello
The application needs to be shut down before we run it again with the breakpoint. Note that WinForm applications don’t automatically exit like console applications as the UI is awaiting for user interaction. We can shut it down using Target Control as follows:
10. Enter gi proc in the Target Control window to get a list of the running processes. This means get processes.
11. Note the PXY process number of HelloWinForm
12. Enter kp XY (without the P) . This means kill process
Windows CE>gi proc PROC: Name PID AcctId VMBase CurZone P00: NK.EXE 00400002 00000000 80220000 00000000 P01: shell.exe 00fc0006 00000000 00010000 00000000 P02: udevice.exe 01be0006 00000000 00010000 00000000 P03: GweUser.exe 02f70002 00000000 00010000 00000000 P04: servicesd.exe 031e0002 00000000 00010000 00000000 P05: minshell.exe 03800002 00000000 00010000 00000000 P06: cmd.exe 01460026 00000000 00010000 00000000 P07: cmd.exe 03e20006 00000000 00010000 00000000 P08: HelloWinForm.exe 03c0000a 00000000 00010000 00000000 Windows CE>kp 08 Attempting to kill process of id 03c0000a ...Succeeded
Windows CE>gi proc
PROC: Name PID AcctId VMBase CurZone
P00: NK.EXE 00400002 00000000 80220000 00000000
P01: shell.exe 00fc0006 00000000 00010000 00000000
P02: udevice.exe 01be0006 00000000 00010000 00000000
P03: GweUser.exe 02f70002 00000000 00010000 00000000
P04: servicesd.exe 031e0002 00000000 00010000 00000000
P05: minshell.exe 03800002 00000000 00010000 00000000
P06: cmd.exe 01460026 00000000 00010000 00000000
P07: cmd.exe 03e20006 00000000 00010000 00000000
P08: HelloWinForm.exe 03c0000a 00000000 00010000 00000000
Windows CE>kp 08
Attempting to kill process of id 03c0000a ...Succeeded
13. Run the application and examine the value of szHello (Quickwatch) It is a garbage string.
14. Shut the application down again and insert the line
dw = GetLastError();
after the LoadString() LOC.
15. Also add a global declaration
DWORD dw;
16. Set a breakpoint on the GetLastError() line, rerun the application and step over it. Examine the value of dw.
Its value is 0x00000716 which is the System Error number 1816 (in decimal).
Looking this System Error Code up at: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx we find that the error is ERROR_RESOURCE_NAME_NOT_FOUND which means: “The specified resource name cannot be found in the image file”. So this means that LoadString couldn’t find the IDS_HELLO resource.
Note: If we didn’t shut the application down before rebuilding there would have been a compilation error.
Now to fix the project by enabling the resource compilation:
17. Shut the application down again.
18. Add that file under the SOURCES in that file as follows:
SOURCES= \ HelloWinForm.cpp \ HelloWinForm.rc \
SOURCES= \
HelloWinForm.cpp \
HelloWinForm.rc \
Listing 14.6 The required resources file file entry (in bold) in the sources file.
19. Rebuild and rerun the app:
Figure 1.15: Corrected HelloWinForm application output on the target device.
NEXT: Add the local time functionality to the WinForm App
Click here to provide feedback and input