home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / clipbrd.zip / pmclip.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  2KB  |  85 lines

  1. #define USE_OS2_TOOLKIT_HEADERS
  2. #define INCL_DOSERRORS
  3. #define INCL_DOSPROCESS
  4. #define INCL_WINCLIPBOARD
  5. #include <os2.h>
  6.  
  7. #include <assert.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "common.h"
  11.  
  12.  
  13. BOOL  fClipbrdOpen = FALSE;
  14.  
  15. HAB   hab          = NULLHANDLE;
  16.  
  17. HMQ   hmq          = NULLHANDLE;
  18.  
  19.  
  20. VOID APIENTRY exitfunc (VOID)
  21. {
  22.   if (fClipbrdOpen)
  23.   {
  24.     assert (hab != NULLHANDLE);
  25.     WinCloseClipbrd (hab);
  26.   }
  27.   if (hmq != NULLHANDLE)
  28.     WinDestroyMsgQueue (hmq);
  29.   if (hab != NULLHANDLE)
  30.     WinTerminate (hab);
  31.  
  32.   DosExitList (EXLST_EXIT, (PFNEXITLIST) exitfunc);
  33. }
  34.  
  35.  
  36. int main (int argc, char *argv [])
  37. {
  38.   HFILE  hfWrite;
  39.  
  40.   int    lo1, hi1, lo2, hi2;
  41.  
  42.   PVOID  pClipbrdBuf, pShareBuf;
  43.  
  44.   ULONG  ulFormat = 0,   /* WinQueryClipbrdFmtInfo seems to return an USHORT, */
  45.                          /* so we'd better initialize it to 0                 */
  46.          ulClipbrdSize,
  47.          ulActual;
  48.  
  49.   if (2 != argc                                                  ||
  50.       4 != sscanf (argv [1], ARG_FORMAT, &lo1, &hi1, &lo2, &hi2) ||
  51.       NO_ERROR != DosExitList (EXLST_ADD | ROUTINE_ORDER,
  52.                                (PFNEXITLIST) exitfunc)           ||
  53.       NULLHANDLE == (hab = WinInitialize (0))                    ||
  54.       NULLHANDLE == (hmq = WinCreateMsgQueue (hab, 0))           ||
  55.       !WinQueryClipbrdFmtInfo (hab, CF_TEXT, &ulFormat)          ||
  56.       !(fClipbrdOpen = WinOpenClipbrd (hab)))
  57.   {
  58.     exit (EXIT_FAILURE);
  59.   }
  60.   assert (ulFormat == CFI_POINTER);
  61.   pShareBuf = (PVOID) JOIN (lo1, hi1);
  62.   hfWrite   = (HFILE) JOIN (lo2, hi2);
  63.  
  64.   pClipbrdBuf = (PVOID) WinQueryClipbrdData (hab, CF_TEXT);
  65.   ulClipbrdSize = strlen (pClipbrdBuf) + 1;   /* including end-zero */
  66.  
  67.   if (NO_ERROR !=
  68.       DosGetSharedMem (pShareBuf, PAG_READ | PAG_WRITE)             ||
  69.       NO_ERROR !=
  70.       DosSetMem (pShareBuf, ulClipbrdSize, PAG_COMMIT | PAG_DEFAULT))
  71.   {
  72.     exit (EXIT_FAILURE);
  73.   }
  74.   memcpy (pShareBuf, pClipbrdBuf, ulClipbrdSize);
  75.  
  76.   if (NO_ERROR !=
  77.       DosWrite (hfWrite, &ulClipbrdSize, sizeof (ulClipbrdSize), &ulActual))
  78.   {
  79.     exit (EXIT_FAILURE);
  80.   }
  81.   assert (ulActual == sizeof (ulClipbrdSize));
  82.  
  83.   return EXIT_SUCCESS;
  84. }
  85.