CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2019
    Posts
    53

    [RESOLVED] Reading contents of file into SetWindowTextA for output into an Edit field of window.

    Can anyone assist?

    I'm trying to read the contents of a file opened with GetOpenFileNameA(&ofn) into an editable text-box part of a Window created with WinApi.

    I'm using the SetWindowTextA function to output the contents of the file...

    I'm not sure how I should read the contents of lpszFile from the OPENFILENAMEA struct into the second parameter of SetWindowTextA.

    Can anyone suggest how to do this so it shows up in the following window type?

    Code:
    CreateWindow(L"Button", L"Open File", WS_VISIBLE | WS_CHILD, 10, 10, 150, 36, hWnd, (HMENU)OPEN_FILE, NULL, NULL);
    
    HANDLE hEdit = CreateWindow(L"Edit", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_AUTOVSCROLL | ES_MULTILINE, 10, 50, 400, 300, hWnd, NULL, NULL, NULL);

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Use CreateFile / ReadFile Win32 API functions
    Victor Nijegorodov

  3. #3
    Join Date
    May 2019
    Posts
    53

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Quote Originally Posted by VictorN View Post
    Use CreateFile / ReadFile Win32 API functions
    Oh... seems I found the solution using your help... though there are still problems...:

    Code:
    HANDLE file = CreateFile(ofn.lpstrFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD fileSize = GetFileSize(file, NULL);
    LPSTR buffer = (LPSTR)GlobalAlloc(GPTR, fileSize + 1);
    DWORD read;
    
    ReadFile(file, buffer, fileSize, &read, NULL);
    
    SetWindowTextA(hEdit, buffer);
    Can you see any problems with the above code?

    I also still have the problem that the text is invisible inside the text box until the user HIGHLIGHTS (click-drags mouse over it) it.
    Any idea how to cause the text box (hEdit) to refresh after the SetWindowTextA operation?
    Last edited by Arianax; May 26th, 2019 at 10:33 AM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Victor Nijegorodov

  5. #5
    Join Date
    May 2019
    Posts
    53

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Thanks Victor, I found the answer by myself after you explained, it's in my edited above post...

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Quote Originally Posted by Arianax View Post
    Oh... seems I found the solution using your help... though there are still problems...:

    Code:
    HANDLE file = CreateFile(ofn.lpstrFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD fileSize = GetFileSize(file, NULL);
    LPSTR buffer = (LPSTR)GlobalAlloc(GPTR, fileSize + 1);
    DWORD read;
    
    ReadFile(file, buffer, fileSize, &read, NULL);
    
    SetWindowTextA(hEdit, buffer);
    Can you see any problems with the above code?
    ...
    1. You nave to check the return values of all the Win32 API function calls.
    2. You must free all the dynamically allocated buffers: in your case it is
    Code:
    LPSTR buffer = (LPSTR)GlobalAlloc(GPTR, fileSize + 1);
    so call
    Code:
    GlobalFree(buffer);
    as it stated inn MSDN
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Why GlobalAlloc() after all?
    You can just use standard C++ memory allocation.
    Preferably using some kind of RAII object like std::vector.
    So, instead of GlobalAlloc(), create std::vector of the required size, and pass the address of the first element to ReadFile() to fill the vector with the data.
    All memory will be cleaned up automatically.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Quote Originally Posted by Marc G View Post
    Why GlobalAlloc() after all?
    You can just use standard C++ memory allocation.
    Preferably using some kind of RAII object like std::vector.
    So, instead of GlobalAlloc(), create std::vector of the required size, and pass the address of the first element to ReadFile() to fill the vector with the data.
    All memory will be cleaned up automatically.
    From looking at the posts, I sort of assumed this was c code and not c++ ??...
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Quote Originally Posted by 2kaud View Post
    From looking at the posts, I sort of assumed this was c code and not c++ ??...
    I don't assume so.
    But it it was so then why not use malloc/free instead?
    Victor Nijegorodov

  10. #10
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Reading contents of file into SetWindowTextA for output into an Edit field of win

    Quote Originally Posted by 2kaud View Post
    From looking at the posts, I sort of assumed this was c code and not c++ ??...
    I assumed C++, this section of the forum is called "C++ and WinAPI" after all.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured