1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice-E is licensed to you under the terms described in the
// ICEE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceE/IceE.h>
#include <Hello.h>
using namespace std;
using namespace Demo;
class HelloI : public Hello
{
public:
HelloI(HWND wnd)
: _wnd(wnd)
{
}
virtual void
sayHello(const Ice::Current&) const
{
//
// Its not legal to write to windows controls in a thread
// other than main, so we post a custom message to the main
// thread which will cause the message to be written to the
// edit control.
//
static wchar_t* hello = L"Hello World\r\n";
::PostMessage(_wnd, WM_USER, (WPARAM)FALSE, (LPARAM)_wcsdup(hello));
}
private:
const HWND _wnd;
};
static HWND editHwnd;
static LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_USER:
{
// tprint from a thread other than main. lParam holds a pointer to the text.
::SendMessage(editHwnd, EM_REPLACESEL, (WPARAM)wParam, (LPARAM)lParam);
free((wchar_t*)lParam);
}
break;
case WM_CREATE:
{
RECT rcClient;
GetClientRect(hWnd, &rcClient);
editHwnd = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL /*| WS_HSCROLL*/ | ES_MULTILINE,
0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
hWnd, (HMENU)101, GetModuleHandle(NULL), NULL);
assert(editHwnd != NULL);
}
break;
case WM_SIZE:
{
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SetWindowPos(editHwnd, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
}
break;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
static const TCHAR windowClassName[] = L"Minimal Server";
WNDCLASS wc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, 0);
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = windowClassName;
if(!RegisterClass(&wc))
{
MessageBox(NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
RECT rect;
GetClientRect(GetDesktopWindow(), &rect);
int width = rect.right - rect.left;
if(width > 320)
{
width = 320;
}
int height = rect.bottom - rect.top;
if(height > 200)
{
height = 200;
}
HWND mainWnd = CreateWindow(windowClassName, L"Minimal Server", WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU|WS_SIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
NULL, NULL, hInstance, NULL);
if(mainWnd == NULL)
{
MessageBox(NULL, L"Window Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(mainWnd, SW_SHOW);
UpdateWindow(mainWnd);
int status = EXIT_SUCCESS;
extern int __argc;
extern char **__argv;
Ice::CommunicatorPtr communicator;
try
{
Ice::InitializationData initData;
initData.properties = Ice::createProperties();
//
// Set a default value for Hello.Endpoints so that the demo
// will run without a configuration file.
//
initData.properties->setProperty("Hello.Endpoints","tcp -p 10000");
//
// Now, load the configuration file if present. Under WinCE we
// use "config.txt" since it can be edited with pocket word.
//
try
{
initData.properties->load("config.txt");
}
catch(const Ice::FileException&)
{
}
communicator = Ice::initialize(__argc, __argv, initData);
Ice::ObjectAdapterPtr adapter = communicator->createObjectAdapter("Hello");
adapter->add(new HelloI(mainWnd), Ice::stringToIdentity("hello"));
adapter->activate();
//
// Display a helpful message to the user.
//
::SendMessage(editHwnd, EM_REPLACESEL,
(WPARAM)FALSE, (LPARAM)L"Close the window to terminate the server.\r\n");
//
// Run the message pump.
//
MSG Msg;
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
catch(const Ice::Exception& ex)
{
TCHAR wtext[1024];
string err = ex.toString();
mbstowcs(wtext, err.c_str(), err.size());
MessageBox(mainWnd, wtext, L"Error", MB_ICONEXCLAMATION | MB_OK);
status = EXIT_FAILURE;
}
if(communicator)
{
try
{
communicator->destroy();
}
catch(const Ice::Exception& ex)
{
TCHAR wtext[1024];
string err = ex.toString();
mbstowcs(wtext, err.c_str(), err.size());
MessageBox(mainWnd, wtext, L"Error", MB_ICONEXCLAMATION | MB_OK);
status = EXIT_FAILURE;
}
}
return status;
}
|