//-----------------------------------------------------
//
// Sender in domain (standard) mode
//
//-----------------------------------------------------
void StandardSender(void)
{
    DWORD         cProps;

    MQMSGPROPS    msgprops;
    MQPROPVARIANT aPropVar[MAX_VAR];
    QUEUEPROPID   aqPropId[MAX_VAR];
    MSGPROPID     amPropId[MAX_VAR];

    MQPROPERTYRESTRICTION aPropRestriction[MAX_VAR];
    MQRESTRICTION Restriction;

    MQCOLUMNSET      Column;
    HANDLE        hEnum;

    WCHAR         wcsFormat[MAX_FORMAT];

    UCHAR         Buffer[MAX_BUFFER];
    WCHAR         wcsMsgLabel[MQ_MAX_MSG_LABEL_LEN+1];

    DWORD         i;

    DWORD         cQueue;
    DWORD         dwNumChars;
    QUEUEHANDLE   aqh[MAX_VAR];

    HRESULT       hr;
	int			  nPos;


    printf("\nSender mode on the computer %s\n\n", mbsMachineName);


    //
    // Prepare parameters for locating queues.
    //

    //
    // 1. Restriction: Queues with PROPID_Q_TYPE = MSMQTest service type GUIID
    //
    cProps = 0;
    aPropRestriction[cProps].rel         = PREQ;
    aPropRestriction[cProps].prop        = PROPID_Q_TYPE;
    aPropRestriction[cProps].prval.vt    = VT_CLSID;
    aPropRestriction[cProps].prval.puuid = &guidMQTestType;
    cProps++;

    Restriction.cRes      = cProps;
    Restriction.paPropRes = aPropRestriction;


    //
    // 2. Columnset (the queue properties to retrieve) = queue GUID
    //
    cProps = 0;
    aqPropId[cProps] = PROPID_Q_INSTANCE;
    cProps++;

    Column.cCol = cProps;
    Column.aCol = aqPropId;


    //
    // Issue the query to locate the queues.
    //
    hr = MQLocateBegin(
             NULL,          // IN:  Context must be NULL
             &Restriction,  // IN:  Restriction
             &Column,       // IN:  Properties to return
             NULL,          // IN:  No need to sort
             &hEnum);       // OUT: Enumeration handle

    if (FAILED(hr))
        Error("MQLocateBegin failed", hr);

    //
    // Get the results (up to MAX_VAR).
    // (For more results, call MQLocateNext in a loop.)
    //
    cQueue = MAX_VAR;
    hr = MQLocateNext(
             hEnum,         // IN:     Enumeration handle
             &cQueue,       // IN/OUT: Number of properties
             aPropVar);     // OUT:    Properties of the queue located

    if (FAILED(hr))
        Error("MQLocateNext failed", hr);

    //
    // End the query and release the resouces associated with it.
    //
    hr = MQLocateEnd(hEnum);

    if (cQueue == 0)
    {
        //
        // No queue could be found, so exit.
        //
        printf("No queue was registered.");
        exit(0);
    }


    printf("\tQueue(s) found: %d\n", cQueue);


    //
    // Open a handle for each of the queues found.
    //
    for (i = 0; i < cQueue; i++)
    {
        // Convert the queue GUID to a format name.
        dwNumChars = MAX_FORMAT;
        hr = MQInstanceToFormatName(
                  aPropVar[i].puuid,    // IN:     Queue GUID
                  wcsFormat,            // OUT:    Format name
                  &dwNumChars);         // IN/OUT: Size of format name

        if (FAILED(hr))
            Error("MQInstanceToFormatName failed", hr);

        //
        // Open the queue with send access.
        //
        hr = MQOpenQueue(
                 wcsFormat,           // IN:  Queue format name
                 MQ_SEND_ACCESS,      // IN:  Want to send to queue
                 0,                   // IN:  Must be 0 for send access
                 &aqh[i]);            // OUT: Handle of open queue

        if (FAILED(hr))
            Error("MQOpenQueue failed", hr);

        //
        // Free the memory that was allocated for the queue GUID during the query.
        //
        MQFreeMemory(aPropVar[i].puuid);
    }


    printf("\nEnter \"quit\" to exit.\n");


    //
    // Construct the message label.
    //
	nPos = _snwprintf_s(
                   wcsMsgLabel, 
                   sizeof(wcsMsgLabel)/sizeof(wcsMsgLabel[0]), 
                   sizeof(wcsMsgLabel)/sizeof(wcsMsgLabel[0]) - 1, 
                   L"Message from %S", 
                   mbsMachineName
                   );
	if(nPos < 0)
	{
		printf("The label is too long for the buffer. Exiting...\n");
		exit(1);
	}	

    //
    // Main sender loop
    //
    for(;;)
    {
        //
        // Get a string from the console.
        //
        printf("Enter a string: ");
		if (fgets(Buffer, MAX_BUFFER - 1, stdin) == NULL)
            break;
		
		Buffer[MAX_BUFFER-1] = '\0';
		//
		// Deleting the new-line character.
		//
		if(Buffer[strlen(Buffer) - 1] == '\n')
		{
			Buffer[strlen(Buffer)-1] = '\0'; 
		}


        //
        // Prepare the message properties for sending messages.
        //
        cProps = 0;

        // Set the message body.
        amPropId[cProps]            = PROPID_M_BODY;
        aPropVar[cProps].vt         = VT_UI1 | VT_VECTOR;
        aPropVar[cProps].caub.cElems = sizeof(Buffer);
        aPropVar[cProps].caub.pElems = Buffer;
        cProps++;

        // Set the message label.
        amPropId[cProps]            = PROPID_M_LABEL;
        aPropVar[cProps].vt         = VT_LPWSTR;
        aPropVar[cProps].pwszVal    = wcsMsgLabel;
        cProps++;

        // Create a MSGPROPS structure.
        msgprops.cProp    = cProps;
        msgprops.aPropID  = amPropId;
        msgprops.aPropVar = aPropVar;
        msgprops.aStatus  = 0;


        //
        // Send the message to all the queues.
        //
        for (i = 0; i < cQueue; i++)
        {
            hr = MQSendMessage(
                    aqh[i],     // IN: Queue handle
                    &msgprops,  // IN: Message properties to send
                    NULL);      // IN: Not part of a transaction

            if (FAILED(hr))
                Error("MQSendMessage failed", hr);
        }

        //
        // Check for a request to end the application.
        //
        if (_stricmp(Buffer, "quit") == 0)
            break;

    } /* for */


    //
    // Close all the queue handles.
    //
    for (i = 0; i < cQueue; i++)
        MQCloseQueue(aqh[i]);

}
Example #2
0
BOOL LocateQueue(CString m_strLabel, WCHAR *wcsFormatName, DWORD dwNumChars)
{
	//
	// Set restrictions to locate the drawing queue with the label specified.
	//
	DWORD cProps = 0;
	MQPROPERTYRESTRICTION aPropRestriction[2];
	MQRESTRICTION Restriction;

	aPropRestriction[cProps].rel           = PREQ;
	aPropRestriction[cProps].prop          = PROPID_Q_TYPE;
	aPropRestriction[cProps].prval.vt      = VT_CLSID;
	aPropRestriction[cProps].prval.puuid   = &guidDrawType;
	cProps++;

	WCHAR wcsLabel[MQ_MAX_Q_LABEL_LEN+1];
        size_t nResultedStringLength=0;
	mbstowcs_s(
            &nResultedStringLength,
            wcsLabel, 
            sizeof(wcsLabel)/sizeof(wcsLabel[0]),
            m_strLabel, 
            sizeof(wcsLabel)/sizeof(wcsLabel[0])-1
            );
	aPropRestriction[cProps].rel           = PREQ;
	aPropRestriction[cProps].prop          = PROPID_Q_LABEL;
	aPropRestriction[cProps].prval.vt      = VT_LPWSTR;
	aPropRestriction[cProps].prval.pwszVal = wcsLabel;
	cProps++;

	Restriction.cRes = cProps;
	Restriction.paPropRes = aPropRestriction;


	//
	// Request the queue instance for the queue specified.
	//
	cProps = 0;
	QUEUEPROPID aPropId[1];
	MQCOLUMNSET Column;
	
	aPropId[cProps] = PROPID_Q_INSTANCE;
	cProps++;

	Column.cCol = cProps;
	Column.aCol = aPropId;


	//
	// Locate the queue specified.
	//
	HANDLE hEnum;
	BOOL fFound = FALSE;
	HRESULT hr = MQLocateBegin(NULL, &Restriction, &Column, NULL, &hEnum);
	if (!FAILED(hr))
	{
	    MQPROPVARIANT aPropVar[1];
		DWORD cQueue = 1;
		hr = MQLocateNext(hEnum, &cQueue, aPropVar);
		if (!FAILED(hr) && cQueue > 0)
		{
			//
			// Obtain the format name for the queue located.
			//
			hr = MQInstanceToFormatName(aPropVar[0].puuid, wcsFormatName, &dwNumChars);
            MQFreeMemory(aPropVar[0].puuid);
			if (!FAILED(hr))
				fFound = TRUE;
		}

		MQLocateEnd(hEnum);
	}


    return fFound;
}