0

I'm using Visual Studio 2022 Community.

For Some reason, I use OutputDebugString() with very long strings. Sometime, It doesnot works.

What I found.

When string length is larger than 1024 * 32, OutputDebugString() doesnot work.

For such long string to work with OutputDebugString(), I have to add 2 NULL at the end of the string..

Is this intended action??

#include <stdio.h>
#include <windows.h>
#include <tchar.h>

void testOutputDebugging( int sizeBuf ) {
    TCHAR *buf = (TCHAR *)calloc( sizeBuf * sizeof( TCHAR ), 1 );
    for( int i = 0; i < sizeBuf - 1; i++ )
        buf[ i ] = _T( 'A' );

    TCHAR buf2[32];
    _stprintf_s( buf2, 32, _T( "\nsize=%d\n" ), sizeBuf );
    OutputDebugString( buf2 );
    OutputDebugString( buf );
    OutputDebugString( buf2 );
    free( buf );
}

int main() {
    testOutputDebugging( 1024 );
    testOutputDebugging( 1024 * 16 );
    testOutputDebugging( 1024 * 17 );
    testOutputDebugging( 1024 * 32 );
    return 0;
}

I ran the with VS2022. Result screen shot looks like this..

enter image description here

Peter Lee
  • 136
  • 9
  • 1
    Yes. The underlying shared memory buffer [used to be 4K](https://stackoverflow.com/a/5627428/17034), now its 64K. Progress. – Hans Passant Mar 20 '23 at 07:42
  • @HansPassant Is OutputDebugString() related with shared memory?? My Sample code use 1 NULL for string terminator, but When I use 2 NULLs, it works fine.. – Peter Lee Mar 20 '23 at 09:50
  • 1
    Debuggers run out-of-process so a mechanism is needed to transfer the string from one process to another. DBWIN_BUFFER is used for that, it is shared memory that both processes have access to. Its size is limited, as you found out, 64KB on recent Windows versions. 2 NULLs is not actually a workaround, you probably concluded that by writing one character less, thus staying below the limit. – Hans Passant Mar 20 '23 at 10:43
  • @HansPassant You are right. I tested more than 32K.. It fails always.. Obviously, It shows that DebugOutputString() has 32K limitation. Thanks a lot. – Peter Lee Mar 20 '23 at 15:18

1 Answers1

0

I found out that removing the '\r' from the string fixes the issue. When a string contains the '\r' character, the function OutputDebugString() would sometimes display only 20 characters, but when I scan the string and replace the '\r' with a space, I can display well over 500 characters in a single call of OutputDebugString().

Daniel
  • 51
  • 3