在Windows2000的消息队列服务里,建立一个专用队列,请问应用程序如何操作这个专用队列呢?

解决方案 »

  1.   

    是MSMQ,现在的问题是有一个应用程序使用Windows2000的消息队列服务,现在的问题是:我如何使用VC将一条信息写入已经在Windows2000里建立的一个专用队列。
      

  2.   

    BOOL SendMessage(LPCTSTR sLable, LPCTSTR sBody, LPCTSTR sForamtAddr) 
    {
    // TODO: Add your dispatch handler code here

    const int NUMBEROFPROPERTIES = 5;
    DWORD cPropId = 0; MQMSGPROPS MsgProps;
    MSGPROPID aMsgPropId[NUMBEROFPROPERTIES];
    MQPROPVARIANT aMsgPropVar[NUMBEROFPROPERTIES]; 
    HRESULT aMsgStatus[NUMBEROFPROPERTIES];
    HRESULT hr = MQ_OK;

    // Set message properties.

    WCHAR* wchDestQueueFormatName = (WCHAR*)malloc(2*strlen(sForamtAddr)+2);
    memset(wchDestQueueFormatName,0,2*strlen(sForamtAddr)+2);
    int ret = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sForamtAddr, strlen(sForamtAddr), wchDestQueueFormatName,strlen(sForamtAddr));
    if(!ret)
    {
    free(wchDestQueueFormatName);
    return FALSE;
    }
    TRACE("---DestQueueForamt--------%S\n",wchDestQueueFormatName); WCHAR pwLable[32];
    memset(pwLable,0,2*32);
    int cchWideChar = 32; aMsgPropId[cPropId] = PROPID_M_LABEL_LEN;            // Property ID
    aMsgPropVar[cPropId].vt =VT_UI4;                     // Type indicator
    aMsgPropVar[cPropId].ulVal = 64;   // Label buffer size
    cPropId++; ret = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sLable, strlen(sLable), pwLable,cchWideChar);
    if(!ret)
    {
    free(wchDestQueueFormatName);
    return FALSE;
    }
    aMsgPropId[cPropId] = PROPID_M_LABEL;                
    aMsgPropVar[cPropId].vt = VT_LPWSTR;                 
    aMsgPropVar[cPropId].pwszVal = pwLable;
    cPropId++;
    TRACE("-----Lable------%S\n",pwLable);
    aMsgPropId[cPropId] = PROPID_M_BODY;  
    aMsgPropVar[cPropId].vt = VT_UI1|VT_VECTOR;                    
    aMsgPropVar[cPropId].caub.cElems = strlen((const char *) sBody);
    aMsgPropVar[cPropId].caub.pElems = (unsigned char *)sBody;
    cPropId++;
    TRACE("-------Body-----%s\n",sBody); // Initialize the MQMSGPROPS structure.
    MsgProps.cProp = cPropId;                      // 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 MQOpenQueue to open the queue with send access.
    HANDLE hQueue = NULL;
    hr = MQOpenQueue (
      wchDestQueueFormatName,      // Format name of the queue
      MQ_SEND_ACCESS,              // Access mode
      MQ_DENY_NONE,                // Share mode
      &hQueue
     );                    // OUT: Queue handle
    if (FAILED(hr))
    {
    free(wchDestQueueFormatName);
    return FALSE;
    }
    // Call MQSendMessage to send the message to the opened queue.
    hr = MQSendMessage(
       hQueue,                     // Queue handle
       &MsgProps,                  // Message property structure
       MQ_NO_TRANSACTION
      );
    if (FAILED(hr))
    {
    free(wchDestQueueFormatName);
    MQCloseQueue(hQueue);
    return FALSE;

    // Call MQCloseQueue to close the queue.
    hr = MQCloseQueue(hQueue);
    free(wchDestQueueFormatName);
    return TRUE;
    }
      

  3.   

    adamx您好,能不能对上面这段代码做一下解释?万般感谢!
      

  4.   

    详细解释可以参考msdn,上面有大部分代码是从msdn拷贝下来的。