C+中的MSMQ样本+;? 有人能给我一些例子,说明如何从C++ API中创建、添加消息、读取和销毁私有消息队列?我尝试了MSDN代码,但无法使它们正常工作

C+中的MSMQ样本+;? 有人能给我一些例子,说明如何从C++ API中创建、添加消息、读取和销毁私有消息队列?我尝试了MSDN代码,但无法使它们正常工作,c++,windows,msmq,message-queue,C++,Windows,Msmq,Message Queue,谢谢我不太确定如何创建或销毁消息队列。Windows应该为每个线程创建一个线程 如果您使用的是MFC,那么任何CWinThread和CWnd派生类都有一个很容易访问的消息队列(使用PostMessage或PostThreadMessage以及ON_命令宏)。要使用windows API执行类似的操作,我认为您需要编写自己的消息泵,类似于CWinApp的run方法 MSG msg; BOOL bRet; while( (bRet = GetMessage( &msg, NULL, 0,

谢谢

我不太确定如何创建或销毁消息队列。Windows应该为每个线程创建一个线程

如果您使用的是MFC,那么任何CWinThread和CWnd派生类都有一个很容易访问的消息队列(使用PostMessage或PostThreadMessage以及ON_命令宏)。要使用windows API执行类似的操作,我认为您需要编写自己的消息泵,类似于CWinApp的run方法

MSG msg;
BOOL bRet; 
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
} 

…是MSDN文档中的示例。这是你用的吗?什么不起作用?

实际上,这是我感兴趣的代码:

#include "windows.h"
#include "mq.h"
#include "tchar.h"


HRESULT CreateMSMQQueue(
                        LPWSTR wszPathName, 
                        PSECURITY_DESCRIPTOR pSecurityDescriptor,
                        LPWSTR wszOutFormatName,
                        DWORD *pdwOutFormatNameLength
                        )
{

  // Define the maximum number of queue properties.
  const int NUMBEROFPROPERTIES = 2;


  // Define a queue property structure and the structures needed to initialize it.
  MQQUEUEPROPS   QueueProps;
  MQPROPVARIANT  aQueuePropVar[NUMBEROFPROPERTIES];
  QUEUEPROPID    aQueuePropId[NUMBEROFPROPERTIES];
  HRESULT        aQueueStatus[NUMBEROFPROPERTIES];
  HRESULT        hr = MQ_OK;


  // Validate the input parameters.
  if (wszPathName == NULL || wszOutFormatName == NULL || pdwOutFormatNameLength == NULL)
  {
    return MQ_ERROR_INVALID_PARAMETER;
  }



  DWORD cPropId = 0;
  aQueuePropId[cPropId] = PROPID_Q_PATHNAME;
  aQueuePropVar[cPropId].vt = VT_LPWSTR;
  aQueuePropVar[cPropId].pwszVal = wszPathName;
  cPropId++;

  WCHAR wszLabel[MQ_MAX_Q_LABEL_LEN] = L"Test Queue";
  aQueuePropId[cPropId] = PROPID_Q_LABEL;
  aQueuePropVar[cPropId].vt = VT_LPWSTR;
  aQueuePropVar[cPropId].pwszVal = wszLabel;
  cPropId++;



  QueueProps.cProp = cPropId;               // Number of properties
  QueueProps.aPropID = aQueuePropId;        // IDs of the queue properties
  QueueProps.aPropVar = aQueuePropVar;      // Values of the queue properties
  QueueProps.aStatus = aQueueStatus;        // Pointer to the return status



  WCHAR wszFormatNameBuffer[256];
  DWORD dwFormatNameBufferLength = sizeof(wszFormatNameBuffer)/sizeof(wszFormatNameBuffer[0]);
  hr = MQCreateQueue(pSecurityDescriptor,         // Security descriptor
                     &QueueProps,                 // Address of queue property structure
                     wszFormatNameBuffer,         // Pointer to format name buffer
                     &dwFormatNameBufferLength);  // Pointer to receive the queue's format name length



  if (hr == MQ_OK || hr == MQ_INFORMATION_PROPERTY)
  {
    if (*pdwOutFormatNameLength >= dwFormatNameBufferLength)
    {
      wcsncpy_s(wszOutFormatName, *pdwOutFormatNameLength - 1, wszFormatNameBuffer, _TRUNCATE);

      wszOutFormatName[*pdwOutFormatNameLength - 1] = L'\0';
      *pdwOutFormatNameLength = dwFormatNameBufferLength;
    }
    else
    {
      wprintf(L"The queue was created, but its format name cannot be returned.\n");
    }
  }
  return hr;
}

这可能会创建一个队列。。。但是,要使其正常工作,还缺少一些部分,这就是为什么我需要一个简单的示例。

MSDN示例代码存在哪些问题?如果你发布了错误,我们可能会有更多的机会帮助你。我同意Seb的观点,你能澄清一下你的问题吗?他指的是MSMQ,而不是窗口程序。哪个部分不起作用?MQCreateQueue是否返回错误,或者问题是否进一步发生?