CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Mar 2009
    Posts
    20

    Question exception during raw disk write ........plz help

    hey guys any one plz help me i would be highly thankful...avtually i a m trying to read and write raw disk sectors using vc++.i am able to read properly but as i write i get an exception which i am not able to sort out..the problem is the bytes are written on destination but after writing the application gives exception plz help me out..exception is in the writefile function line

    here goes the program
    Code:
    // cod.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "cod.h"
    //#include <iostream.h>
    #include <winnt.h>
    #include <Winbase.h>
    #include <conio.h>
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    #ifdef UNICODE
    #undef UNICODE
    #endif
    
    // The one and only application object
    
    CWinApp theApp;
    
    //using namespace std;
    
    
            
    
            int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {		
    	
    	DWORD bytesread;
    	char hexcode[10];
    	DWORD sector=0;
    DWORD sectorN=1;
    
    		char* buffer = (char*)malloc (512);
    		
    	int drive=0;
    	HANDLE hDevice= CreateFileA("\\\\.\\N:",GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,NULL, OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL); 
    	if (hDevice == INVALID_HANDLE_VALUE) 
            printf("Error");
    	else
    		printf("Success\n");
    
    	SetFilePointer (hDevice,(sector*512), NULL, FILE_BEGIN); 
    if (!ReadFile (hDevice, buffer, 512*sectorN, &bytesread, NULL) )
    		 printf("unable to read");
    
    for (int i=0; i<512; i++)
    	{
    		if(i%16==0)
    			printf("\n");
    printf("%02X ",buffer[i]&0xff);
    	}
    
    
    
    printf("\n");
    
    for (int i=0; i<512; i++)
    	{
    buffer[i]=12;
    }
    
    
    SetFilePointer (hDevice, (sector*512), NULL, FILE_BEGIN); 
    LPDWORD bytesN=0;BOOL h;
    
    
    try{
    
    h=WriteFile(hDevice,buffer,512,bytesN,NULL);   //this is the part where i am trying to write plz help 
    
    }catch(CException *e){printf("exception");}
    
    
    
    printf("\n\nWritten\n");
    
    
    	for (int i=0; i<512; i++)
    	{
    		if(i%16==0)
    			printf("\n");
    printf("%02X ",buffer[i]&0xff);
    
    	}
    getch();
    return 0;
    
    }


    EXCEPTION IT GIVES IS:


    Unhandled exception at 0x7c811024 in cod.exe: 0xC0000005: Access violation writing location 0x00000000.



    if it is unable to write then how can data be still written..


    i had also attached the source file and the exception i get screenshot


    help me out guys plzzzzzz
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by vikrant3mahajan; March 7th, 2009 at 10:18 AM. Reason: formatting the post

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
  •  





Click Here to Expand Forum to Full Width

Featured