2010. 3. 8. 23:10

OutputDebugString 캡처하기 - DebugView 를 만들어보자

디버깅시 유용한 DebugView 는 OutputDebugString 을 캡처하여 보여주는 역할을 한다. 이와 동일한 기능을 구현하는 코드 예는 다음과 같다.

 

////////////////////////////////////////////////////////////////////////////////////////////////

    struct DbWinBuffer

    {

        DWORD dwProcessId;

        char data[4096 - sizeof(DWORD)];

    };

 

    DbWinBuffer* dbBuffer;

 

    HANDLE hAckEvent;

    HANDLE hEvent;

    HANDLE hSharedFile;

 

    SECURITY_DESCRIPTOR sd;

    SECURITY_ATTRIBUTES sa;

 

    ////////////////////////////////////////////////////////////////////////////////////////////////

 

    sa.nLength = sizeof(SECURITY_ATTRIBUTES);

    sa.bInheritHandle = true;

    sa.lpSecurityDescriptor = &sd;

 

    if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))

    {

        printf("ERROR: InitializeSecurityDescriptor\\n");

        return 1;

    }

 

    if (!SetSecurityDescriptorDacl(&sd, true, 0, false))

    {

        printf("ERROR: SetSecurityDescriptorDacl\n");

        return 1;

    }

 

    hAckEvent = CreateEvent(&sa, false, false, L"DBWIN_BUFFER_READY");

    if (!hAckEvent)

    {

        printf("ERROR: CreateEvent(\\"DBWIN_BUFFER_READY\\")\\n");

        return 1;

    }

 

    hEvent = CreateEvent(&sa, false, false, L"DBWIN_DATA_READY");

    if (!hEvent)

    {

        printf("ERROR: CreateEvent(\\"DBWIN_DATA_READY\\")\\n");

        return 1;

    }

 

    hSharedFile = CreateFileMapping((HANDLE)-1, &sa, PAGE_READWRITE, 0, 4096, L"DBWIN_BUFFER");

    if(!hSharedFile)

    {

        printf("ERROR: CreateFileMapping(\\"DBWIN_BUFFER\\")\\n");

        return 1;

    }

 

    dbBuffer = static_cast<DbWinBuffer*>(MapViewOfFile(hSharedFile, FILE_MAP_READ, 0, 0, 4096));

    if(!dbBuffer)

    {

        printf("ERROR: MapViewOfFile\\n");

        return 1;

    }

 

    SetEvent(hAckEvent);

 

    DWORD pid = GetCurrentProcessId();

    printf("Tracing PID: %dnn", pid);

 

    for(;;)

    {            

        DWORD ret = WaitForSingleObject(hEvent, INFINITE);

        if (ret == WAIT_FAILED)

        {

            printf("ERROR: WaitForSingleObject\\n");

            return 1;

        }

 

        if (dbBuffer->dwProcessId == pid)

        {

            printf("%s", dbBuffer->data);

        }

 

        SetEvent(hAckEvent);

    }

 




http://www.devmaster.net/forums/showthread.php?t=11229