[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);
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
Use CreateFile / ReadFile Win32 API functions
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
Quote:
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?
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
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...
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
Quote:
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
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.
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
Quote:
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++ ??...
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
Quote:
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?
Re: Reading contents of file into SetWindowTextA for output into an Edit field of win
Quote:
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.