[Box Backup-dev] Problem with console_read() on Windows XP

Mark boxbackup-dev at fluffy.co.uk
Thu Feb 23 21:55:00 GMT 2006


Martin Ebourne wrote:
> On Thu, 2006-02-23 at 22:38 +0100, Mark wrote:
>   
>> Hi Chris,
>>
>> I think the problem is that the BufferSize parameter of console_read(..) 
>> is only 4. This is weird, because the size of the buffer variable 
>> (mBuffer) as it is declared in FdGetLine is 1024. Something goes wrong 
>> with sizeof(mBuffer) I think.
>>     
>
> That'd be because it's turned it into a pointer, so you get size of
> pointer, not array.
>
> Cheers,
>
> Martin.
>
> _______________________________________________
> Boxbackup-dev mailing list
> Boxbackup-dev at fluffy.co.uk
> http://lists.warhead.org.uk/mailman/listinfo/boxbackup-dev
>   
Hi Martin and Chris,

If that is the case then would this code ever work:

int console_read(char* pBuffer, size_t BufferSize)
{
    HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);

    if (hConsole == INVALID_HANDLE_VALUE)
    {
        ::fprintf(stderr, "Failed to get a handle on standard input: "
            "error %d\n", GetLastError());
        return -1;
    }

    int WideSize = BufferSize /
5;                                                        <<<<<<
WideSize = 0
    WCHAR* pWideBuffer = new WCHAR [WideSize];                   <<<<<<
No buffer

    if (!pWideBuffer)
    {
        ::perror("Failed to allocate wide character buffer");
        return -1;
    }

    DWORD numCharsRead = 0;

    if (!ReadConsoleW(
            hConsole,
            pWideBuffer,
            WideSize -
1,
<<<<<<<<<<< Read -1 characters from the console
            &numCharsRead,
            NULL // reserved
        ))
    {
        ::fprintf(stderr, "Failed to read from console: error %d\n",
            GetLastError());
        return -1;
    }


Or don't I get it, and is there nothings wrong with the code above?

If I understand the code, we ask the function ReadConsoleW(..) to read
-1 characters in a buffer with a length of zero.

Mark




More information about the Boxbackup-dev mailing list