2014-01-29 56 views
-1

我嘗試將VB.NET源代碼轉換爲C.它的目的是向我顯示我的硬盤是否爲NTFS。我無法讀取mft並將VB.net轉換爲C

假設讀取MFT並可以比較第三個字節,如果是78十進制(N)和84(T)等等...返回0爲「真」,但我不能這樣做;我的代碼不起作用。

我該如何解決?

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(); 
} 

編輯

我更改代碼,但沒有

我試過 「\。\ C:」 像MSDN example,但沒有:(

和錯誤開始於「if(buff [3] =='N')」,我不知道ReadFile失敗或是「if」?

中秋節是驅動器。 「C:\」 或 「C:」 像 「\。\ C:」 或 「\。\ C:\」

+0

你的代碼不工作?當你調試它會發生什麼? –

+0

'BYTE buff [1024]'等效於'Dim Buffer(1024)As Byte'爲什麼是1024 * 1024,爲什麼要'malloc'?哪一步失敗?在VB中,你給了Mid(strDrive,1,2)'這是2個字符的大小,但在c中給了''PhysicalDrive0''這個更長。也許這是原因? – SHR

+0

我改變了代碼,但仍然無法正常工作。 –

回答