tags:

views:

49

answers:

2

Hello,

I'm trying to read the contents of a small text file using the common dialog box, pass the text in the file into a buffer and draw it to the form by invalidating the window and forcing the repaint.

Everything works with exception to displaying the text on the screen, when I click on the OK button on the dialog box no text appears.

I'm new to C so I might of missed a keyword or used an incorrect pointer.

Here's a snippet of my code so far:

LRESULT CALLBACK WindowFunc(HWND hMainWindow, UINT message, 
                            WPARAM wParam, LPARAM lParam)
{
    CHAR fileText[1024];
    HDC hdc;
    OPENFILENAME ofn;
    TCHAR szFile[MAX_PATH];
    HANDLE fileHandle;
    RECT clientArea;
    PAINTSTRUCT pStruct;

    // Act on current message
    switch(message)    
    {
    case WM_CREATE:
        AddMenus(hMainWindow);
        break;

    case WM_COMMAND:


        switch(LOWORD(wParam))
        {
        case IDM_FILE_OPEN:

            ZeroMemory(&ofn, sizeof(ofn));
            ofn.lStructSize = sizeof(ofn);
            ofn.lpstrFile = szFile;
            ofn.lpstrFile[0] = '\0';
            ofn.hwndOwner = hMainWindow;
            ofn.nMaxFile = sizeof(szFile);
            ofn.lpstrFilter = TEXT("All files(*.*)\0*.*\0");
            ofn.nFilterIndex = 1;
            ofn.lpstrInitialDir = NULL;
            ofn.lpstrFileTitle = NULL;
            ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

            if(GetOpenFileName(&ofn))
            {
                fileHandle = CreateFile(&ofn.lpstrFile,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
                ReadFile(fileHandle,fileText,1023,0,0);
                CloseHandle(fileHandle);
            }

            GetClientRect(hMainWindow, &clientArea);
            InvalidateRect(hMainWindow,
                            &clientArea,
                            TRUE
                            );          

            break;
        case IDM_FILE_QUIT:
            SendMessage(hMainWindow, WM_CLOSE, 0, 0);
            break;

        }
        break;

    case WM_PAINT:

        hdc = BeginPaint(hMainWindow, &pStruct);

        DrawTextA(hdc,
                fileText, 
                -1,
                &clientArea,
                DT_WORDBREAK);

        EndPaint(hMainWindow, &pStruct);

        break;

    case WM_DESTROY:

        PostQuitMessage(0);
        break;

    default:
        return DefWindowProc(hMainWindow, message, wParam, lParam);
    }
    return 0;
}

Where am I going wrong exactly? Also I know it's not perfect in it's conception as my aim is to simply get it working and then re factor it afterwards.

Thank you for your time.

A: 

This: ReadFile(fileHandle,fileText,1023,0,0);

Should be: ReadFile(fileHandle, &fileText,1023,0,0);

use a char* instead of an array. A char pointer basically will point to the first item in the array.

Tony
Hmm? int main(void) { char array[15] = {0}; if ((void*)array != (void*) }
Bertrand Marron
+1  A: 

CreateFile(W)'s first parameter's type is LPCWSTR (Constant WString).

ofn.lpstrFile is a LPWSTR (WString)

So &ofn.lpstrFile is wrong in your code (being a LPWSTR*).

Bertrand Marron
What would be the correct version?
Jamie Keeling
You want a `LPCWSTR` (`const wchar_t*`). ``
Bertrand Marron