How do you compare the content of an edit control to the text in a file?


I am writing a simple text editor using the Win32 API and I am trying to write a function to compare the content of the file to the content of an edit control. I currently have this:

BOOL checkForModification (PCWSTR pszFileName, HWND hEdit) {
    BOOL bSuccess = FALSE;
    DWORD dwTextLength = GetWindowTextLengthA(hEdit);
    hFile = CreateFile(pszFileName, GENERIC_READ, 
        OPEN_EXISTING, 0, NULL);
    if (hFile != INVALID_HANDLE_VALUE) {
            DWORD dwFileSize;

            dwFileSize = GetFileSize(hFile, NULL);
            if (dwFileSize != 0xFFFFFFFF)
                PSTR pszFileText;

                pszFileText = (PSTR)GlobalAlloc(GPTR, dwFileSize + 1);
                if (pszFileText != NULL) {
                    DWORD dwRead;

                    if (ReadFile(hFile, pszFileText, dwFileSize + 1, &dwRead, NULL))
                        bSuccess = TRUE;
                        pszFileText[dwFileSize] = 0; 

                        LPSTR pszEditText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1);
                        GetWindowTextA(hEdit, pszEditText, dwTextLength);

                        int res = CompareStringA(LOCALE_SYSTEM_DEFAULT, NULL, pszFileText, -1, pszEditText, -1);
                        if (res != CSTR_EQUAL) {
                            MessageBox(NULL, L"You changed the text!", L"Alert", MB_OK | MB_ICONINFORMATION);


                    else {
                        MessageBox(NULL, L"Oh no! Something went wrong!\nError code: 2", L"Error", MB_OK | MB_ICONERROR);
        else {
            MessageBox(NULL, L"Oh no! Something went wrong!\nError code: 1", L"Error", MB_OK | MB_ICONERROR);
    return bSuccess;

The problem that I am having is that the result of CompareStringA is always returning CSTR_LESS_THAN, even when I don't change the text in the edit control. The encoding of the file is UTF-8. Why is this happening?


2 Answers


Seriously, use a debugger and test it with a file that contains simple text like ABCDE. You should be able to figure out the problem in less than 30 seconds just by inspecting a few variables!

You can trivially determine that the problem is not reading the documentation of function GetWindowTextA (

The size you pass include the null terminating charaters. Assuming that the edit also contains ABCDE, then the length is 5.

Calling GetWindowTextA(hEdit, pszEditText, dwTextLength); where dwTextLength will returns a buffer that contains ABCD plus the null character.

Obviously ABCD is before ABCDE using usual sorting rules.

answered on Stack Overflow Jan 17, 2021 by Phil1970

Actually, for my purposes, I have found it to be easiest to just use the Edit_GetModify macro to see if any edits have been made to the text in the edit control. While this doesn't do exactly what I want (it will return TRUE even if you undo your changes), it is more efficient than having to read the entire file then compare its entire contents to that of the edit control.

answered on Stack Overflow Jan 18, 2021 by professionalgrammer • edited Jan 18, 2021 by acraig5075

User contributions licensed under CC BY-SA 3.0