-1

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:\"

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Makuvex Linux
  • 91
  • 1
  • 2
  • 6
  • You code doesn't work? What happens when you debug it? – Preet Sangha Jan 29 '14 at 03:16
  • `BYTE buff[1024]` is the equivalent to `Dim Buffer(1024) As Byte` why is the 1024*1024 and why you `malloc`? and which step is failed? in the VB you gave `Mid(strDrive, 1, 2)` which is 2 chars size, but in the c you gave `"PhysicalDrive0"` which is longer. maybe this is the reason? – SHR Jan 29 '14 at 03:27
  • I changed the code but still don't working. – Makuvex Linux Jan 29 '14 at 05:16

1 Answers1

0

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

Community
  • 1
  • 1
DNT
  • 2,356
  • 14
  • 16