-
May 26th, 2019, 07:30 AM
#1
[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);
-
May 26th, 2019, 09:18 AM
#2
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
Use CreateFile / ReadFile Win32 API functions
Victor Nijegorodov
-
May 26th, 2019, 10:20 AM
#3
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
 Originally Posted by VictorN
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.
-
May 26th, 2019, 10:29 AM
#4
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
Victor Nijegorodov
-
May 26th, 2019, 10:34 AM
#5
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...
-
May 26th, 2019, 10:52 AM
#6
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
 Originally Posted by Arianax
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
-
May 28th, 2019, 12:58 PM
#7
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.
-
May 28th, 2019, 03:00 PM
#8
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
 Originally Posted by Marc G
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.2)
-
May 28th, 2019, 03:16 PM
#9
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
 Originally Posted by 2kaud
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
-
May 29th, 2019, 01:50 AM
#10
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
 Originally Posted by 2kaud
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|