NEURON
ddesrvr.cpp
Go to the documentation of this file.
1 /* Borland C++ - (C) Copyright 1994 by Borland International */
2 
3 /***************************************************************************
4 
5  Program Name DDESrvr.cpp
6 
7  Purpose A simple DDE client application, which communicates
8  to a DDE server using the new 3.1 api DDEML calls.
9 
10  To use this program, build DDEClnt and DDESrvr. There
11  are project files for this.
12 
13 ****************************************************************************/
14 
15 #define STRICT
16 
17 #include <windows.h>
18 #pragma hdrstop
19 #include <ddeml.h>
20 #include <dde.h>
21 #include <windowsx.h>
22 
23 #include <stdio.h>
24 #include <string.h>
25 
26 #include "ddesrvr.h"
27 
28 #include "nrnbbs.h"
29 void nrnbbs_server_post(const char* name, const char* value);
30 void nrnbbs_server_take(const char* name, char* value);
31 void nrnbbs_server_admin_post(const char* name, const char* value);
32 void nrnbbs_server_admin_request(const char* name, char* value);
34 
35 HANDLE hInst; /* Current instance of application */
36 HWND hWnd; /* Handle of Main window */
37 
38 int xScreen; /* Screen metrics */
39 int yScreen; /* ... */
40 int yFullScreen; /* ... */
41 int xFrame; /* ... */
42 int yMenu; /* ... */
43 TEXTMETRIC tm; /* Text metrics */
44 int cxChar; /* Character metrics */
45 int cyChar; /* ... */
46 
47 char szScreenText[10][80]; /* Contains 10 lines of display data*/
48 int cCurrentLine; /* Index into szScreenText */
49 int cTotalLines; /* Total lines in szScreenText */
50 
51 /*
52  The DDE variables
53 */
54 
55 DWORD idInst = 0L; /* Instance of app for DDEML */
56 FARPROC lpDdeProc; /* DDE callback function */
59 HSZ hszUser;
60 HCONV hConvApp = (HCONV)NULL; /*Handle of established conversation*/
61 char szDDEData[256]; /* Local receive buffer */
62 char szDDEString[256]; /* Local send buffer */
63 int iServerCount = 0; /* Send message counter */
64 char tbuf[100]; /* Temporary buffer for count */
65 
66 char szAppName[] = "DDEServerApplication";
67 
68 
69 /***************************************************************************/
70 
71 #pragma argsused
72 int PASCAL WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
73  LPSTR lpszCmdLine, int nCmdShow )
74 {
75  MSG msg;
76 
77 
78  if ( !hPrevInstance ){ /* Other instances of app running? */
79  if ( !InitApplication ( hInstance ) ){ /* Initialize shared things */
80  return ( FALSE ); /* Exits if unable to initialize */
81  }
82  }
83  if ( !InitInstance ( hInstance, nCmdShow ) )
84  return ( FALSE );
86  while ( GetMessage ( &msg, NULL, 0, 0 ) )
87  {
88  TranslateMessage ( &msg );
89  DispatchMessage ( &msg );
90  }
91  DdeUninitialize ( idInst );
93  return ( msg.wParam );
94 }
95 
96 /***************************************************************************/
97 
98 BOOL FAR PASCAL InitApplication ( HANDLE hInstance )
99 {
100  WNDCLASS wc;
101 
102  wc.style = CS_HREDRAW | CS_VREDRAW;
103  wc.lpfnWndProc = MainWndProc;
104  wc.cbClsExtra = 0;
105  wc.cbWndExtra = 0;
106  wc.hInstance = hInstance;
107  wc.hIcon = LoadIcon ( hInstance, "DDEServerIcon" );
108  wc.hCursor = LoadCursor ( NULL, IDC_ARROW );
109  wc.hbrBackground = GetStockObject ( WHITE_BRUSH );
110  wc.lpszMenuName = "DDEServerMenu";
111  wc.lpszClassName = szAppName;
112 
113  if ( !RegisterClass ( &wc ) )
114  return ( FALSE );
115 
116  return ( TRUE );
117 }
118 
119 /***************************************************************************/
120 
121 BOOL InitInstance ( HANDLE hInstance, int nCmdShow )
122 {
123  hInst = hInstance;
124 
125  xScreen = GetSystemMetrics ( SM_CXSCREEN );
126  yScreen = GetSystemMetrics ( SM_CYSCREEN );
127 
128  hWnd = CreateWindow ( szAppName,
129  "NrnBBS Server Window",
130  WS_OVERLAPPEDWINDOW,
131  190, /* These co-ordinates look */
132  yScreen / 2 - 20, /* good on a VGA monitor */
133  xScreen - 200, /* running in 640x480. No */
134  yScreen / 2 - 50, /* combination was tried. */
135  NULL,
136  NULL,
137  hInstance,
138  NULL );
139 
140 /*
141  If window could not be created, return "failure"
142 */
143 
144  if ( !hWnd )
145  return ( FALSE );
146 
147 /*
148  Make the window visible; update its client area; and return "success"
149 */
150 #if 1
151  ShowWindow ( hWnd, nCmdShow ); /* Show the window */
152 #else
153  ShowWindow ( hWnd, SW_HIDE );
154 #endif
155  UpdateWindow ( hWnd ); /* Sends WM_PAINT message */
156  return ( TRUE ); /* Returns the value from PostQuitMessage */
157 
158 }
159 
160 /***************************************************************************/
161 
162 #pragma warn -eff
163 LRESULT CALLBACK _export MainWndProc ( HWND hWnd, UINT message,
164  WPARAM wParam, LPARAM lParam )
165 {
166  HDC hDC;
167  PAINTSTRUCT ps;
168  DLGPROC dlgProcAbout;
169  int i;
170  int j;
171  int y;
172 
173 
174  switch ( message )
175  {
176  case WM_CREATE:
177  hDC = GetDC ( hWnd );
178 
179  GetTextMetrics ( hDC, &tm );
180  cxChar = tm.tmAveCharWidth;
181  cyChar = tm.tmHeight + tm.tmExternalLeading;
182 
183  ReleaseDC ( hWnd, hDC );
184 
185  lpDdeProc = MakeProcInstance ( (FARPROC) DDECallback, hInst );
186  if ( DdeInitialize ( (LPDWORD)&idInst, (PFNCALLBACK)lpDdeProc,
187  APPCLASS_STANDARD, 0L ) )
188  {
189  HandleOutput ( "DDE initialization failure." );
190  return ( FALSE );
191  }
192  else
193  {
194  hszService = DdeCreateStringHandle ( idInst, "NrnBBS", CP_WINANSI );
195  hszAdmin = DdeCreateStringHandle ( idInst, "Admin", CP_WINANSI );
196  hszUser = DdeCreateStringHandle ( idInst, "User", CP_WINANSI );
197  hConvApp = DdeConnect ( idInst, hszService, 0L,
198  (PCONVCONTEXT) NULL );
199  if ( hConvApp ){
200  MessageBox(NULL, "NrnBBS server already running", "NrnBBS", MB_OK);
201  DdeDisconnect(hConvApp);
202  hConvApp = 0;
203  DestroyWindow(hWnd);
204  break;
205  }
206 
207  DdeNameService ( idInst, hszService, (HSZ) NULL, DNS_REGISTER );
208  }
209 
210  cCurrentLine = 0;
211  cTotalLines = 0;
212  break;
213 
214  case WM_COMMAND:
215  switch ( GET_WM_COMMAND_ID(wParam, lParam) )
216  {
217  case IDM_EXIT:
218  DestroyWindow ( hWnd );
219  break;
222  break;
223  default:
224  return ( DefWindowProc ( hWnd, message, wParam, lParam ) );
225  }
226  break;
227 
228  case WM_PAINT:
229  hDC = BeginPaint ( hWnd, &ps );
230 
231  y = 0;
232 
233  for ( i = 0; i < cTotalLines; i ++ )
234  {
235  if ( cTotalLines == 8 )
236  j = ( (cCurrentLine + 1 + i) % 9 );
237  else
238  j = i;
239 
240  TextOut ( hDC, 0, y, (LPSTR)(szScreenText[j]),
241  lstrlen ( szScreenText[j] ) );
242  y = y + cyChar;
243  }
244 
245  EndPaint ( hWnd, &ps );
246  break;
247 
248  case WM_DESTROY:
249  if ( hConvApp != (HCONV)NULL )
250  {
251  DdeDisconnect ( hConvApp );
252  hConvApp = (HCONV)NULL;
253  }
254  DdeFreeStringHandle ( idInst, hszService );
255  DdeFreeStringHandle ( idInst, hszAdmin );
256  DdeFreeStringHandle ( idInst, hszUser );
257 
258  FreeProcInstance ( lpDdeProc );
259 
260  PostQuitMessage ( 0 );
261  break;
262 
263  default:
264  return ( DefWindowProc ( hWnd, message, wParam, lParam ) );
265  }
266 
267  return ( FALSE );
268 }
269 #pragma warn .eff
270 
271 /***************************************************************************/
272 
273 
274 #pragma argsused
275 HDDEDATA EXPENTRY _export DDECallback ( WORD wType, WORD wFmt, HCONV hConv, HSZ hsz1,
276  HSZ hsz2, HDDEDATA hData, DWORD dwData1,
277  DWORD dwData2 )
278 {
279  switch ( wType )
280  {
281  case XTYP_CONNECT:
282  if ( hsz2 == hszService ){
283  return ( (HDDEDATA) TRUE );
284  }else{
285  HandleOutput ( "XTYP_CONNECT: hsz2 != hszService" );
286  return ( (HDDEDATA) FALSE );
287  }
288 
289  case XTYP_REQUEST: /* hsz1 topic ; hsz2 item */
290  DdeQueryString(idInst, hsz2, szDDEData, 40, CP_WINANSI);
291  if (hsz1 == hszUser) {
293  }else if (hsz1 == hszAdmin) {
295  }else{
296  HandleOutput("Request for invalid Topic.");
297  return ( (HDDEDATA)NULL );
298  }
299  hData = DdeCreateDataHandle ( idInst, (LPBYTE) szDDEString,
300  sizeof ( szDDEString ), 0L, hsz1, wFmt, 0 );
301 
302  if ( hData != (HDDEDATA)NULL ){
303  return ( hData );
304  }else{
305  HandleOutput ( "Could not create data handle." );
306  return ( (HDDEDATA)NULL );
307  }
308 
309  case XTYP_EXECUTE:
310  break;
311 
312  case XTYP_POKE: /* hsz1 topic; hsz2 item */
313  DdeGetData ( hData, (LPBYTE) szDDEData, 80L, 0L );
314 
315  if ( szDDEData != NULL ){
316  DdeQueryString(idInst, hsz2, szDDEString, 40, CP_WINANSI);
317  if (hsz1 == hszUser) {
318  nrnbbs_server_post(szDDEString, szDDEData);
319  }else if (hsz1 == hszAdmin) {
320  nrnbbs_server_admin_post(szDDEString, szDDEData);
321  }else{
322  return ( (HDDEDATA) NULL );
323  }
324  DdePostAdvise(idInst, hsz1, hsz2);
325  return ( (HDDEDATA) DDE_FACK );
326  }
327  return ( (HDDEDATA) NULL );
328  case XTYP_CONNECT_CONFIRM:
329  HandleOutput ( "DDE connection confirmed." );
330  hConvApp = hConv;
331  break;
332 
333  case XTYP_DISCONNECT:
334  hConvApp = (HCONV)NULL;
335  HandleOutput ( "A client has disconnected." );
336  break;
337 
338  case XTYP_ADVREQ:
339  hData = DdeCreateDataHandle ( idInst, (LPBYTE) szDDEString,
340 // sizeof ( szDDEString ), hsz1, hsz2, wFmt, 0 );
341  sizeof ( szDDEString ), 0L, hsz2, wFmt, 0 );
342  if ( hData != (HDDEDATA)NULL ){
343  return ( hData );
344  }else{
345  HandleOutput ( "Could not create data handle." );
346  return ( (HDDEDATA)NULL );
347  }
348 
349  case XTYP_ADVSTART:
350  return TRUE;
351 
352  case XTYP_ERROR:
353  break;
354  }
355 
356  return ( (HDDEDATA) NULL );
357 }
358 
359 /***************************************************************************/
360 
361 void HandleOutput ( char *szOutputString )
362 {
363 //DebugMessage("%s\n", szOutputString);
364  strcpy ( szScreenText[cCurrentLine], szOutputString );
365  cCurrentLine = ( cCurrentLine + 1 ) % 9;
366  if ( cTotalLines < 8 )
367  cTotalLines++;
368 
369  InvalidateRect ( hWnd, NULL, TRUE );
370  UpdateWindow ( hWnd );
371 }
HCONV hConvApp
Definition: ddesrvr.cpp:60
int cxChar
Definition: ddesrvr.cpp:44
int cTotalLines
Definition: ddesrvr.cpp:49
BOOL FAR PASCAL InitApplication(HANDLE hInstance)
Definition: ddesrvr.cpp:98
int cyChar
Definition: ddesrvr.cpp:45
void nrnbbs_server_admin_post(const char *name, const char *value)
Definition: nrnbbs.cpp:103
int iServerCount
Definition: ddesrvr.cpp:63
#define TRUE
Definition: err.c:57
HCONV hConv
Definition: ddeclnt.cpp:27
#define IDM_EXIT
Definition: ddeclnt.h:21
bool nrnbbs_connect()
Definition: ivocmac.cpp:32
void nrnbbs_show_postings()
Definition: nrnbbs.cpp:67
HDDEDATA hData
Definition: ddeclnt.cpp:29
int yFullScreen
Definition: ddesrvr.cpp:40
FARPROC lpDdeProc
Definition: ddesrvr.cpp:56
#define IDM_SHOW_CONNECTIONS
Definition: ddesrvr.h:22
void nrnbbs_server_admin_request(const char *name, char *value)
Definition: nrnbbs.cpp:110
void HandleOutput(char *szOutputString)
Definition: ddesrvr.cpp:361
HDDEDATA EXPENTRY _export DDECallback(WORD wType, WORD wFmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2)
Definition: ddesrvr.cpp:275
char szAppName[]
Definition: ddesrvr.cpp:66
int xFrame
Definition: ddesrvr.cpp:41
size_t j
char szScreenText[10][80]
Definition: ddesrvr.cpp:47
char * name
Definition: init.cpp:16
char tbuf[100]
Definition: ddesrvr.cpp:64
BOOL InitInstance(HANDLE hInstance, int nCmdShow)
Definition: ddesrvr.cpp:121
static uint32_t value
Definition: scoprand.cpp:26
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
Definition: ddesrvr.cpp:72
void nrnbbs_server_take(const char *name, char *value)
Definition: nrnbbs.cpp:80
HSZ hszUser
Definition: ddesrvr.cpp:59
HANDLE hInst
Definition: ddesrvr.cpp:35
WORD wFmt
Definition: ddeclnt.cpp:31
int cCurrentLine
Definition: ddesrvr.cpp:48
HSZ hszAdmin
Definition: ddesrvr.cpp:58
TEXTMETRIC tm
Definition: ddesrvr.cpp:43
int yMenu
Definition: ddesrvr.cpp:42
void nrnbbs_server_post(const char *name, const char *value)
Definition: nrnbbs.cpp:58
DWORD idInst
Definition: ddesrvr.cpp:55
int yScreen
Definition: ddesrvr.cpp:39
void nrnbbs_disconnect()
Definition: ivocmac.cpp:33
#define FALSE
Definition: err.c:56
#define i
Definition: md1redef.h:12
int xScreen
Definition: ddesrvr.cpp:38
char szDDEString[256]
Definition: ddesrvr.cpp:62
LRESULT CALLBACK _export MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Definition: ddesrvr.cpp:163
HSZ hszService
Definition: ddesrvr.cpp:57
HWND hWnd
Definition: ddesrvr.cpp:36
return NULL
Definition: cabcode.cpp:461
char szDDEData[256]
Definition: ddesrvr.cpp:61