惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - Sephil

[小技巧] 倍数的向上取整和向下取整 WTL汉化版 Delphi 与 VC 共享接口和对象 DelphiXE泛型不能用类类型做为约束的另类解决方案 百度音乐下载工具 (最后更新: 2012-7-20) 为Delphi应用增加脚本支持 插件框架Extensible Framework for Delphi DirectUI for Delphi VC CListCtrl 第一列列宽自适应 批量更改文件名的批处理文件 替代Windows运行功能的工具FastRun 关于 API 中返回字串的一些问题 BCB/Delphi2007 隐藏任务栏图标 OGA & WGA Crack 所有小工具 迅雷/快车/旋风地址转换器 oracle ora-01033和ora-00600错误 数独游戏 SudokuPuzzle 将文件夹映射为驱动器的工具
WriteFile写磁盘扇区是87错误的原因
Sephil · 2011-03-18 · via 博客园 - Sephil

今天在写个工具,功能是将一些数据写到磁盘的绝对扇区上.

但是发现ReadFile没问题,可是WriteFile总是报87错误(参数不正确).

Google上查了一会,发现碰到问题的人不少,可惜没有正确答案....

没辙,还是啃MSDN,终于还是自己解决了~呵呵

赶紧记下来,免得忘了...

其实主要是写入的数据大小,必须是BytesOfSector大小的倍数,一般是512字节,ReadFile却无所谓...

另外,MSDN上还说,WriteFile前最好先用FSCTL_LOCK_VOLUME或者FSCTL_DISMOUNT_VOLUME将磁盘锁定或卸载,不过我试下来好像没什么影响...

代码摘录如下:

 1 UINT CDevInfo::Write(LPVOID pData, UINT nSize)
 2 {
 3     if (!pData && nSize <= 0)
 4         return 0;
 5 
 6     TCHAR szDevName[30];
 7     _stprintf(szDevName, _T("\\\\.\\PHYSICALDRIVE%d"), m_nDevNum); // DeviceNumber
 8 
 9     HANDLE hDev = ::CreateFile(szDevName, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, NULL);
10     if (hDev == INVALID_HANDLE_VALUE)
11         return 0;
12 
13     DWORD dwCount;
14     if (!::DeviceIoControl(hDev, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0&dwCount, NULL))
15         return 0;
16 
17     DWORD dwWritten(0);
18     ::SetFilePointer(hDev, SECTOR_NUM * GetSectorSize(), NULL, FILE_BEGIN);
19     if (!::WriteFile(hDev, pData, nSize, &dwWritten, NULL))
20         dwWritten = 0;
21     
22     ::DeviceIoControl(hDev, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0&dwCount, NULL);
23     ::CloseHandle(hDev);
24     return dwWritten;
25 }