Problem ze znalezieniem błęda w prostym notatniku

0

Próbuję zrobić prosty program do odczytywania plików tekstowych w WinApi. Robię go na postawie kursu WinApi ze strony cpp0x.pl. Podczas próby wczytania pliku funkcja ReadFile zgłasza błąd wczytywania pliku.

#include <windows.h>

LPSTR g_strWindowClass = "Darek";
HWND g_hText;
LRESULT CALLBACK WindowEventProc( HWND hWnd, UINT uMsg,
	WPARAM wParam, LPARAM lParam );

void ReadFile(char * sFileName, HWND hWnd );

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow )
{
	WNDCLASSEX WindowClass;

	ZeroMemory( &WindowClass, sizeof(WNDCLASSEX) );
	WindowClass.cbSize = sizeof(WNDCLASSEX);
	WindowClass.lpszClassName = g_strWindowClass;
	WindowClass.lpfnWndProc = WindowEventProc;
	WindowClass.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	WindowClass.hCursor = LoadCursor( NULL, IDC_ARROW );
	WindowClass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );

	if (!RegisterClassEx( &WindowClass ))
	{
		MessageBox( NULL, "Nieudana rejestracja klasy okna.", "Blad", MB_ICONERROR );
		return 1;
	}

	HWND hWindow;
	hWindow = CreateWindowEx( NULL,
		g_strWindowClass,
		"Okno dialogowe 2",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		400,
		300,
		NULL,
		NULL,
		hInstance,
		NULL);

	if (!hWindow)
	{
		MessageBox( NULL, "Nieudane tworzenie okna.", "Blad", MB_ICONERROR );
		return 1;
	}

	RECT rcClient;
	GetClientRect( hWindow, &rcClient);
	g_hText = CreateWindowEx(NULL, "EDIT",
		"Tutaj mozna wyswietlic jakis tekst.", WS_CHILD |
		WS_VISIBLE | WS_BORDER | ES_MULTILINE | WS_VSCROLL | ES_AUTOVSCROLL,
		0, 0, rcClient.right, rcClient.bottom - 30, hWindow, NULL, hInstance, NULL);

	CreateWindowEx( 0, "BUTTON", "Plik...", WS_CHILD | WS_VISIBLE | WS_BORDER,
		0, rcClient.bottom - 27, 50, 25, hWindow, NULL, hInstance, NULL );

	ShowWindow( hWindow, nCmdShow );

	MSG Message;
	while (GetMessage(&Message, NULL , 0, 0))
	{
		TranslateMessage(&Message);
		DispatchMessage(&Message);
	}
	return static_cast<int>(Message.wParam);
}

LRESULT CALLBACK WindowEventProc( HWND hWnd, UINT uMsg,
	WPARAM wParam, LPARAM lParam )
{
	switch (uMsg)
	{
	case WM_COMMAND:
		{
		OPENFILENAME ofn;
		char sFileName[MAX_PATH] = "";

		ZeroMemory( &ofn, sizeof(ofn) );
		ofn.lStructSize = sizeof(ofn);
		ofn.lpstrFilter = "Pliki tekstowe (*.txt)\0*.txt\0Wszystkie pliki (*.txt)\0*.*\0";
		ofn.nMaxFile = MAX_PATH;
		ofn.lpstrFile = sFileName;
		ofn.lpstrDefExt = "txt";
		ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
		
		if (GetOpenFileName( &ofn ))
		{
			ReadFile( sFileName, g_hText );
		}
		}
		return 0;
	case WM_CLOSE:
		DestroyWindow(hWnd);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

void ReadFile(char * sFileName, HWND hWnd )
{
	LPSTR Buffer;
	DWORD dwSize, dwRead;
	HANDLE hFile;

	hFile = CreateFile( sFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL );
	if (hFile == INVALID_HANDLE_VALUE)
	{
		MessageBox( NULL, "Nieudany odczyt pliku.", "Blad", MB_ICONERROR );
		PostQuitMessage(1);
	}

	dwSize = GetFileSize( hFile, NULL );
	if (dwSize == 0xFFFFFFFF)
	{
		MessageBox( NULL, "Nieprawidlowy rozmiar pliku.", "Blad", MB_ICONERROR );
		PostQuitMessage(1);
	}

	Buffer = (LPSTR) GlobalAlloc( GPTR, dwSize + 1 );
	if (Buffer = NULL)
	{
		MessageBox( NULL, "Nieudany przydzial pamieci bufora.", "Blad", MB_ICONERROR );
		PostQuitMessage(1);
	}

	if (!ReadFile( hFile, Buffer, dwSize, &dwRead, NULL) )
	{
		MessageBox( NULL, "Blad czytania pliku.", "Blad", MB_ICONERROR );
		PostQuitMessage(1);
	}

	Buffer[dwSize] = '\0';
	SetWindowText( g_hText, Buffer );

	GlobalFree( Buffer );
	CloseHandle( hFile );
}
3
if(buffer = NULL)
0

Nie mogłem tego zauważyć :(.Teraz wszystko działa. Dzięki za poświęcenie czasu!

1 użytkowników online, w tym zalogowanych: 0, gości: 1