Problem with opening files in a simple notepad project

Hello,
recently i've started learning WINAPI and made my project based on http://www.winprog.org/tutorial/app_two.html but when i debug it and go to file/open in the menu nothing happens... :/ I compared the codes few times with the tutorial and it seems to be pretty much the same however mine doesn't work.

Here's my code for handling of the message in the window procedure:

1
2
3
case ID_FILE_OPEN:
				DoFileOpen(hwnd);
				break;



Here's my code for DoFileOpen function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void DoFileOpen(HWND hwnd){
	OPENFILENAME ofn;
	char FileName[MAX_PATH]="";

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize=sizeof(OPENFILENAME);
	ofn.hwndOwner=hwnd;
	ofn.lpstrFilter="Text files (*.txt)\0*.txt\0All files(*.*)\0*.*\0";
	ofn.lpstrFile=FileName;
	ofn.Flags=OFN_EXPLORER | OFN_FILEMUSTEXIST;
	ofn.lpstrDefExt="txt";
	
	if(GetOpenFileName(&ofn)){
		HWND hCtrl=GetDlgItem(hwnd,IDC_MAIN_EDIT);			
		LoadTextFile(hCtrl,FileName);
	}
}




and here's my code for BOOL LoadTextFile function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
BOOL LoadTextFile(HWND hCtrl, LPCTSTR szFileName){
	HANDLE hFile;
	BOOL Success=FALSE;

	hFile=CreateFile(szFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);

	if(hFile != INVALID_HANDLE_VALUE){
		DWORD dwFileSize;

		dwFileSize=GetFileSize(hFile,NULL);
		if(dwFileSize != 0xFFFFFFFF){
				LPSTR FileText;

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

					if(ReadFile(hFile, FileText,dwFileSize,&dwRead,NULL)){
						FileText[dwFileSize]=0;
						if(SetWindowText(hCtrl,FileText))
							Success=TRUE;
					}
					GlobalFree(FileText);
				}
		}
		CloseHandle(hFile);
	}
	return Success;
}



I'd appreciate any kind of help because I'm pretty much stuck... :/
Last edited on
As per MSDN Online, the filter needs to end with two null chars, not just one. Besides that, I don't see much of an issue, but just in case, you need to read http://msdn.microsoft.com/en-us/library/ms646927(VS.85).aspx and http://msdn.microsoft.com/en-us/library/ms646839(VS.85).aspx to be 100% sure.

Also note that for Windows Vista and 7, this function is superseded.
According to the tutorial "(...)the compiler will add the second one at the end as it
always does with string constants" but anyways i added another null char and there's no difference. :/
webJose wrote:
Also note that for Windows Vista and 7, this function is superseded.


I wasn't aware of this. That's why it's good to read posts even if you may not be interested in them.

The good thing about the new function is its versatility.

The bad thing is that it takes about fifteen thousand extra lines of code to even do the basics of opening the dialog. -:)
Last edited on
ok so nothing was working and the code seemed to be ok so i decided to rewrite it and when i was assining ZeroMemory to ofn I noticed that after putting a comma after &ofn the compiler still thought that sizeof(ofn) was still a part of destination and not length so all it took was to put brackets around the address of OPENFILNAME object. :)
Topic archived. No new replies allowed.