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

从C++(非COM组件)代码访问MSMQ,c++,runtime-error,msmq,C++,Runtime Error,Msmq,我不熟悉MSMQMicrosoft消息队列的概念。我从中瞥见了示例代码,并试图创建一个简单的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 <iostr

我不熟悉MSMQMicrosoft消息队列的概念。我从中瞥见了示例代码,并试图创建一个简单的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错误。通过调试,我发现它是由于内存访问错误引起的,但没有关于它发生在哪里的提示。请帮帮我


队列名称有任何格式吗?还是输出格式名称?

应用程序是否启动,即是否已开始打印?哪一行代码打印该消息?错误代码被错误引用,它是ix 0xC0000142。程序使用的某个DLL中的DllMain函数崩溃。这很糟糕,与您的代码没有任何关系。您需要寻找环境原因,例如反恶意软件。调试+异常,勾选Win32异常的抛出复选框可能有助于找到麻烦制造者。@HansPassant我打开了Win32异常,我得到了这个错误!仍然没有显示它发生在哪里。。Test.EXE中的第一个机会异常:FEFD47,940D:微软C++异常:内存位置的RealthyAccess错误0x0023eab0@MSalters不,它甚至没有开始打印。一旦运行,它就会关闭并显示该异常。听起来是可以修复的,请使用SysInternals的进程监视器查看它是否访问注册表。