If i open a drive (C: or D:) in edit mode using CreateFile then WriteFile is failed. While there is no issue for Win XP.
Is there something else which should be taken care when write data for drive in windows7
Printable View
If i open a drive (C: or D:) in edit mode using CreateFile then WriteFile is failed. While there is no issue for Win XP.
Is there something else which should be taken care when write data for drive in windows7
Define "failed"
CreateFile returns INVALID_HANDLE_VALUE if it fails. According to the MSDN, call GetLastError to find out why.
Viggy
Posting the applicable code will go a long way to getting help with your problem...
I'll write the error code and source code.
But i think it's known problem for window7
I am login in windows with a user which has admin right. UAC is turn on.
I run my exe using Administrator but fail to write data on disk.
CreateFile open discs successfully but WriteFile fail.
Since this is the first post of this problem I have seen in this forum with Win7 a copy of the code or sample project that duplicates the problems would be helpful.
I have a commercial program that writes to Win7 and Server 2008 over 750,000 times a day with no errors...this is why I ask for the offending code to help you solve your problem.
I got the error "Access is Denied"
Code is
__int64 pos = 1000000;
TCHAR drivename[255];
memset(drivename,0,sizeof(drivename));
sprintf(drivename,_T("\\\\.\\PhysicalDrive%d"),0);
HANDLE hDrive2 = CreateFile(drivename,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
if(hDrive2 == INVALID_HANDLE_VALUE)
{
MessageBox(_T("fail to open disk"));
return;
}
LARGE_INTEGER n64Pos;
n64Pos.QuadPart = pos;
DWORD nRet = SetFilePointer(hDrive2 , n64Pos.LowPart,&n64Pos.HighPart, FILE_BEGIN);
car buf[512];
memset(buf,0,512);
BOOL bl = WriteFile(drivehandle,buf,512,&dwBytes,0);
if(bl == FALSE)
{
LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
// Handle the error.
return;
}
MessageBox( (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
- After 158 posts in this Forum (since Jul 2006!) don't you still know how to use Code tags? :( :thumbd:
- You do NOT "open a drive (C: or D: ...)", instead you open some "\\\\.\\PhysicalDrive%d"
Code:
__int64 pos = 1000000;
TCHAR drivename[255];
memset(drivename,0,sizeof(drivename));
sprintf(drivename,_T("\\\\.\\PhysicalDrive%d"),0);
HANDLE hDrive2 = CreateFile(drivename,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);
if(hDrive2 == INVALID_HANDLE_VALUE)
{
MessageBox(_T("fail to open disk"));
return;
}
LARGE_INTEGER n64Pos;
n64Pos.QuadPart = pos;
DWORD nRet = SetFilePointer(hDrive2 , n64Pos.LowPart,&n64Pos.HighPart, FILE_BEGIN);
car buf[512];
memset(buf,0,512);
BOOL bl = WriteFile(drivehandle,buf,512,&dwBytes,0);
if(bl == FALSE)
{
LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
// Handle the error.
return;
}
MessageBox( (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
// Free the buffer.
LocalFree( lpMsgBuf );
}
Questions:
1. What is the return value of DWORD nRet
2. What is car buf[512];...is this is suppose to be a char buf[512] ?? if so, how did this compile
3. What is the sector size of Disk 0
Sorry for typing mistake
It's char buf[512]
Return value is 5 (nRet)
sector size is 512
I forgot to mention that i open pysicaldrive0 (not Cor d drive) sorry for that
But i think if i can write on disk whether i open c drive or PysicalDrive0
Well if you are trying to write to the PhysicalDrive0, not to the PysicalDrive0 then have a look at the:
http://stackoverflow.com/questions/6...or-access-deni
and
http://www.codeproject.com/Answers/9...t=7750#answer2
You might also want to check your SetFilePointer() value...
__int64 pos = 1000000;
When writing to PhysicalDrive0 you must be aligned on sector boundries...and your "pos" value of 1000000 won't get you to a sector boundry...should be a multiple of the sector size.
But this problem is more than likely a permission thing or some intermediate step to allow such a function is missing...
I just gave an example using 10000.
To read/write from disk the offset sould be multiple of sector size
As suggested
After locking the drive you have to dismount it and get rid of the DOS device
I will use FSCTL_DISMOUNT_VOLUME and DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE
and update in this thread
As suggested i unlock the disk, dismound it and call remove dos device function. But still the same problem exist "Access is denied" when WriteFile is called.
I am attaching the code. Please have a liink and suggest where i am wrong.
Hi Victor,
I have attached the source code to write data on disk. The code failed towrite data.
Please suggest How to correct it.