//----------------------------------------------------- // // Sender in workgroup (direct) mode // //----------------------------------------------------- void DirectSender(void) { MQMSGPROPS msgprops; MQPROPVARIANT aPropVar[MAX_VAR]; MSGPROPID amPropId[MAX_VAR]; DWORD cProps; WCHAR wcsFormat[MAX_FORMAT]; WCHAR wcsReceiverComputer[MAX_COMPUTERNAME_LENGTH + 1]; UCHAR Buffer[MAX_BUFFER]; WCHAR wcsMsgLabel[MQ_MAX_MSG_LABEL_LEN+1]; QUEUEHANDLE qhSend; HRESULT hr; int nPos; // // Get the receiver computer name. // printf("Enter receiver computer name: "); if (fgetws(wcsReceiverComputer, MAX_COMPUTERNAME_LENGTH, stdin) == NULL) { printf("An invalid parameter was entered. Exiting...\n"); exit(1); } wcsReceiverComputer[MAX_COMPUTERNAME_LENGTH] = L'\0'; // // Deleting the new-line character. // if(wcsReceiverComputer[wcslen(wcsReceiverComputer) - 1] == L'\n') { wcsReceiverComputer[wcslen(wcsReceiverComputer) - 1] = L'\0'; } if(wcsReceiverComputer[0] == 0) { printf("An invalid parameter was entered. Exiting...\n"); exit(1); } // // Open the queue with send access. // nPos = _snwprintf_s( wcsFormat, sizeof(wcsFormat)/sizeof(wcsFormat[0]), sizeof(wcsFormat)/sizeof(wcsFormat[0]) - 1, L"DIRECT=OS:%s\\private$\\MSMQTest", wcsReceiverComputer ); if(nPos < 0) { printf("The format name is too long for the buffer. Exiting...\n"); exit(1); } hr = MQOpenQueue( wcsFormat, // IN: Queue format name MQ_SEND_ACCESS, // IN: Want to send to queue 0, // IN: Must be 0 for send access &qhSend); // OUT: Handle of open queue if (FAILED(hr)) Error("MQOpenQueue failed", hr); 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); } fflush(stdin); // // 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'; // // Delete the new-line character. // if(Buffer[strlen(Buffer) - 1] == '\n') { Buffer[strlen(Buffer) - 1] = '\0'; } // // Set the message properties for sending messages. // cProps = 0; // Set the body of the message. 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 label of the message. 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. // hr = MQSendMessage( qhSend, // 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 the queue handle. // MQCloseQueue(qhSend); }
bool CMsmqWarpper::SendMessage(const char* pdata, int len) { const int NUM_OF_PROPERTIES = 1; int nPropId = 0; HRESULT hr = MQ_OK; MQMSGPROPS msgProps; MSGPROPID aMsgPropId[NUM_OF_PROPERTIES]; MQPROPVARIANT aMsgPropVar[NUM_OF_PROPERTIES]; HRESULT aMsgStatus[NUM_OF_PROPERTIES]; aMsgPropId[nPropId] = PROPID_M_BODY; aMsgPropVar[nPropId].vt = VT_VECTOR | VT_UI1; aMsgPropVar[nPropId].caub.pElems = (unsigned char*)(pdata); aMsgPropVar[nPropId].caub.cElems = len; nPropId++; msgProps.cProp = nPropId; msgProps.aPropID = aMsgPropId; msgProps.aPropVar = aMsgPropVar; msgProps.aStatus = aMsgStatus; hr = MQSendMessage(m_hQueue, &msgProps, MQ_NO_TRANSACTION); if (FAILED(hr)) { return false; } return true; }
void CDisdrawDlg::SendMouseMovement(LINE line) { // // Send the line to the friend queue, if there is one. // if (m_hqOutgoing != NULL) { // // Prepare the message properties to send the message. // DWORD cProps = 0; MQMSGPROPS propsMessage; MQPROPVARIANT aPropVar[5]; MSGPROPID aPropId[5]; WCHAR wcsBody[MAX_MSG_BODY_LEN+1]; swprintf_s(wcsBody, sizeof(wcsBody)/sizeof(wcsBody[0]), L"%07ld%07ld%07ld%07ld", line.ptStart.x, line.ptStart.y, line.ptEnd.x, line.ptEnd.y); aPropId[cProps] = PROPID_M_BODY; aPropVar[cProps].vt = VT_UI1 | VT_VECTOR; aPropVar[cProps].caub.cElems = sizeof(wcsBody); aPropVar[cProps].caub.pElems = (UCHAR *)wcsBody; cProps++; WCHAR wcsLabel[MQ_MAX_MSG_LABEL_LEN+1]; swprintf_s(wcsLabel, sizeof(wcsLabel)/sizeof(wcsLabel[0]), L"%ld,%ld To %ld,%ld", line.ptStart.x, line.ptStart.y, line.ptEnd.x, line.ptEnd.y); aPropId[cProps] = PROPID_M_LABEL; aPropVar[cProps].vt = VT_LPWSTR; aPropVar[cProps].pwszVal = wcsLabel; cProps++; UpdateData(TRUE); aPropId[cProps] = PROPID_M_DELIVERY; aPropVar[cProps].vt = VT_UI1; aPropVar[cProps].bVal = m_iDelivery; cProps++; aPropId[cProps] = PROPID_M_PRIORITY; aPropVar[cProps].vt = VT_UI1; aPropVar[cProps].bVal = 3; cProps++; aPropId[cProps] = PROPID_M_BODY_TYPE; aPropVar[cProps].vt = VT_UI4; aPropVar[cProps].lVal = (long)VT_BSTR; cProps++; propsMessage.cProp = cProps; propsMessage.aPropID = aPropId; propsMessage.aPropVar = aPropVar; propsMessage.aStatus = NULL; // // Send the message to the outgoing queue. // MQSendMessage(m_hqOutgoing, &propsMessage, NULL); } }
//----------------------------------------------------- // // 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]); }
HRESULT MsmqQueue::sendMessage( const char *szMessageBody, int iMessageBodySize, int iMessageBodyType, const char *szLabel, const char *szCorrelationID, bool isTransactional ) { const int NUMBEROFPROPERTIES = 8; // Number of properties DWORD i = 0; HRESULT hr = MQ_OK; CHAR aszCorrelationID[PROPID_M_CORRELATIONID_SIZE+1] = {0}; // transactionFlag: MQ_NO_TRANSACTION, MQ_MTS_TRANSACTION, MQ_XA_TRANSACTION, or MQ_SINGLE_MESSAGE // see mq.h for details... ITransaction* pTransaction = isTransactional ? MQ_SINGLE_MESSAGE : MQ_NO_TRANSACTION; int len = strlen(szCorrelationID); if (len > 0) { if (len > PROPID_M_CORRELATIONID_SIZE) len = PROPID_M_CORRELATIONID_SIZE; // copy across JMS correlationID truncating to MSMQ msgid size of 20 memcpy(aszCorrelationID, szCorrelationID, len); } // Define an MQMSGPROPS structure. MQMSGPROPS msgprops; MSGPROPID aMsgPropId[NUMBEROFPROPERTIES]; PROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES]; HRESULT aMsgStatus[NUMBEROFPROPERTIES]; //Set the body aMsgPropId[i] = PROPID_M_BODY; aMsgPropVar[i].vt = VT_VECTOR | VT_UI1; aMsgPropVar[i].caub.pElems = (LPBYTE)szMessageBody; //szFormattedMessageBody aMsgPropVar[i].caub.cElems = iMessageBodySize ; ++i; // Message Body Type aMsgPropId[i] = PROPID_M_BODY_TYPE; aMsgPropVar[i].vt = VT_UI4; aMsgPropVar[i].ulVal = iMessageBodyType; // VT_BSTR / VT_ARRAY | VT_UI1 i++; // Specify that the message should not be lost if restarting the computer aMsgPropId[i] = PROPID_M_DELIVERY; aMsgPropVar[i].vt = VT_UI1; aMsgPropVar[i].bVal = MQMSG_DELIVERY_RECOVERABLE; i++; //Set the correlation id aMsgPropId[i] = PROPID_M_CORRELATIONID; aMsgPropVar[i].vt = VT_VECTOR | VT_UI1; aMsgPropVar[i].caub.pElems = (LPBYTE)aszCorrelationID; aMsgPropVar[i].caub.cElems = PROPID_M_CORRELATIONID_SIZE; ++i; //Set the label aMsgPropId[i] = PROPID_M_LABEL; aMsgPropVar[i].vt = VT_LPWSTR; WCHAR wszLabel[MQ_MAX_MSG_LABEL_LEN] = {0}; len= strlen(szLabel); if (len > 0) { if (MultiByteToWideChar( (UINT) CP_ACP, (DWORD) 0, (LPCSTR) szLabel, len, wszLabel, _countof(wszLabel) ) == 0) { return MQ_ERROR_INVALID_PARAMETER; } if (len < _countof(wszLabel) ) wszLabel[len]=0; // terminate } aMsgPropVar[i].pwszVal = wszLabel; ++i; // Initialize the MQMSGPROPS structure. msgprops.cProp = i; // Number of message properties msgprops.aPropID = aMsgPropId; // IDs of the message properties msgprops.aPropVar = aMsgPropVar; // Values of the message properties msgprops.aStatus = aMsgStatus; // Error reports // Call MQSendMessage to put the message to the queue. hr = MQSendMessage( hQueue, // Handle to the destination queue &msgprops, // Pointer to the MQMSGPROPS structure pTransaction ); return hr; };