Add an extra two bytes for zero termination. iFileLength = GetFileSize (hFile, NULL) ; pBuffer = malloc (iFileLength + 2) ;
// Read file and put terminating zeros at end. ReadFile (hFile, pBuffer, iFileLength, &dwBytesRead, NULL) ; CloseHandle (hFile) ; pBuffer[iFileLength] = '\0' ; pBuffer[iFileLength + 1] = '\0' ;
// Test to see if the text is Unicode iUniTest = IS_TEXT_UNICODE_SIGNATURE | IS_TEXT_UNICODE_REVERSE_SIGNATURE ; if (IsTextUnicode (pBuffer, iFileLength, &iUniTest)) { pText = pBuffer + 2 ; iFileLength -= 2 ;
if (iUniTest & IS_TEXT_UNICODE_REVERSE_SIGNATURE) { for (i = 0 ; i < iFileLength / 2 ; i++) { bySwap = ((BYTE *) pText) [2 * i] ; ((BYTE *) pText) [2 * i] = ((BYTE *) pText) [2 * i + 1] ; ((BYTE *) pText) [2 * i + 1] = bySwap ; } }
// Allocate memory for possibly converted string pConv = malloc (iFileLength + 2) ; // If the edit control is not Unicode, convert Unicode text to // non-Unicode (i.e., in general, wide character). #ifndef UNICODE WideCharToMultiByte (CP_ACP, 0, (PWSTR) pText, -1, pConv, iFileLength + 2, NULL, NULL) ; // If the edit control is Unicode, just copy the string #else lstrcpy ((PT |