Question

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.

I am going to read/write directly device(usb)

on ReadFile() case, It was successful.

But, I have tried to call SetFilePointer

But GetLastError returned 87. it means invalid input

What is the problem? on my code

shortly, CreateFile,ReadFile is ok but SetFilePointer and WriteFile failed

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top