home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / DDJ9403A.ZIP / DOS2WIN.ASC < prev    next >
Text File  |  1994-01-30  |  10KB  |  256 lines

  1. _PORTING FROM DOS TO WINDOWS_
  2. by Walter Oney
  3.  
  4. Listing One
  5.  
  6. /**********************************************************************/
  7. /* CLIENT.C -- Database client program (Windows app). By Walter Oney  */
  8. /**********************************************************************/
  9.  
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <string.h>
  13. #include <wingate.h>
  14.  
  15. static HINSTANCE hInst;
  16.  
  17. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  18.  
  19. #define arraysize(p) (sizeof(p)/sizeof((p)[0]))
  20. /**********************************************************************/
  21. int NEAR PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrev,
  22.     LPSTR lpCmd, int nShow)                          // WinMain 
  23. {   HWND hwnd;              // main window handle
  24.     MSG msg;                // current message
  25.     WNDCLASS wc;            // window class descriptor
  26.  
  27.     if (hPrev)              // only allow 1 instance at a time
  28.         return 0; 
  29.     hInst = hInstance;
  30.     memset(&wc, 0, sizeof(wc));
  31.     wc.lpszClassName = "clientwindow";
  32.     wc.hInstance = hInstance;
  33.     wc.lpfnWndProc = MainWndProc;
  34.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  35.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  36.  
  37.     wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  38.     if (!RegisterClass(&wc))
  39.         return 0;
  40.     hwnd = CreateWindow("clientwindow", "WINGate Database Demonstration",
  41.         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  42.         CW_USEDEFAULT, 0, NULL, hInstance, NULL);
  43.     if (!hwnd)
  44.         return 0;
  45.     ShowWindow(hwnd, nShow);
  46.  
  47.     while (GetMessage(&msg, 0, 0, 0))
  48.     {                                 // process messages
  49.         TranslateMessage(&msg);
  50.         DispatchMessage(&msg);
  51.     }                                 // process messages end
  52.     return msg.wParam;
  53. }                                     // WinMain end
  54. /**********************************************************************/
  55. LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
  56. {                                     // MainWndProc
  57.     static char *country[] =
  58.     {   "Afghanistan","Algeria","Angola","Argentina","Australia","Austria"
  59.     };
  60.     static HWND hwndList;           // list of countries
  61.     static BOOL bRegistered;        // TRUE if registered with WINGate
  62.     switch (msg)                    // process message
  63.     {   case WM_CREATE:             // WM_CREATE
  64.         {   int i;
  65.             if (WGRegisterClient("client"))
  66.             {                      // couldn't initialize WINGate
  67.                 MessageBox(hwnd, "Unable to initialize WINGate", "ERROR",
  68.                                    MB_OK | MB_ICONHAND);
  69.                 return -1;
  70.             }                      // couldn't initialize WINGate
  71.             bRegistered = TRUE;
  72.             hwndList = CreateWindow("listbox", "",
  73.                         WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_NOTIFY,
  74.                         0, 0, 0, 0, hwnd, (HMENU) 100, hInst, NULL);
  75.             if (!hwndList)
  76.                 return -1;
  77.             for (i = 0; i < arraysize(country); ++i)
  78.                 ListBox_AddString(hwndList, country[i]);
  79.             break;
  80.         }                                // WM_CREATE end
  81.     case WM_SIZE: 
  82.         {   RECT rc;                     // WM_SIZE
  83.             GetClientRect(hwnd, &rc);
  84.             if (hwndList)
  85.                 MoveWindow(hwndList, rc.left, rc.top, rc.right-rc.left,
  86.                     rc.bottom-rc.top, TRUE);
  87.             break;
  88.         }                                // WM_SIZE end
  89.     case WM_COMMAND:
  90.         switch (LOWORD(wParam))          // select on control id
  91.         { case 100:                      // the list box
  92.  
  93.             switch (HIWORD(lParam))      // select on notification code
  94.             {  
  95.             case LBN_SELCHANGE:          // LBN_SELCHANGE
  96.                 {   char msgbuf[80];     // message assembly buffer
  97.                     int i;               // selection (country) index
  98.                     char capital[80];    // response from server
  99.                     WGTRANS trans;       // query transaction
  100.                     int code;            // error code
  101.                     long tid;            // query transaction id
  102.                     i = ListBox_GetCurSel(hwndList);
  103.                     trans = WGCreateTrans("client", 80);
  104.                     WGSetTransString(trans, country[i]);
  105.                     WGSetTransValue(trans, 1, 1);
  106.                     tid = WGExecute(trans, "server", 1000,
  107.                         WG_STAT_WAIT_RESPONSE, &code);
  108.                     WGGetResponse(trans, tid, WG_STAT_WAIT_RESPONSE);
  109.                     WGGetTransString(trans, capital, sizeof(capital));
  110.                     WGDestroyTrans(trans);
  111.                     wsprintf(msgbuf, "The capital of %s is %s",
  112.                         (LPSTR) country[i], (LPSTR) capital);
  113.                     MessageBox(hwnd, msgbuf, "Geographical Information",
  114.                         MB_OK | MB_ICONINFORMATION);
  115.                     break;
  116.                 }                        // LBN_SELCHANGE end
  117.             }                            // select on notification code end
  118.         }                                // select on control id end
  119.         break;
  120.     case WM_DESTROY:
  121.         if (bRegistered)
  122.         {   int code;                    // close out WINGate connection
  123.             WGTRANS trans = WGCreateTrans("client", 0);
  124.             WGSetTransValue(trans, 1, 0);
  125.             WGExecute(trans, "server", 1000, WG_STAT_NO_RESPONSE, &code);
  126.             WGDestroyTrans(trans);
  127.             WGUnregisterClient("client");
  128.         }                                // close out WINGate connection end
  129.         PostQuitMessage(0);
  130.         break;
  131.     default:
  132.         return DefWindowProc(hwnd, msg, wParam, lParam);
  133.     }                                    // process message end
  134.     return 0;
  135. }                                        // MainWndProc end
  136.  
  137.  
  138.  
  139. Listing Two 
  140.  
  141. /*********************************************************************/
  142. /*    SERVER.C -- DOS-based database server for WINGate demo.        */
  143. /*    Written by Walter Oney                                         */
  144. /*********************************************************************/
  145.  
  146. #include <stdio.h>
  147. #include <stdlib.h>
  148. #include <wingate.h>
  149.  
  150. static void error(int code);
  151.  
  152.  
  153. #define arraysize(p) (sizeof(p)/sizeof((p)[0]))
  154. /*********************************************************************/
  155. void main()                          // main
  156. {   short code;                      // error code
  157.     if ((code = WGInit(0, 16384)))
  158.         error(code);
  159.     if ((code = WGRegisterServer("server", 1024, 0, 0)))
  160.     {                                // can't register server
  161.         WGTerm();
  162.         error(code);
  163.     }
  164.     while (1)                        // until told to quit
  165.     {   WGTRANS trans = WGGetTrans();
  166.         if (trans)                   // process transaction
  167.         {   char country[80];        // name of country being queried
  168.             int i;                   // loop index
  169.             long tid;                // transaction id
  170.             WGTRANS response;        // response to query
  171.             static char *key[] =
  172.             {
  173.                 "Afghanistan", "Algeria", "Angola", "Argentina",
  174.                 "Australia", "Austria"
  175.             };
  176.             static char *value[] =
  177.             {
  178.                 "Kabul", "Algiers", "Luanda", "Buenos Aires",
  179.                 "Canberra", "Vienna"
  180.             };
  181.             tid = WGGetTransID(trans, &code);
  182.             if (WGGetTransValue(trans, 1, &code) == 0)
  183.                 break;                  // error or "quit" request
  184.             WGGetTransString(trans, country, sizeof(country));
  185.             for (i = 0; i < arraysize(country); ++i)
  186.                 if (strcmp(country, key[i]) == 0)
  187.                     break;              // found it
  188.             response = WGCreateTrans("server", strlen(value[i])+1);
  189.             WGSetTransString(response, value[i]);
  190.             WGPostResponse(response, tid);
  191.             WGDestroyTrans(response);
  192.         }                               // process transaction end
  193.         _asm                            // yield time slice
  194.         {   mov   ax, 1680h
  195.             int   2Fh
  196.         }                               // yield time slice end
  197.     }   // until told to quit
  198.     WGUnregisterServer("server");
  199.     WGTerm();
  200. }                                       // main end
  201. /*********************************************************************/
  202. static void error(int code)
  203. {                                       // error
  204.     printf("WINGate error %d\n", code);
  205.     exit(1);
  206. }                                       // error end
  207.  
  208.  
  209.  
  210. Example 1:  
  211.  
  212. WGTRANS trans = WGCreateTrans("client", 80);
  213. WGSetTransString(trans, country[i]);
  214. WGSetTransValue(trans, 1, 1);
  215. long tid = WGExecute(trans, "server", 1000,
  216.    WG_STAT_WAIT_RESPONSE, &code);
  217. WGGetResponse(trans, tid, WG_STAT_WAIT_RESPONSE);
  218. WGGetTransString(trans, capital, sizeof(capital));
  219.  
  220. WGDestroyTrans(trans);
  221.  
  222.  
  223. Example 2: 
  224.  
  225. WGTRANS trans = WGGetTrans();
  226. if (trans)
  227. {                  // process transaction
  228.    WGGetTransString(trans, country, sizeof(country));
  229.    ...             // [code that looks up capital city name]
  230.    WGTRANS response = WGCreateTrans("server",strlen(capital)+1);
  231.    WGSetTransString(response, capital);
  232.    long tid = WGGetTransID(trans, &code);
  233.    WGPostResponse(response, tid);
  234.    WGDestroyTrans(response);
  235. }        
  236.  
  237.  
  238. Example 3: 
  239.  
  240. (a)
  241.  
  242. C:\WINGATE>winspawn notepad
  243. Execution OK ==> 11591
  244.  
  245. (b)
  246.  
  247. C:\WINGATE>winctrl -ms #11591 300 240 300 240
  248. Execution OK ==> 0
  249.  
  250.  
  251. (c)
  252.  
  253. C:\WINGATE>winkill #11591
  254. Execution OK ==> 0
  255.  
  256.