Thread: Problems posting a message from the buffer

  1. #1
    Registered User
    Join Date
    Mar 2004

    Unhappy Problems posting a message from the buffer

    Hi all,
    I am new to windows programming (and not much better at programming in general) and am having a problem.

    This is the readers digest version of my code (hopefully I havent left out anything pertanent):

    BOOL ReadFile( HWND hWnd, TCHAR *szFilePath, BOOL *bModified, BOOL *bUnicode )
        int   iFileLength     = 0;
        HANDLE hFile          = 0;
        HWND   hWndView       = 0;
        TCHAR *szFormatted    = {0};
        HLOCAL hLocal         = 0;
        iFileLength = GetFileSize( hFile, NULL );
       // Allocate small buffer
       hLocal = LocalAlloc( LMEM_MOVEABLE, 80 );	
       szFormatted  = (TCHAR*)calloc( iFileLength + (sizeof(TCHAR)*2), sizeof(TCHAR) );
       // Set edit box's handle
       PostMessage( hWndView, EM_SETHANDLE, (WPARAM)hLocal, 0 );	
       // Reallocate buffer
       LocalReAlloc( hLocal, (iFileLength*2)+sizeof(TCHAR), LMEM_MOVEABLE );	
       // Lock it
       pVoidBuf = LocalLock( hLocal );	
        szFormatted = InterpretScript(szPreFormat,iFileLength);
        if (szFormatted != NULL)
    	memcpy( pVoidBuf, szFormatted, iFileLength*2);
        	PrintBuff((TCHAR *)pVoidBuf,"log2");
       LocalUnlock( hLocal);
       // Tell edit box it can repaint now
       PostMessage( hWndView, WM_SETREDRAW, TRUE, 0 );   
    What goes to the screen is an abbreviated version of szFormatted and pVoid. I am not certain why. The really wierd part is that it seems to be a random variation. It is a little different every time I run the exe given the same compilation .

    One example: if szFormatted is the string:
    SET $9 0 SAY "W000D/

    The window will show:
    Set $9 0
    90% fo the time with an occasional
    Set $9 0 S
    or an even more rare
    Set $9 0 SAY "WOO

    in my observations, it has never been shorter than the most common output.

    InterpretScript is not represented here, but I have verified its output with the PrintBuff, and I am confident the problem is not there.

    The print buffs were just a debug to give me a warm-fuzzy that memcpy was working as expected (it was).

    My first hunch was that the LocalReAlloc was not sufficient, but I have manually made it larger with no change in printed characters.

    I suspect (hope) its a simple mis-understanding of PostMessgae usage or TCHAR's, but at this point Im lost.

    Any help would be appreciated.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    I hope this is actually the cliff-notes of the cliff-notes of the readers-digest version.......

    Looks like you're using EM_SETHANDLE wrong and the memory calloc'd to szFormatted is leaked.

    What exactly are you trying to do with the edit box? If you just want to change it's contents, use WM_SETTEXT.


  3. #3
    Registered User
    Join Date
    Mar 2004
    The memory is not actually leaked, I forgot to include the free statement.

    The edit box is actually a text editor window. I am modifying an open source text editor for my own purposes.

    The line

    PostMessage( hWndView, EM_SETHANDLE, (WPARAM)hLocal, 0 );
    is from the original functional code and is far more likely to be correct than my own additions.


  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    I can only comment on the code that was posted.....which is wrong.

    Read up on EM_SETHANDLE and EM_GETHANDLE on MSDN. Perhaps understanding why the original code worked in the first place will help you understand why your additions have broken it.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem in message handling VC++
    By 02mca31 in forum Windows Programming
    Replies: 5
    Last Post: 01-16-2009, 09:22 PM
  2. clear buffer
    By justins in forum C Programming
    Replies: 5
    Last Post: 05-19-2007, 06:16 AM
  3. Posting a message to a child window
    By axr0284 in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2005, 09:35 AM
  4. posting a mouse message to a window
    By marc74 in forum Windows Programming
    Replies: 1
    Last Post: 01-09-2005, 09:31 AM