bool CMsmqWarpper::OpenPublicQueue(LPCWSTR wszPathName, bool connecttype) { if (wszPathName == NULL) { return false; } HRESULT hr = MQ_OK; if (connecttype) { DWORD dwFormatNameBufferLength = 256; WCHAR wszFormatNameBuffer[256] = {0}; hr = MQPathNameToFormatName(wszPathName, wszFormatNameBuffer, &dwFormatNameBufferLength); if (FAILED(hr)) { return false; } hr = MQOpenQueue(wszFormatNameBuffer, MQ_SEND_ACCESS, MQ_DENY_NONE, &m_hQueue ); } else { hr = MQOpenQueue(wszPathName, MQ_SEND_ACCESS, MQ_DENY_NONE, &m_hQueue ); } if (FAILED(hr)) { return false; } return true; }
//----------------------------------------------------- // // 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); }
//----------------------------------------------------- // // 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]); }
//-------------------------------------------------------- // // Receiver Mode // ------------- // The receiver side does the following: // 1. Creates a queue on the local computer // of type "guidMQTestType". // The queue is either public or private, depending // on the connection we want to establish. // 2. Opens the queue. // 3. In a loop, // receives messages // and prints the message body and message label. // 4. Cleans up handles. // 5. Deletes the queue from the directory service. // //-------------------------------------------------------- void Receiver(int dDirectMode) { MQQUEUEPROPS qprops; MQMSGPROPS msgprops; MQPROPVARIANT aPropVar[MAX_VAR]; QUEUEPROPID aqPropId[MAX_VAR]; MSGPROPID amPropId[MAX_VAR]; DWORD cProps; WCHAR wcsFormat[MAX_FORMAT]; UCHAR Buffer[MAX_BUFFER]; WCHAR wcsMsgLabel[MQ_MAX_MSG_LABEL_LEN+1]; WCHAR wcsPathName[MAX_FORMAT]; DWORD dwNumChars; QUEUEHANDLE qh; HRESULT hr; int nPos; printf("\nReceiver Mode on Machine: %s\n\n", mbsMachineName); // // Set properties to create a queue on the local computer. // cProps = 0; // Set the path name. if(dDirectMode == DIRECT) // Private queue path name { nPos = _snwprintf_s( wcsPathName, sizeof(wcsPathName)/sizeof(wcsPathName[0]), sizeof(wcsPathName)/sizeof(wcsPathName[0]) - 1, L"%S\\private$\\MSMQTest", mbsMachineName ); if(nPos < 0) { printf("The path name is too long for the buffer. Exiting...\n"); exit(1); } } else // Public queue path name { nPos = _snwprintf_s( wcsPathName, sizeof(wcsPathName)/sizeof(wcsPathName[0]), sizeof(wcsPathName)/sizeof(wcsPathName[0]) - 1, L"%S\\MSMQTest", mbsMachineName ); if(nPos < 0) { printf("The path name is too long for the buffer. Exiting...\n"); exit(1); } } aqPropId[cProps] = PROPID_Q_PATHNAME; aPropVar[cProps].vt = VT_LPWSTR; aPropVar[cProps].pwszVal = wcsPathName; cProps++; // Specify the service type GUID of the queue. // (This property will be used to locate all the queues of this type.) aqPropId[cProps] = PROPID_Q_TYPE; aPropVar[cProps].vt = VT_CLSID; aPropVar[cProps].puuid = &guidMQTestType; cProps++; // Add a descriptive label to the queue. // (A label is useful for administration through the MSMQ admin tools.) aqPropId[cProps] = PROPID_Q_LABEL; aPropVar[cProps].vt = VT_LPWSTR; aPropVar[cProps].pwszVal = L"Sample application of MSMQ SDK"; cProps++; // Create a QUEUEPROPS structure. qprops.cProp = cProps; qprops.aPropID = aqPropId; qprops.aPropVar = aPropVar; qprops.aStatus = 0; // // Create the queue. // dwNumChars = MAX_FORMAT; hr = MQCreateQueue( NULL, // IN: Default security &qprops, // IN/OUT: Queue properties wcsFormat, // OUT: Format name &dwNumChars); // IN/OUT: Size of the format name if (FAILED(hr)) { // // API failed, but not because the queue exists. // if (hr != MQ_ERROR_QUEUE_EXISTS) Error("MQCreateQueue failed", hr); // // The queue exist, so get its format name. // printf("The queue already exists. Open it anyway.\n"); if(dDirectMode == DIRECT) // It's a private queue, so we know its direct format name. { int n = _snwprintf_s( wcsFormat, sizeof(wcsFormat)/sizeof(wcsFormat[0]), sizeof(wcsFormat)/sizeof(wcsFormat[0]) - 1, L"DIRECT=OS:%s", wcsPathName); if(n < 0) { printf("The format name is too long for the buffer. Exiting...\n"); exit(1); } } else // It's a public queue, so we must get its format name from the DS. { dwNumChars = sizeof(wcsFormat)/sizeof(wcsFormat[0]); hr = MQPathNameToFormatName( wcsPathName, // IN: Queue path name wcsFormat, // OUT: Format name &dwNumChars); // IN/OUT: Size of the format name if (FAILED(hr)) Error("The format name cannot be retrieved", hr); } } // // Open the queue with receive access. // hr = MQOpenQueue( wcsFormat, // IN: Queue format name MQ_RECEIVE_ACCESS, // IN: Want to receive from queue 0, // IN: Allow sharing &qh); // OUT: Handle of open queue // // Things are a little bit tricky. MQCreateQueue succeeded, but in the // case of a public queue, this does not mean that MQOpenQueue // will succeed, because replication delays are possible. The queue is // registered in the DS, but it might take a replication interval // until the replica reaches the server that I am connected to. // To overcome this, open the queue in a loop. // // In this specific case, this can happen only if this program // is run on an MSMQ 1.0 backup server controller (BSC) or on // a client connected to a BSC. // To be totally on the safe side, we should have put some code // to exit the loop after a few retries, but this is just a sample. // while (hr == MQ_ERROR_QUEUE_NOT_FOUND) { printf("."); // Wait a bit. Sleep(500); // Retry. hr = MQOpenQueue(wcsFormat, MQ_RECEIVE_ACCESS, 0, &qh); } if (FAILED(hr)) Error("The queue cannot be opened", hr); // // Main receiver loop // if(dDirectMode == DIRECT) { printf("\n* Working in workgroup (direct) mode.\n"); } printf("\n* Waiting for messages...\n"); for(;;) { // // Set the message properties for reading messages. // cProps = 0; // Ask for 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++; // Ask for the label of the message. amPropId[cProps] = PROPID_M_LABEL; aPropVar[cProps].vt = VT_LPWSTR; aPropVar[cProps].pwszVal = wcsMsgLabel; cProps++; // Ask for the length of the label of the message. amPropId[cProps] = PROPID_M_LABEL_LEN; aPropVar[cProps].vt = VT_UI4; aPropVar[cProps].ulVal = sizeof(wcsMsgLabel); cProps++; // Create a MSGPROPS structure. msgprops.cProp = cProps; msgprops.aPropID = amPropId; msgprops.aPropVar = aPropVar; msgprops.aStatus = 0; // // Receive the message. // hr = MQReceiveMessage( qh, // IN: Queue handle INFINITE, // IN: Time-out MQ_ACTION_RECEIVE, // IN: Read operation &msgprops, // IN/OUT: Message properties to retrieve NULL, // IN/OUT: No OVERLAPPED structure NULL, // IN: No callback NULL, // IN: No cursor NULL); // IN: Not part of a transaction if (FAILED(hr)) Error("MQReceiveMessage failed", hr); // // Display the received message. // printf("%S : %s\n", wcsMsgLabel, Buffer); // // Check for a request to end the application. // if (_stricmp(Buffer, "quit") == 0) break; } /* while (1) */ // // Cleanup: Close the handle to the queue. // MQCloseQueue(qh); // // In the concluding stage, we delete the queue from the directory // service. (We don't need to do this. In case of a public queue, // leaving it in the DS enables sender applications to send messages // even if the receiver is not available.) // hr = MQDeleteQueue(wcsFormat); if (FAILED(hr)) Error("The queue cannot be deleted", hr); }
HRESULT MsmqQueue::openQueue(const char *szQueuePath, int openmode) { WCHAR wszPathName[MQ_MAX_Q_NAME_LEN] = {0}; long accessmode = openmode; // bit field: MQ_{RECEIVE,SEND,PEEK,ADMIN}_ACCESS, long sharemode = MQ_DENY_NONE; // Validate the input string. if (szQueuePath == NULL) { return MQ_ERROR_INVALID_PARAMETER; } // convert to wide characters; int len = strlen(szQueuePath); if (MultiByteToWideChar( CP_ACP, 0, szQueuePath, len, wszPathName, _countof(wszPathName) ) == 0) { return MQ_ERROR_INVALID_PARAMETER; } if (len < _countof( wszPathName ) ) wszPathName[len]= 0; // need this to terminate HRESULT hr = MQ_OK; // dinoch Mon, 18 Apr 2005 16:12 printf("open: "); printf("fmtname(%ls) ", wszPathName); printf("accessmode(%d) ", accessmode); printf("sharemode(%d) ", sharemode); printf("\n"); hr = MQOpenQueue( wszPathName, // Format name of the queue accessmode, // Access mode sharemode, // Share mode &hQueue // OUT: Handle to queue ); // Retry to handle AD replication delays. // if (hr == MQ_ERROR_QUEUE_NOT_FOUND) { int iCount = 0 ; while((hr == MQ_ERROR_QUEUE_NOT_FOUND) && (iCount < 120)) { printf("."); // Wait a bit. iCount++ ; Sleep(50); // Retry. hr = MQOpenQueue(wszPathName, accessmode, sharemode, &hQueue); } } if (FAILED(hr)) { MQCloseQueue(hQueue); } return hr; };
BOOL CDisdrawDlg::OpenPrivateReceiveQueue() { // // Compose the pathname to the receiving queue and make it private. // char mbsPathName[MQ_MAX_Q_NAME_LEN+1]; DWORD dwNumChars = sizeof(mbsPathName); GetComputerName(mbsPathName, &dwNumChars); strcat_s(mbsPathName, sizeof(mbsPathName), "\\"); strcat_s(mbsPathName, sizeof(mbsPathName), "private$"); strcat_s(mbsPathName, sizeof(mbsPathName), "\\"); strcat_s(mbsPathName, sizeof(mbsPathName), m_strLogin); // // Prepare the properties of the receiving queue. // DWORD cProps = 0; QUEUEPROPID aPropId[3]; MQPROPVARIANT aPropVar[3]; MQQUEUEPROPS propsQueue; WCHAR wcsPathName[MQ_MAX_Q_NAME_LEN+1]; size_t nResultedStringLength=0; mbstowcs_s( &nResultedStringLength, wcsPathName, sizeof(wcsPathName)/sizeof(wcsPathName[0]), mbsPathName, sizeof(wcsPathName)/sizeof(wcsPathName[0])-1 ); aPropId[cProps] = PROPID_Q_PATHNAME; aPropVar[cProps].vt = VT_LPWSTR; aPropVar[cProps].pwszVal = wcsPathName; cProps++; aPropId[cProps] = PROPID_Q_TYPE; aPropVar[cProps].vt = VT_CLSID; aPropVar[cProps].puuid = &guidDrawType; cProps++; WCHAR wcsLabel[MQ_MAX_Q_LABEL_LEN+1]; nResultedStringLength=0; mbstowcs_s( &nResultedStringLength, wcsLabel, sizeof(wcsLabel)/sizeof(wcsLabel[0]), m_strLogin, sizeof(wcsLabel)/sizeof(wcsLabel[0])-1 ); aPropId[cProps] = PROPID_Q_LABEL; aPropVar[cProps].vt = VT_LPWSTR; aPropVar[cProps].pwszVal = wcsLabel; cProps++; propsQueue.cProp = cProps; propsQueue.aPropID = aPropId; propsQueue.aPropVar = aPropVar; propsQueue.aStatus = NULL; // // Create the receiving queue. // WCHAR wcsFormatName[MAX_FORMAT_NAME_LEN+1]; dwNumChars = sizeof(wcsFormatName)/sizeof(wcsFormatName[0]); HRESULT hr = MQCreateQueue(NULL, &propsQueue, wcsFormatName, &dwNumChars); if (FAILED(hr) && hr != MQ_ERROR_QUEUE_EXISTS) { return FALSE; } if( hr == MQ_ERROR_QUEUE_EXISTS) { // // If the receiving queue already exists, obtain its format name. // WCHAR wcsLoginName[MQ_MAX_Q_NAME_LEN+1]; nResultedStringLength=0; mbstowcs_s( &nResultedStringLength, wcsLoginName, sizeof(wcsLoginName)/sizeof(wcsLoginName[0]), m_strLogin, sizeof(wcsLoginName)/sizeof(wcsLoginName[0])-1 ); swprintf_s(wcsFormatName, sizeof(wcsFormatName)/sizeof(wcsFormatName[0]), L"DIRECT=OS:.\\private$\\%s",wcsLoginName); } // // Open the receiving queue (a local private queue). // hr = MQOpenQueue(wcsFormatName, MQ_RECEIVE_ACCESS, MQ_DENY_NONE , &m_hqIncoming); if (FAILED(hr)) { return FALSE; } return TRUE; }
void CDisdrawDlg::OnButtonAttach() { // // The constructor changes the cursor to an hourglass. // When the object goes out of scope, the cursor changes back to normal. // CWaitCursor wait ; WCHAR wcsFormatName[MAX_FORMAT_NAME_LEN+1]; if(m_fDsEnabledLocaly && m_iRadioDS == 0) { // // Connect with a remote computer in standard mode. // // // Obtain the name of the friend queue. // UpdateData(TRUE); m_strFriend.MakeUpper(); // // Make sure the friend queue exists. // if (!LocateQueue(m_strFriend, wcsFormatName, sizeof(wcsFormatName)/sizeof(wcsFormatName[0])) ) { AfxMessageBox("No such queue exists."); return; } } else if(!m_fDsEnabledLocaly || m_iRadioDS == 1) { // // Connect with a remote computer in direct mode. // // // Obtain the name of the friend queue and the remote computer. // UpdateData(TRUE); m_strFriend.MakeUpper(); m_strRemoteComputerName.MakeUpper(); // // Make sure the friend queue exists. Since we are connecting to // a private queue, we cannot obtain its format name from the // directory service (DS), so we will asuume it exists and try to open it. // // // Open the friend queue for sending (in direct mode) // Please Note: // Opening a remote queue for sending will always // succeed even if the queue or computer does not exist. // When working in direct mode, we must use private queues. // We cannot know whether a given private queue on another computer exists or not, // so here we will just assume that it does. To make the application more robust, // an acknowledgement queue should be created on the local computer, and // a request for acknowledgement messages should be added to the messages sent. // Then the application can notify the user when a negative adknowledgement message // (NACK) is received. // // // Compose the format name. // char mbsFormatName[MAX_FORMAT_NAME_LEN+1]; sprintf_s(mbsFormatName, sizeof(mbsFormatName), "DIRECT=OS:%s\\private$\\%s",m_strRemoteComputerName,m_strFriend); size_t nResultedStringLength=0; mbstowcs_s( &nResultedStringLength, wcsFormatName, sizeof(wcsFormatName)/sizeof(wcsFormatName[0]), mbsFormatName, sizeof(mbsFormatName)-1 ); } // // Open the remote queue for sending. // The action is the same in both cases. // HANDLE hqNewFriend; HRESULT hr = MQOpenQueue(wcsFormatName, MQ_SEND_ACCESS, MQ_DENY_NONE , &hqNewFriend); if (FAILED(hr)) AfxMessageBox("The remote queue cannot be opened."); else { // // Close the previous friend queue and update the title bar. // if (m_hqOutgoing != NULL) { MQCloseQueue(m_hqOutgoing); } m_hqOutgoing = hqNewFriend; CString strTitle = m_strLogin + " connected to " + m_strFriend; SetWindowText(strTitle); m_btnAttach.EnableWindow(FALSE); } }
BOOL CDisdrawDlg::OpenReceiveQueue() { // // Do not create the receiving queue if it already exists in the enterprise. // HRESULT hr; WCHAR wcsFormatName[MAX_FORMAT_NAME_LEN+1]; if (!LocateQueue(m_strLogin, wcsFormatName, sizeof(wcsFormatName)/sizeof(wcsFormatName[0]))) { // // Form the pathname of the receiving queue. // char mbsPathName[MQ_MAX_Q_NAME_LEN+1]; DWORD dwNumChars = sizeof(mbsPathName); GetComputerName(mbsPathName, &dwNumChars); strcat_s(mbsPathName, sizeof(mbsPathName), "\\"); strcat_s(mbsPathName, sizeof(mbsPathName), m_strLogin); // // Prepare the receiving queue properties. // DWORD cProps = 0; QUEUEPROPID aPropId[3]; MQPROPVARIANT aPropVar[3]; MQQUEUEPROPS propsQueue; WCHAR wcsPathName[MQ_MAX_Q_NAME_LEN+1]; size_t nResultedStringLength=0; mbstowcs_s( &nResultedStringLength, wcsPathName, sizeof(wcsPathName)/sizeof(wcsPathName[0]), mbsPathName, sizeof(wcsPathName)/sizeof(wcsPathName[0])-1 ); aPropId[cProps] = PROPID_Q_PATHNAME; aPropVar[cProps].vt = VT_LPWSTR; aPropVar[cProps].pwszVal = wcsPathName; cProps++; aPropId[cProps] = PROPID_Q_TYPE; aPropVar[cProps].vt = VT_CLSID; aPropVar[cProps].puuid = &guidDrawType; cProps++; WCHAR wcsLabel[MQ_MAX_Q_LABEL_LEN+1]; nResultedStringLength=0; mbstowcs_s( &nResultedStringLength, wcsLabel, sizeof(wcsLabel)/sizeof(wcsLabel[0]), m_strLogin, sizeof(wcsLabel)/sizeof(wcsLabel[0])-1 ); aPropId[cProps] = PROPID_Q_LABEL; aPropVar[cProps].vt = VT_LPWSTR; aPropVar[cProps].pwszVal = wcsLabel; cProps++; propsQueue.cProp = cProps; propsQueue.aPropID = aPropId; propsQueue.aPropVar = aPropVar; propsQueue.aStatus = NULL; // // Create the receiving queue. // dwNumChars = sizeof(wcsFormatName)/sizeof(wcsFormatName[0]); hr = MQCreateQueue(NULL, &propsQueue, wcsFormatName, &dwNumChars); // // If the receiving queue already exists, obtain its format name. // if (hr == MQ_ERROR_QUEUE_EXISTS) { dwNumChars = sizeof(wcsFormatName)/sizeof(wcsFormatName[0]); hr = MQPathNameToFormatName(wcsPathName, wcsFormatName, &dwNumChars); } if (FAILED(hr)) return FALSE; } // // Open the receiving queue (it may be necessary to retry due to replication latency). // int iCount = 0; while ((hr = MQOpenQueue(wcsFormatName, MQ_RECEIVE_ACCESS, MQ_DENY_NONE , &m_hqIncoming)) == MQ_ERROR_QUEUE_NOT_FOUND) { Sleep (500); iCount++; if (iCount >=30) { // // A period of 15 seconds past without locating the queue. // We allow the user to stop attempts to open the queue. // int iRes = MessageBox("The receiving queue was not located.\nDo you want to continue searching?", NULL, MB_YESNO); if(iRes == IDYES) { // // Continue searching. // iCount =0; } else { // // Stop searching. // break; } } } if (FAILED(hr)) return FALSE; return TRUE; }