Example Method/Function heading
///////////////////////////////////////////////////////////////////////////////
//
// HANDLE CreateMsgQueue( char* szQueueName, DWORD dwReadTimeout );
//
// Abstract: Create a message queue.
//
// Parameters:
// szQueueName [in] message queue name (case sensitive)
// dwReadTimeout [in] read timeout value in milliseconds
//
// Returns:
// HANDLE to message queue used by ReadMsgQueue(), WriteMessageQueue(),
// and SetMsgQueueInfo(). A NULL is returned if the queue cannot be
// created or if it already exists.
//
// Notes:
// dwReadTimeout specifies the time a posted ReadMsgQueue() is to wait for
// an incoming message.
// A dwReadTimeout value of INFINITE specifies an indefinite wait. A value
// of zero specifies no wait.
// Does not check to see if a message queue with the same name
// already exists. Don't create duplicate-named queues!
//
// Initially, there is no limit to the number of msgs that can be placed
// into a queue. A limit can be set by calling SetMsgQueueWriteInfo().
//
///////////////////////////////////////////////////////////////////////////////
HANDLE CreateMsgQueue( char* szQueueName, DWORD dwReadTimeout )
{
LPQUEUEHEADER pQhdr;
char szEventName[ MAX_PATH ]; // event flag name
#ifdef DEBUG_MSGQUEUE
#pragma message( "Compiling msgqueue.c with DEBUG_MSGQUEUE" );
printf("\nCreateMsgQueue( %s, %d )", szQueueName, dwReadTimeout );
#endif
if( !gbCSInit ) // is Critical Section uninitialized?
{
gbCSInit = TRUE;
InitializeCriticalSection( &gCS ); // initialize Critical Section
}
if( strlen( szQueueName ) == 0 )
return( (HANDLE)NULL ); // filename cannot be NULL
pQhdr = (LPQUEUEHEADER)malloc( sizeof(QUEUEHEADER) );
if( NULL == pQhdr ) // can't allocate memory for QUEUEHEADER
return( (HANDLE)NULL );
pQhdr->pszQueueName = // allocate memory for queue name
char*)malloc( strlen(szQueueName)+1 );
if( NULL == pQhdr->pszQueueName ) // can't allocate memory for queue name
{
free( pQhdr );
return( (HANDLE)NULL );
}
strcpy( pQhdr->pszQueueName, szQueueName ); // copy specified queue name
pQhdr->pMsgQueue = NULL; // initialize ptr to messages in queue
pQhdr->dwReadTimeoutValue = dwReadTimeout; // set queue read timeout value
pQhdr->nMaxMsgs = -1; // initial value; this means infinite msgs
pQhdr->nCurrentMsgs = 0; // initial count of msgs in queue
pQhdr->dwWriteTimeoutValue = 0; // initial value; means no wait
// this is the event object that is used for Read operations; it is
// used when a pending Read operation is waiting on a queue, and
// is signaled when something arrives into the queue that can be read.
strcpy( szEventName, szQueueName ); // Read event flag name
strcat( szEventName, "_r" ); // "_r" appended to base queue name
pQhdr->hReadEvent = CreateEvent( NULL, // security descriptor
FALSE, // F:auto-reset; T:manual-reset
FALSE, // F:non-signaled initially; T:initially signaled
szEventName );
// this is the event object that is used for Write operations; it is
// used when the queue is full and a pending Write operation
// cannot continue and thus waits on the queue. This event
// is signaled when a Read operation pulls a message off the queue
// thereby allowing another message to be placed into the queue..
strcpy( szEventName, szQueueName ); // Write event flag name
strcat( szEventName, "_w" ); // "_w" appended to base queue name
pQhdr->hWriteEvent = CreateEvent( NULL, // security descriptor
FALSE, // F:auto-reset; T:manual-reset
FALSE, // F:non-signaled initially; T:initially signaled
szEventName );
if( (NULL == pQhdr->hReadEvent) || (NULL == pQhdr->hWriteEvent) ) // can't create Event objects!
{
free( pQhdr->pszQueueName );
free( pQhdr );
return( (HANDLE)NULL );
}
EnterCriticalSection( &gCS ); // prevent other threads from entering
pQhdr->pNextHeader = gpQMaster; // hook queue header into linked
gpQMaster = pQhdr; // list of queue headers
LeaveCriticalSection( &gCS ); // release other threads
return( (HANDLE)pQhdr );
}
...