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):
Code:
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;
hFile = CreateFile( szFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,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);
PrintBuff(szFormatted,"log1");
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.