CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  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

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: exception during raw disk write ........plz help

    Ah, good old null pointer exception. I know thee well. However, in this case I think it's due to confusion regarding the WinAPI typedefs rather than anything tricky.

    Code:
    LPDWORD bytesN=0;
    h=WriteFile(hDevice,buffer,512,bytesN,NULL);
    An LPDWORD stands for "long pointer to double word", which is a fancy way of saying int*. In other words, bytesN needs to be a pointer to int. The WriteFile documentation indicates that this is an out parameter, so clearly the function is going to dereference the pointer and write a value into the int there.

    You, however, are providing a null pointer. Hence the error.

    The proper way to use that sort of interface is to do like such:

    Code:
    DWORD bytesN=0;
    h=WriteFile(hDevice,buffer,512,&bytesN,NULL);
    Do you see and understand the difference?

    I haven't looked at the other parameters in depth, so it's possible there may be more problems.

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: plzzzz help me with this prg any one ....urgent

    Code:
    WriteFile(hDevice,buffer,512,bytesN,NULL)
    should be:

    Code:
    WriteFile(hDevice,buffer,512,&bytesN,NULL)
    I'm not sure how it compiled.

  4. #4
    Join Date
    Mar 2009
    Posts
    20

    Talking Re: plzzzz help me with this prg any one ....urgent

    hey man....thanx a hell lot ...you are the saviour ..thnx a lot..u solved the prob

  5. #5
    Join Date
    Feb 2005
    Posts
    2,160

    Re: plzzzz help me with this prg any one ....urgent

    Lindley beat me to it.

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