WriteFile



  • hi ich bins mal wieder....

    die datei wird erstellt nur hat er mit WriteFile irgendein Problem zumindest schreibt er nicht in die erstellte datei... Was hab ich falsch gemacht ? die MSDN gibt mir auch nicht die erwünschte lösung..:(

    #include <windows.h>
    #include "resource.h"
    #include <commdlg.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PSTR szCmdLine, int iCmdShow)
    {
    	static char szAppName[] = "Name" ;
    	HWND        hwnd ;
    	MSG         msg ;
    	WNDCLASSEX  wndclass ;
    
    	wndclass.cbSize        = sizeof (wndclass) ;
    	wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
    	wndclass.lpfnWndProc   = WndProc ;
    	wndclass.cbClsExtra    = 0 ;
    	wndclass.cbWndExtra    = 0 ;
    	wndclass.hInstance     = hInstance ;
    	wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    	wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
    	wndclass.lpszMenuName  = NULL ;
    	wndclass.lpszClassName = szAppName ;
    	wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
    	wndclass.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU2);
    
    	RegisterClassEx (&wndclass) ;
    
    	hwnd = CreateWindow (szAppName, "C&D Editor", WS_OVERLAPPEDWINDOW,
    	CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
    	NULL, NULL, hInstance, NULL) ;
    
    	ShowWindow (hwnd, iCmdShow) ;
    	UpdateWindow (hwnd) ;
    
    	while (GetMessage (&msg, NULL, 0, 0))
    	{
    		TranslateMessage (&msg) ;
    		DispatchMessage (&msg) ;
    	}
    	return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
    
    	OPENFILENAME ofn;       // common dialog box structure
    	char szFile[260];       // buffer for file name
    	HANDLE hf;              // file handle
    
    	// Initialize OPENFILENAME
    	ZeroMemory(&ofn, sizeof(ofn));
    	ofn.lStructSize = sizeof(ofn);
    	ofn.hwndOwner = hwnd;
    	ofn.lpstrFile = szFile;
    	//
    	// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
    	// use the contents of szFile to initialize itself.
    	//
    	ofn.lpstrFile[0] = '\0';
    	ofn.nMaxFile = sizeof(szFile);
    	ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
    	ofn.nFilterIndex = 1;
    	ofn.lpstrFileTitle = NULL;
    	ofn.nMaxFileTitle = 0;
    	ofn.lpstrInitialDir = NULL;
    	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
    
    	// Display the Open dialog box.
    
    	HDC hdc;
    	PAINTSTRUCT ps;
    	static HINSTANCE hInstance;
    	static HWND hEditMain;
    	int platzzuallenseiten=0;
    	//LPVOID lpBuffer;
    	static char filename[] = "";  
        DWORD filesize;
    	DWORD bytesRead = 0; 
    
    	switch (iMsg)
    	{
    		case WM_PAINT:
    		{
    			hdc = BeginPaint (hwnd, &ps);
    			EndPaint (hwnd, &ps);
    			break;
    		}
    		case WM_CREATE:
    		{
    			hEditMain = CreateWindowEx(WS_EX_CLIENTEDGE,"Edit","",WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE |ES_AUTOVSCROLL,0, 0, 0, 0,hwnd,NULL,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
    			break;
    		}
    		case WM_SIZE:
    		{
    			MoveWindow(hEditMain,platzzuallenseiten,platzzuallenseiten,LOWORD(lParam)-2*platzzuallenseiten,HIWORD(lParam)-2*platzzuallenseiten,1);
    
    		}
    		case WM_COMMAND:
    		{
    			switch(wParam)
    			{
    				case ID_DATEI_SPEICHERN:
    				{
    // um den folgenenden codebschnitt handelt s sich 
    			    	if (GetSaveFileName(&ofn)==TRUE)
    			 hf = CreateFile(ofn.lpstrFile,          
                 GENERIC_WRITE,                
                 0,                           
                 NULL,                        
                 CREATE_ALWAYS,                
                 FILE_ATTRIBUTE_NORMAL |      
                 FILE_FLAG_OVERLAPPED,         
                 NULL);    
    
    				  DWORD bytesRead = 0; 
                      filesize = GetFileSize(hf, NULL); 
    				  int nLength = GetWindowTextLength(hEditMain)+1; 
    				  LPTSTR pszText = new TCHAR[nLength]; 
    				  GetWindowText(hEditMain, pszText, nLength); 
    
                      WriteFile(hf, pszText, nLength, &bytesRead, NULL); // was ist daran falsch ?
    				  CloseHandle(hf); 
    
    					break;
    				}
    				case ID_DATEI_OEFFNEN:
    				{
    					if (GetOpenFileName(&ofn)==TRUE)
    					{
    					hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
    					0, (LPSECURITY_ATTRIBUTES) NULL,
    					OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
    					(HANDLE) NULL);
    					}
    					if (hf == INVALID_HANDLE_VALUE)
    					{
    						break;
    					}  
    					else { 
    
                      filesize = GetFileSize(hf, NULL); 
    				  char textdaten[10000];
    				  ZeroMemory(textdaten,sizeof(textdaten));
    
                      ReadFile(hf, &textdaten, filesize, &bytesRead, NULL); 
    				  CloseHandle(hf); 
                      SetWindowText(hEditMain, textdaten); 
    					}
    		break;
    				}
    
    			}return 0;
    
    			break;
    		}
    		case WM_DESTROY :
    		{
    			PostQuitMessage (0) ;
    			return 0 ;
    		}
    	}
    	return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
    }
    

    so wie ich mich kenne ist es nur nen kleiner denkfehler .. 😉



  • Zitat MSDN:

    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Was sagt denn GetLastError?



  • Ist denn der CreateFile Aufruf erfolgreich?
    btw: der if(GetSaveFileName(&ofn)==TRUE) Block sollte sich über das ganze Schreiben etc. erstrecken -> { }



  • habs nun hinbekommmen..danke trotzdem


Anmelden zum Antworten