I can't read mft and convert VB.net to C

355 Views Asked by At

I try to convert VB.NET source code to C. It's purpose is to show me if my hard disk is NTFS.

Supposedly reads MFT and can compare the third byte, if is 78 decimal (N) and 84 (T) and so on...returns 0 for "true", but I can't do it; my code it doesn't work.

How can I fix it?

VB.net:

Public Function IsNFTSDrive(ByVal strDrive As String) As Boolean
    Dim Hnd As Integer, nRead As Integer
    Dim ret As UInt32
    Dim Buffer(1024) As Byte
    Hnd = CreateFile("\\.\" & Mid(strDrive, 1, 2), GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, _
    Nothing, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, IntPtr.Zero)
    If (Hnd <> INVALID_HANDLE_VALUE) Then
        ret = ReadFile(Hnd, Buffer, 1024, nRead, New System.Threading.NativeOverlapped)
    Else
        Return False
    End If
    If ret = 0 Then
        ret = WaitForSingleObject(Hnd, INFINITE)
        Select Case ret
            Case WAIT_OBJECT_0
            Case WAIT_TIMEOUT
        End Select
    Else
        Return False
    End If
    CloseHandle(Hnd)
    Return Buffer(3) = 78 And Buffer(4) = 84 And Buffer(5) = 70 And Buffer(6) = 83
End Function

C:

#include <Windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <stdlib.h>


#define zwpath L"\\\\.\\PhysicalDrive0"

int main(int argc, char *argv[]){

    HANDLE hDevice;
    OVERLAPPED overlapped;
    BYTE buff[1024];
    DWORD numerobyte = 0;
    UINT32 ret;
    ZeroMemory(&overlapped, sizeof(OVERLAPPED));

    hDevice = CreateFileW(zwpath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);

    if(hDevice != INVALID_HANDLE_VALUE){

        ret = ReadFile(hDevice, buff, 1024, &numerobyte, &overlapped);

    }else
    {
        return NULL;
    }

    if(ret == 0){

        ret = WaitForSingleObject(hDevice,INFINITE );

        switch (ret)
        {
        case WAIT_OBJECT_0:break;
        case WAIT_TIMEOUT:break;
        default:
            break;
        }
    }
    else
    {
        return NULL;
    }

    CloseHandle(hDevice);

    if(buff[3] == 'N'){

        printf("N");
    }

    getchar();
}

EDIT

I change the code but nothing

I tried with "\.\C:" like MSDN example, but nothing :(

and the error begins in "if(buff[3] == 'N')", I don't know if ReadFile is failing or is the "if"?

The Mid is for the Drive.. "C:\" or "C:" like "\.\C:" or "\.\C:\"

1

There are 1 best solutions below

0
On

If you want to check for file system type check this relevant question that suggests using GetVolumeInformation().