从C ++(非COM组件)代码访问MSMQ

时间:2014-05-19 12:38:00

标签: c++ runtime-error msmq

我是MSMQ(Microsoft Message Queues)概念的新手。我瞥见了Microsoft's link的示例代码,并尝试创建一个简单的MSMQ队列。这是我的完整代码(主要来自微软的链接)。

#include "stdafx.h"
#include "windows.h"
#include "mq.h"
#pragma comment (lib, "Mqrt.lib")
#include "tchar.h"
#include <stdio.h>
#define BUFLEN = 256;


#include <iostream>

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;
  }


  // Set queue properties.
  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++;


  // Initialize the MQQUEUEPROPS structure.
  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


  // Call MQCreateQueue to create the queue.
  WCHAR wszFormatNameBuffer[256];
  DWORD dwFormatNameBufferLength = 256;


  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 in Unicode characters not bytes.


  // Return the format name if the queue is created successfully.

  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;

}


int _tmain(int argc, _TCHAR* argv[])
{
std::cout<<"started!";
LPWSTR sam = L".\\myQueue";
LPWSTR out_name = L"Sampleoutput";
DWORD d1 = 60;
DWORD *dd = &d1;
HRESULT hs = CreateMSMQQueue(sam,pt,out_name,dd);
return 0;
}

我没有改变太多的代码。当我运行此代码时,我得到一个“应用程序无法正确启动(0x0000142)”错误。通过调试我发现它是由于“内存访问错误”,但没有提示它发生的位置。请帮帮我!

队列名称是否有任何格式?或输出格式名称?

0 个答案:

没有答案