2008-09-23 5 views
3

C++ API에서 비공개 메시지 큐를 작성, 추가, 읽기 및 삭제할 수있는 방법에 대한 실제 예제를 제공 할 수 있습니까? MSDN 코드 조각을 시도했지만 제대로 작동하지 않을 수 있습니다.C++의 MSMQ 샘플?

감사

+0

MSDN 샘플 코드에 어떤 문제점이 있습니까? 오류를 게시 한 경우 우리는 당신을 도울 기회가 더 많을 수 있습니다. –

+0

Seb와 동의합니다. 조금 질문을 명확히 할 수 있습니까? –

답변

-1

작성하거나 메시지 큐를 파괴에 대한 가고 싶어하는 방법을 아주 확실하지. Windows는 스레드 당 하나씩 만들어야합니다.

MFC를 사용하는 경우 모든 CWinThread 및 CWnd 파생 클래스에는 PostMessage 또는 PostThreadMessage 및 ON_COMMAND 매크로를 사용하여 액세스 할 수있는 메시지 큐가 있습니다. 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 설명서의 예제입니다. 당신이 사용하고있는 것입니까? 작동하지 않는 것은 무엇입니까?

+1

그는 Window Proc가 아니라 MSMQ를 언급하고 있습니다. –

2

Actualy이 난에 관심 코드입니다 :

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

이것은 아마도 큐를 생성은 ...하지만이 작업을 위해 내가 작동하는 간단한 예제가 필요한 이유, 그건 실종 일부가있다.

+0

어떤 부분이 작동하지 않습니까? MQCreateQueue가 오류를 리턴합니까? 또는 문제점이 더 이상 발생하지 않습니까? – jeffm