home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol05 / 01 / pmaccess / msgq.c < prev    next >
C/C++ Source or Header  |  1989-10-19  |  3KB  |  110 lines

  1. /* msgq.c RHS 7/15/89
  2.  *
  3.  * message queue routines
  4.  */
  5. #define    INCL_DOS
  6. #include<os2.h>
  7. #include<string.h>
  8. #include<malloc.h>
  9. #include"errexit.h"
  10. #include"msgq.h"
  11.  
  12. typedef struct _qtable                          // message queue table stru
  13.     {
  14.     HQUEUE      qhandle;
  15.     PID         qowner;
  16.     } QTABLE;
  17.  
  18. #define MAXQUEUES    15
  19.  
  20. QTABLE qtable[MAXQUEUES];                       // message queue table
  21. qtableindex = 0;
  22.  
  23. // creates a new message queue
  24. void MsgQCreate(HQUEUE *qhandle, char *qname)
  25.     {
  26.     USHORT retval;
  27.  
  28.     if(retval = DosCreateQueue(qhandle,QUE_FIFO,qname))
  29.         error_exit(retval,"DosCreateQueue");
  30.     }
  31.  
  32. // opens an existing message queue
  33. USHORT MsgQOpen(HQUEUE *qhandle, char *qname)
  34.     {
  35.     PID    qowner;
  36.     USHORT retval;
  37.  
  38.     if(retval = DosOpenQueue(&qowner,qhandle,qname))    // open queue
  39.         {
  40.         *qhandle = 0;
  41.         return retval;
  42.         }
  43.  
  44.     qtable[qtableindex].qhandle = *qhandle;     // put handle and PID
  45.     qtable[qtableindex].qowner = qowner;        // into table
  46.     qtableindex++;
  47.  
  48.     return 0;
  49.     }
  50.  
  51. // sends a message
  52. void MsgQSend(HQUEUE qhandle, PVOID event, USHORT size, USHORT msg)
  53.     {
  54.     USHORT retval,i;
  55.     SEL sel,newsel;
  56.     PVOID qptr;
  57.  
  58.     if(event != NULL)                           // if data with the message
  59.         {
  60.         for(i = 0; i < qtableindex; i++)        // find the queue
  61.             if(qtable[i].qhandle == qhandle)
  62.                 break;
  63.                                                 // create a segment for data        
  64.         if(retval = DosAllocSeg(size,&sel,(SEG_GIVEABLE)))
  65.             error_exit(retval,"DosAllocSeg");
  66.         DosGiveSeg(sel,qtable[i].qowner,&newsel);   // make it giveable
  67.         qptr = MAKEP(newsel,0);
  68.         memmove(qptr,event,size);               // put the data in it
  69.         }
  70.     else
  71.         qptr = NULL;
  72.                                                 // write message+data to q
  73.     if(retval = DosWriteQueue(qhandle,msg,size,(PBYTE)qptr,0))
  74.         error_exit(retval,"DosWriteQueue");
  75.     if((qptr != NULL) && (sel != newsel))       // free seg if not ours
  76.         DosFreeSeg(sel);
  77.     }
  78.  
  79. // get a message from a queue
  80. void MsgQGet(HQUEUE qhandle, PVOID *event, USHORT *size, USHORT *msg)
  81.     {
  82.     QUEUERESULT     qresult;
  83.     BYTE               priority;
  84.     USHORT retval;
  85.  
  86.     if(retval = DosReadQueue(qhandle,&qresult,size,
  87.             (PVOID FAR *)event,0x0000,DCWW_WAIT,&priority,0L))
  88.         error_exit(retval,"DosReadQueue");
  89.     *msg = qresult.usEventCode;
  90.     }
  91.  
  92. // close queue and remove from table
  93. void MsgQClose(HQUEUE qhandle)
  94.     {
  95.     USHORT i;
  96.  
  97.     DosCloseQueue(qhandle);
  98.     for(i = 0; i < qtableindex; i++)
  99.         if(qtable[i].qhandle == qhandle)
  100.             break;
  101.     if(i != qtableindex)
  102.         memmove(&qtable[i],&qtable[i+1],(sizeof(QTABLE)*(qtableindex-(i+1))));
  103.     qtableindex--;
  104.     }
  105.  
  106.     /******************************************************/
  107.  
  108.  
  109.  
  110.