Access Violation when calling NTFSControlFile - Programmer

This is a discussion on Access Violation when calling NTFSControlFile - Programmer ; I keep getting an Access Violation error when my code calls NTFSControlFile() . I have checked all other parts of my app for any memory leaks etc. but nothing found so far. I have even moved the function to a ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Access Violation when calling NTFSControlFile

  1. Access Violation when calling NTFSControlFile

    I keep getting an Access Violation error when my code calls
    NTFSControlFile() . I have checked all other parts of my app for any
    memory leaks etc. but nothing found so far. I have even moved the
    function to a test MFC app, so that its the only fuinction being
    called. I still get the same error. The code is pasted below. Any help
    is most appreciated. The app is an MFC app in VS 20005:

    void CtestApp::test() {
    int nError = 0;
    PBITMAP_DESCRIPTOR bitMappings;
    IO_STATUS_BLOCK ioStatus;
    DWORD status;
    ULONGLONG startLcn;
    ULONGLONG nextLcn;
    ULONGLONG lastLcn;
    ULONGLONG cluster;

    #define BITMAPBYTES 4096
    #define BITMAPSIZE (BITMAPBYTES+2*sizeof(ULONGLONG))

    HANDLE m_volHandle=0;
    BYTE m_BitMap[ BITMAPSIZE ];
    bitMappings = (PBITMAP_DESCRIPTOR) m_BitMap;
    bitMappings->StartLcn = 0;
    bitMappings->ClustersToEndOfVol = 0;
    bitMappings->Map[0] = 0;

    m_volHandle = CreateFile(_T("\\\\?\\E:"), GENERIC_READ,
    FILE_SHARE_READ|FILE_SHARE_WRITE,
    NULL, OPEN_EXISTING, 0, 0 );
    status = NtFsControlFile(m_volHandle, NULL, NULL, 0, &ioStatus,
    FSCTL_GET_VOLUME_BITMAP, &nextLcn,sizeof( cluster ),
    bitMappings, BITMAPSIZE);
    CloseHandle(m_volHandle);


  2. Re: Access Violation when calling NTFSControlFile

    sengupta.amit@gmail.com wrote:

    > I keep getting an Access Violation error when my code calls
    > NTFSControlFile() . I have checked all other parts of my app for any
    > memory leaks etc. but nothing found so far. I have even moved the
    > function to a test MFC app, so that its the only fuinction being
    > called. I still get the same error. The code is pasted below. Any help
    > is most appreciated. The app is an MFC app in VS 20005:



    First, add some error checking to see what is going wrong and why. Also,
    you have forgot to zero the variable nextLcn before using it in the API
    call (this gave an "invalid parameter" error when I tried your code). I
    did not get any crash though, just errors returned.

    Just my quick modifications (I used DeviceIoControl() instead to make it
    a little bit easier to build):

    #define WIN32_LEAN_AND_MEAN
    #include
    #include
    #include

    int
    main()
    {
    PVOLUME_BITMAP_BUFFER bitMappings;
    DWORD ret;
    ULONGLONG startLcn;
    ULONGLONG nextLcn = 0; // Important, must be zeroed
    ULONGLONG lastLcn;
    ULONGLONG cluster;

    #define BITMAPBYTES 4096
    #define BITMAPSIZE (BITMAPBYTES+2*sizeof(ULONGLONG))

    HANDLE m_volHandle=0;
    BYTE m_BitMap[BITMAPSIZE];
    bitMappings = (PVOLUME_BITMAP_BUFFER) m_BitMap;
    bitMappings->StartingLcn.QuadPart = 0;
    bitMappings->BitmapSize.QuadPart = 0;
    bitMappings->Buffer[0] = 0;

    m_volHandle = CreateFile(TEXT("\\\\?\\E:"), GENERIC_READ,
    FILE_SHARE_READ|FILE_SHARE_WRITE,
    NULL, OPEN_EXISTING, 0, 0);

    if (m_volHandle == INVALID_HANDLE_VALUE)
    {
    printf("Error %u.\n", GetLastError());
    return 1;
    }

    if (!DeviceIoControl(m_volHandle, FSCTL_GET_VOLUME_BITMAP,
    &nextLcn, sizeof(cluster),
    bitMappings, BITMAPSIZE, &ret, NULL))
    if (GetLastError() != ERROR_MORE_DATA)
    {
    printf("Error %u.\n", GetLastError());
    return 2;
    }

    CloseHandle(m_volHandle);

    printf("Returned %u bytes.\n", ret);
    }


    --
    Olof Lagerkvist
    ICQ: 724451
    Web: http://here.is/olof

  3. Re: Access Violation when calling NTFSControlFile

    Thanks for the assistance Olof. I changed my code to resemble yours. I
    am using DeviceIoControl now instead of NtfsFileControl but I am
    getting the same Access Violation error on the Deviceiocontrol call
    now.
    I am pretty sure theres some data which is not initialized properly
    hence the same error. I just cant figure out which data. Could it be
    the way I am declaring the functions:

    BOOL (__stdcall *DeviceioControl) { }
    NTSTATUS (__stdcall *NtFsControlFile) {}

    Thanks for all your help.



    On Sep 7, 1:48 pm, Olof Lagerkvist wrote:
    > sengupta.a...@gmail.com wrote:
    > > I keep getting an Access Violation error when my code calls
    > > NTFSControlFile() . I have checked all other parts of my app for any
    > > memory leaks etc. but nothing found so far. I have even moved the
    > > function to a test MFC app, so that its the only fuinction being
    > > called. I still get the same error. The code is pasted below. Any help
    > > is most appreciated. The app is an MFC app in VS 20005:

    >
    > First, add some error checking to see what is going wrong and why. Also,
    > you have forgot to zero the variable nextLcn before using it in the API
    > call (this gave an "invalid parameter" error when I tried your code). I
    > did not get any crash though, just errors returned.
    >
    > Just my quick modifications (I used DeviceIoControl() instead to make it
    > a little bit easier to build):
    >
    > #define WIN32_LEAN_AND_MEAN
    > #include
    > #include
    > #include
    >
    > int
    > main()
    > {
    > PVOLUME_BITMAP_BUFFER bitMappings;
    > DWORD ret;
    > ULONGLONG startLcn;
    > ULONGLONG nextLcn = 0; // Important, must be zeroed
    > ULONGLONG lastLcn;
    > ULONGLONG cluster;
    >
    > #define BITMAPBYTES 4096
    > #define BITMAPSIZE (BITMAPBYTES+2*sizeof(ULONGLONG))
    >
    > HANDLE m_volHandle=0;
    > BYTE m_BitMap[BITMAPSIZE];
    > bitMappings = (PVOLUME_BITMAP_BUFFER) m_BitMap;
    > bitMappings->StartingLcn.QuadPart = 0;
    > bitMappings->BitmapSize.QuadPart = 0;
    > bitMappings->Buffer[0] = 0;
    >
    > m_volHandle = CreateFile(TEXT("\\\\?\\E:"), GENERIC_READ,
    > FILE_SHARE_READ|FILE_SHARE_WRITE,
    > NULL, OPEN_EXISTING, 0, 0);
    >
    > if (m_volHandle == INVALID_HANDLE_VALUE)
    > {
    > printf("Error %u.\n", GetLastError());
    > return 1;
    > }
    >
    > if (!DeviceIoControl(m_volHandle, FSCTL_GET_VOLUME_BITMAP,
    > &nextLcn, sizeof(cluster),
    > bitMappings, BITMAPSIZE, &ret, NULL))
    > if (GetLastError() != ERROR_MORE_DATA)
    > {
    > printf("Error %u.\n", GetLastError());
    > return 2;
    > }
    >
    > CloseHandle(m_volHandle);
    >
    > printf("Returned %u bytes.\n", ret);
    >
    > }
    >
    > --
    > Olof Lagerkvist
    > ICQ: 724451
    > Web:http://here.is/olof




  4. Re: Access Violation when calling NTFSControlFile

    sengupta.amit@gmail.com wrote:

    > Thanks for the assistance Olof. I changed my code to resemble yours. I
    > am using DeviceIoControl now instead of NtfsFileControl but I am
    > getting the same Access Violation error on the Deviceiocontrol call
    > now.
    > I am pretty sure theres some data which is not initialized properly
    > hence the same error. I just cant figure out which data. Could it be
    > the way I am declaring the functions:
    >
    > BOOL (__stdcall *DeviceioControl) { }
    > NTSTATUS (__stdcall *NtFsControlFile) {}



    Never declare the functions yourself, use the SDK headers. In this case:
    #include
    #include

    --
    Olof Lagerkvist
    ICQ: 724451
    Web: http://here.is/olof

  5. Re: Access Violation when calling NTFSControlFile

    I have included the two headers and removed my own definitions. But I
    am getting undeclared identifier for the functions when I comple now.
    Is something else required to be included or linked?


    On Sep 7, 3:32 pm, Olof Lagerkvist wrote:
    > sengupta.a...@gmail.com wrote:
    > > Thanks for the assistance Olof. I changed my code to resemble yours. I
    > > am using DeviceIoControl now instead of NtfsFileControl but I am
    > > getting the same Access Violation error on the Deviceiocontrol call
    > > now.
    > > I am pretty sure theres some data which is not initialized properly
    > > hence the same error. I just cant figure out which data. Could it be
    > > the way I am declaring the functions:

    >
    > > BOOL (__stdcall *DeviceioControl) { }
    > > NTSTATUS (__stdcall *NtFsControlFile) {}

    >
    > Never declare the functions yourself, use the SDK headers. In this case:
    > #include
    > #include
    >
    > --
    > Olof Lagerkvist
    > ICQ: 724451
    > Web:http://here.is/olof




  6. Re: Access Violation when calling NTFSControlFile

    Sorry spoke too soon. Its working
    Thanks. I guess declaring the functions myself was causing something
    to screw up.

    Thanks for all ur help.

    On Sep 7, 3:32 pm, Olof Lagerkvist wrote:
    > sengupta.a...@gmail.com wrote:
    > > Thanks for the assistance Olof. I changed my code to resemble yours. I
    > > am using DeviceIoControl now instead of NtfsFileControl but I am
    > > getting the same Access Violation error on the Deviceiocontrol call
    > > now.
    > > I am pretty sure theres some data which is not initialized properly
    > > hence the same error. I just cant figure out which data. Could it be
    > > the way I am declaring the functions:

    >
    > > BOOL (__stdcall *DeviceioControl) { }
    > > NTSTATUS (__stdcall *NtFsControlFile) {}

    >
    > Never declare the functions yourself, use the SDK headers. In this case:
    > #include
    > #include
    >
    > --
    > Olof Lagerkvist
    > ICQ: 724451
    > Web:http://here.is/olof




+ Reply to Thread