home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / GWAPI / GWAPI.C_ / GWAPI.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  6.5 KB  |  231 lines

  1. // gwapi.c    DLL illustrates how to get recognition results without messages
  2.  
  3. /******************* Include-controlling Defines ****************************/
  4. #define NOGDIDCAPMASKS
  5. #define OEMRESOURCE
  6. #define NOMETAFILE
  7. #define NOSOUND
  8. #define NOKANJI
  9. #define NOCOMM
  10. #define NOMINMAX
  11.  
  12. #define BUILDDLL        // include _loadds in function attr
  13.  
  14. /******************* Includes ***********************************************/
  15. #include <windows.h>
  16. #include <penwin.h>
  17.  
  18. /******************* Defines ************************************************/
  19. #define PUBLIC        FAR PASCAL
  20. #define PRIVATE        NEAR PASCAL
  21.  
  22. #define fTrue            1
  23. #define fFalse            0
  24.  
  25. #define szClassGWR    "GWRClass"
  26.  
  27. /******************* Typedefs ***********************************************/
  28. typedef struct
  29.     {
  30.     LPSTR lpsz;
  31.     int cbSize;
  32.     LPHANDLE lphPendata;
  33.     }
  34.     PARAMINFO, FAR *LPPARAMINFO;
  35.  
  36. /******************* Macros *************************************************/
  37. #define wRcrtAbort \
  38.     (RCRT_NOSYMBOLMATCH | RCRT_NORECOG | RCRT_ALREADYPROCESSED | RCRT_PRIVATE)
  39.  
  40. /******************* Module variables ***************************************/
  41. static HANDLE hInstanceDLL = NULL;
  42.  
  43. /******************* Local procedures ***************************************/
  44. BOOL PRIVATE FInitDLL(HANDLE hInstance);
  45.  
  46. /******************* Public procedures **************************************/
  47. LONG __export WINAPI GWRWndProc(HWND, UINT, WPARAM, LPARAM);
  48.  
  49. // this proto will have to be duplicated in caller:
  50. REC  __export WINAPI GetWriting(LPRC, LPSTR, int, LPHANDLE);
  51.  
  52. /******************* Export Functions ***************************************/
  53.  
  54. /*--------------------------------------------------------------------------
  55. PURPOSE: Windows Exit Procedure 
  56. RETURN: UINT 1
  57. CONDITIONS: pro forma
  58. */
  59. UINT __export WINAPI _WEP(int nParam)
  60.     {
  61.     nParam;    // noref
  62.  
  63.     return 1;
  64.     }
  65.  
  66.  
  67. /*--------------------------------------------------------------------------
  68. PURPOSE: Get writing from user and return string and/or data
  69. RETURN: REC result of recognition
  70. CONDITIONS: creates temp GWRClass window to receive WM_RCRESULT
  71. */
  72. REC __export WINAPI GetWriting(
  73.     LPRC lprc,                // initialized RC or NULL
  74.     LPSTR lpsz,                // ptr to buffer for recognized string, or NULL
  75.     int cbSize,                // sizeof lpsz, including NULL terminator
  76.     LPHANDLE lphPendata)    // ptr to handle of pen data, or NULL if not wanted
  77.     {
  78.     RC rc;
  79.     PARAMINFO pi;
  80.     REC rec;
  81.  
  82.     if (!lprc && !lpsz && !lphPendata)
  83.         return REC_NOINPUT;        // bad parameter combination
  84.  
  85.     // copy string input parameters into local struct:
  86.     pi.lpsz = lpsz;
  87.     pi.cbSize = cbSize;
  88.     pi.lphPendata = lphPendata;
  89.  
  90.     // get RC:
  91.     if (lprc)
  92.         rc = *lprc;                // caller's RC
  93.     else
  94.         InitRC(NULL, &rc);    // default RC; we'll set hwnd momentarily
  95.  
  96.     if (!(rc.hwnd = CreateWindow(
  97.         (LPSTR)szClassGWR,    // send WM_RCRESULT to GWRWndProc()
  98.         (LPSTR)"",                // no text
  99.         WS_POPUP,                // invisible
  100.         0, 0, 0, 0,                // size immaterial
  101.         NULL,                        // no parent
  102.         NULL,                        // no id or menu
  103.         hInstanceDLL,            // this dll
  104.         NULL)))                    // no extra params
  105.             return REC_OOM;    // maybe another problem but we'll report this
  106.  
  107.     // set up RC options:
  108.     if (lphPendata)
  109.         rc.lRcOptions |= RCO_SAVEHPENDATA;
  110.  
  111.     // pass address of our info struct in dwAppParam:
  112.     rc.dwAppParam = (DWORD)(LPPARAMINFO)π
  113.  
  114.     // GWRWndProc will get WM_RCRESULT, and fills param ptrs:
  115.     rec = ProcessWriting(rc.hwnd, &rc);
  116.  
  117.     DestroyWindow(rc.hwnd);
  118.     return rec;
  119.     }
  120.  
  121.  
  122. /*--------------------------------------------------------------------------
  123. PURPOSE: Window proc target for WM_RCRESULT message
  124. RETURN: 
  125. CONDITIONS: This window doesn't get any pen events etc.
  126. */
  127. LONG __export WINAPI GWRWndProc(
  128.     HWND hwnd,            // standard params
  129.     UINT message,
  130.     WPARAM wParam,
  131.     LPARAM lParam)
  132.     {
  133.     // we only handle one message:
  134.     if (message == WM_RCRESULT && lParam)        // lParam must be non-NULL
  135.         {
  136.         LPRCRESULT lpr = (LPRCRESULT)lParam;    // ptr to RCRESULT
  137.  
  138.         if (!(lpr->wResultsType & wRcrtAbort))    // we have a legitimate result
  139.             {
  140.             // get API ptrs from rc.dwAppParam:
  141.             LPPARAMINFO lppi = (LPPARAMINFO)lpr->lprc->dwAppParam;
  142.  
  143.             // set caller's handle to pen data if requested:
  144.             if (lppi->lphPendata)
  145.                 *lppi->lphPendata = lpr->hpendata;
  146.  
  147.             // fill caller's result [string] buffer if requested:
  148.             if (lppi->lpsz)
  149.                 {
  150.                 *lppi->lpsz = '\0';    // preterminate for safety
  151.  
  152.                 if (lpr->wResultsType & RCRT_GESTURE)
  153.                     // pass gestures back in first 2 bytes - this could 
  154.                     //    also be expanded (if room) to include hotspots...
  155.                     {
  156.                     if (lpr->cSyv == 1                        // sanity checks
  157.                         && lpr->lpsyv != NULL
  158.                         && (lppi->cbSize > sizeof(SYV)))    // if room
  159.                         {
  160.                         *(LPSYV)lppi->lpsz = *lpr->lpsyv;
  161.                         lppi->lpsz[sizeof(SYV)] = '\0';    // null terminate
  162.                         }
  163.                     }
  164.  
  165.                 else    // take recognizer's best guess
  166.                     {
  167.                     int cb = lpr->cSyv;    // doesn't include null-term
  168.  
  169.                     if (lppi->cbSize -1 < lpr->cSyv)
  170.                         cb = lppi->cbSize -1;
  171.                     SymbolToCharacter(lpr->lpsyv, cb, lppi->lpsz, NULL);
  172.                     lppi->lpsz[cb] = '\0';    // null terminate
  173.                     }
  174.                 }
  175.             }
  176.         return 1L;
  177.         }
  178.  
  179.     return DefWindowProc(hwnd, message, wParam, lParam);
  180.     }
  181.  
  182. /******************* Public Functions ***************************************/
  183.  
  184. /*--------------------------------------------------------------------------
  185. PURPOSE: Entry point called from libentry.asm
  186. RETURN: BOOL success
  187. CONDITIONS: 
  188. */
  189. int __export PUBLIC LibMain(
  190.     HANDLE hInstance,        // instance handle of DLL
  191.     UINT wDataSeg,            // (unused)
  192.     UINT cbHeapSize,        // heapsize must be > 0
  193.     LPSTR lpszCmdLine)    // ptr to command line (unused)
  194.     {
  195.     // noref:
  196.     wDataSeg;
  197.     lpszCmdLine;
  198.  
  199.     if (!cbHeapSize)
  200.         return fFalse;
  201.  
  202.     UnlockData(0);
  203.     return FInitDLL(hInstance);    // also sets global hInstanceDLL
  204.     }
  205.  
  206. /******************* Private Functions **************************************/
  207.  
  208. /*--------------------------------------------------------------------------
  209. PURPOSE: Register Input Window class
  210. RETURN: TRUE
  211. CONDITIONS: called from LibMain at init
  212. */
  213. BOOL PRIVATE FInitDLL(HANDLE hInstance)
  214.     {
  215.     WNDCLASS wndClass;
  216.  
  217.     wndClass.hCursor = LoadCursor(NULL, IDC_PEN);
  218.     wndClass.hIcon = NULL;
  219.     wndClass.lpszMenuName = NULL;
  220.     wndClass.lpszClassName = (LPSTR)szClassGWR;
  221.     wndClass.hbrBackground = NULL;
  222.     wndClass.hInstance = hInstanceDLL = hInstance;    // set global
  223.     wndClass.style = CS_SAVEBITS;
  224.     wndClass.lpfnWndProc = GWRWndProc;
  225.     wndClass.cbClsExtra = 0;
  226.     wndClass.cbWndExtra = 0;
  227.     RegisterClass((LPWNDCLASS) &wndClass);    // may be FALSE if already reg'd
  228.  
  229.     return fTrue;
  230.     }
  231.