Factor Language Blog

Windows CE Pocket Console crash when writing long strings

Friday, November 9, 2007

This one was a pain to track down. If you write a string longer than about 1024 characters to the console, it just crashes your application. Test case:

#include <windows.h>

#define SIZE 1124

int main() {
        HANDLE h = _fileno(_getstdfilex(1));
        char buf[SIZE];
        memset(buf,'x',SIZE);
        unsigned int count = 0;
        printf("About to call WriteFile()\n");
        int result = WriteFile(h,buf,SIZE,&count,NULL);
        printf("\n");
        printf("WriteFile returns %d\n",result);
}

It never gets as far as “WriteFile returns…”. If you change SIZE to something small like 124 it works.