问题描述
BOOL SetDeviceID(HANDLE device,char *id){//
char data[2];
data[0]=0x02;
data[1]=0x27;
DWORD dwPtr=SetFilePointer(device,0x33,//distance
NULL,//
FILE_BEGIN);
if(dwPtr==INVALID_SET_FILE_POINTER) cout<<GetLastError()<<endl;
BOOL result=WriteFile(device,data,2,NULL,NULL);
//cout<<GetLastError()<<endl;
if(result==false)cout<<"Fail WRITE "<<endl;
return TRUE;
}
HANDLE GetDeviceHandle(char *path){
HANDLE handle= CreateFile(LPCSTR(path),
GENERIC_ALL,//
0,
NULL,
OPEN_EXISTING,
NULL,
NULL);
if(handle==INVALID_HANDLE_VALUE){
cout<<"fail to createfile()"<<endl;
exit(1);
}
else return handle;
}
这是我的一些作品代码.
this is some code of my works.
我要直接读/写设备(usb)
I am going to read/write directly device(usb)
在 ReadFile() 案例中,它成功了.
on ReadFile() case, It was successful.
但是,我尝试调用 SetFilePointer
But, I have tried to call SetFilePointer
但 GetLastError 返回 87. 表示无效输入
But GetLastError returned 87. it means invalid input
有什么问题?在我的代码上
What is the problem? on my code
很快,CreateFile,ReadFile 就可以了,但是 SetFilePointer 和 WriteFile 失败了
shortly, CreateFile,ReadFile is ok but SetFilePointer and WriteFile failed
推荐答案
当您直接访问磁盘设备时,您无法寻找扇区中间的位置.该位置必须始终是扇区长度的倍数.而且 0x33
不是您扇区长度的倍数.
When you are directly accessing a disk device you cannot seek to positions in the middle of a sector. The position must always be a multiple of the sector length. And 0x33
is not a mutiple of your sector length.
您需要做的是读取整个扇区.修改需要修改的字节.最后写回整个扇区.
What you will need to do is read an entire sector. Modify the bytes that need to be modified. And finally write back the entire sector.
这篇关于为什么它失败了 SetFilePointer()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!