Friday, November 01, 2013

WriteFile fail with error 87

最近為了測試檔案系統的效能, 寫了一個測試程式, 結果掛在一個很簡單的地方, 所以記錄一下

     這是寫檔程式, 

 hFile = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,FILE_FLAG_NO_BUFFERING, NULL);

 if (WriteFile(hFile, &OutData, iRand, &BytesOut, NULL) == 0)    
{
        printf (" Error writing file  error=%d\n\n",GetLastError ( ));
        CloseHandle(hFile);
        return 0;
}
           CloseHandle(hFile);


     很簡單, 確會傳出 INVAILD_PARAMETER 87 的error number , 簡查了老半天沒啥參數寫錯呀, 整個MSDN找了一遍也不知道啥原因, 反到是拜google大神找到, 因為要測效能, 所以我用FILE_FLAG_NO_BUFFERING 來測, 不過只要設成NO Buffer , writefile 就只能接受一次至少要寫1個sector 的限制, 而目前windows 7 都是4k也就是4096, 所以就一定要寫4096的倍數

所以看是要把FILE_FLAG_NO_BUFFERING改為 FILE_ATTRIBUTE_NORMAL來解,或是iRand這個變數一定要為4096的倍數都可以摟.!!


Update :
                 看來是我眼濁, MSDN有寫很清楚

                  http:// http://support.microsoft.com/kb/99794/en-us


The FILE_FLAG_NO_BUFFERING takes this concept one step further and eliminates all read-ahead file buffering and disk caching as well, so that all reads are guaranteed to come from the file and not from any system buffer or disk cache. When using FILE_FLAG_NO_BUFFERING, disk reads and writes must be done on sector boundaries, and buffer addresses must be aligned on disk sector boundaries in memory. 
 

No comments:

Post a Comment