Code:
WriteFile(hDevice,buffer,512,bytesN,NULL)
should be:

Code:
WriteFile(hDevice,buffer,512,&bytesN,NULL)
I'm not sure how it compiled.