Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello World from Programming Microsoft Windows CE .NET won't compile

Status
Not open for further replies.

ddeham

Programmer
Sep 30, 2005
11
0
0
I can't get the HelloCE.cpp program listed on pages 30-33 in Douglas Boling's Programming Microsoft Windows CE .NET to compile! I am running Microsoft Embedded Visual C++ 4.0 SP2. I copied the code from the CD included with the book to my hard drive. I opened the workspace file. Here's what the compiler says when I try to compile it for POCKET PC 2003, Win32 (WCE ARMV4) Release, POCKET PC 2003 Device:

--------------------Configuration: HelloCE - Win32 (WCE ARMV4) Release--------------------
Compiling...
HelloCE.cpp
C:\Documents and Settings\Dan\My Documents\Code\Chap01\HelloCE\HelloCE.cpp(55) : error C2086: 'hWnd' : redefinition
C:\Documents and Settings\Dan\My Documents\Code\Chap01\HelloCE\HelloCE.cpp(58) : error C2440: 'return' : cannot convert from 'const int' to 'struct HWND__ *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing clarm.exe.

HelloCE.exe - 2 error(s), 0 warning(s)
-------------------------------------------------------------------------------------------------------
The problem is with these few lines of code:
#if defined(WIN32_PLATFORM_PSPC)
// If Pocket PC, only allow one instance of the application
HWND hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return -1;
}
#endif

I went to go to the support page listed in the book. I clicked on the Support link for "Microsoft Press Books and Microsoft Press Book CDs." I clicked on "Search for corrections using the ISBN." I typed in the ISBN for the book (0-7356-1884-4), and it said: "We Currently Have No Documents That Match Your Search"! Please help! Please reply!

Thanks,

Dan



// HelloCE.h
//======================================================================
// Header file
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//================================================================
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))

//----------------------------------------------------------------------
// Generic defines and data types
//
struct decodeUINT { // Structure associates
UINT Code; // messages
// with a function.
LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
};
struct decodeCMD { // Structure associates
UINT Code; // menu IDs with a
LRESULT (*Fxn)(HWND, WORD, HWND, WORD); // function
};

//----------------------------------------------------------------------
// Function prototypes
//
HWND InitInstance (HINSTANCE, LPWSTR, int);
int TermInstance (HINSTANCE, int);

// Window procedures
LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM);

// Message handlers
LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyMain (HWND, UINT, WPARAM, LPARAM);

//------------------------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------------

// HelloCE.cpp
//======================================================================
// HelloCE - A simple application for Windows CE
//
// Written for the book Programming Windows CE
// Copyright (C) 2003 Douglas Boling
//======================================================================
#include <windows.h> // For all that Windows stuff
#include "helloce.h" // Program-specific stuff

//----------------------------------------------------------------------
// Global data
//
const TCHAR szAppName[] = TEXT("HelloCE");
HINSTANCE hInst; // Program instance handle

// Message dispatch table for MainWindowProc
const struct decodeUINT MainMessages[] = {
WM_PAINT, DoPaintMain,
WM_DESTROY, DoDestroyMain,
};

//======================================================================
// Program entry point
//
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
HWND hwndMain;

// Initialize this instance.
hwndMain = InitInstance (hInstance, lpCmdLine, nCmdShow);
if (hwndMain == 0) return 0x10;

// Application message loop
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// Instance cleanup
return TermInstance (hInstance, msg.wParam);
}
//----------------------------------------------------------------------
// InitInstance - Instance initialization
//
HWND InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow) {
WNDCLASS wc;
HWND hWnd;

// Save program instance handle in global variable.
hInst = hInstance;

#if defined(WIN32_PLATFORM_PSPC)
// If Pocket PC, only allow one instance of the application
HWND hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return -1;
}
#endif

// Register application main window class.
wc.style = 0; // Window style
wc.lpfnWndProc = MainWndProc; // Callback function
wc.cbClsExtra = 0; // Extra class data
wc.cbWndExtra = 0; // Extra window data
wc.hInstance = hInstance; // Owner handle
wc.hIcon = NULL, // Application icon
wc.hCursor = LoadCursor (NULL, IDC_ARROW);// Default cursor
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL; // Menu name
wc.lpszClassName = szAppName; // Window class name

if (RegisterClass (&wc) == 0) return 0;

// Create main window.
hWnd = CreateWindow (szAppName, // Window class
TEXT("HelloCE"), // Window title
// Style flags
WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
CW_USEDEFAULT, // x position
CW_USEDEFAULT, // y position
CW_USEDEFAULT, // Initial width
CW_USEDEFAULT, // Initial height
NULL, // Parent
NULL, // Menu, must be null
hInstance, // Application instance
NULL); // Pointer to create
// parameters
if (!IsWindow (hWnd)) return 0; // Fail code if not created.

// Standard show and update calls
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return hWnd;
}
//----------------------------------------------------------------------
// TermInstance - Program cleanup
//
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
//======================================================================
// Message handling procedures for main window
//
//----------------------------------------------------------------------
// MainWndProc - Callback function for application window
//
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
//
// Search message list to see if we need to handle this
// message. If in list, call procedure.
//
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages.Code)
return (*MainMessages.Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
//----------------------------------------------------------------------
// DoPaintMain - Process WM_PAINT message for window.
//
LRESULT DoPaintMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PAINTSTRUCT ps;
RECT rect;
HDC hdc;

// Get the size of the client rectangle
GetClientRect (hWnd, &rect);

hdc = BeginPaint (hWnd, &ps);
DrawText (hdc, TEXT ("Hello Windows CE!"), -1, &rect,
DT_CENTER | DT_VCENTER | DT_SINGLELINE);

EndPaint (hWnd, &ps);
return 0;
}
//----------------------------------------------------------------------
// DoDestroyMain - Process WM_DESTROY message for window.
//
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
 
Obviously whoever wrote the book has never tried compiling the code on the proper compiler. It is moaning about

hWnd being declared twice
returning -1 which is an integer. What it is expecting is an HWND.


Try changing the code to this
Code:
#if defined(WIN32_PLATFORM_PSPC)
    // If Pocket PC, only allow one instance of the application
    HWND hPPCWnd = FindWindow (szAppName, NULL);
    if (hPPCWnd) {
        SetForegroundWindow ((HWND)(((DWORD)hPPCWnd) | 0x01));    
        return (HWND)(-1);
    }
#endif
 
That worked! Thanks a lot!

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top