How to use CreateFile for windows7
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: How to use CreateFile for windows7

  1. #1
    Join Date
    Jul 2006
    Posts
    203

    How to use CreateFile for windows7

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,359

    Re: How to use CreateFile for windows7

    Define "failed"
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: How to use CreateFile for windows7

    CreateFile returns INVALID_HANDLE_VALUE if it fails. According to the MSDN, call GetLastError to find out why.

    Viggy

  4. #4
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,538

    Re: How to use CreateFile for windows7

    Posting the applicable code will go a long way to getting help with your problem...
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  5. #5
    Join Date
    Jul 2006
    Posts
    203

    Re: How to use CreateFile for windows7

    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.

  6. #6
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,538

    Re: How to use CreateFile for windows7

    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.
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  7. #7
    Join Date
    Jul 2006
    Posts
    203

    Re: How to use CreateFile for windows7

    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 );

    }

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,359

    Re: How to use CreateFile for windows7

    1. After 158 posts in this Forum (since Jul 2006!) don't you still know how to use Code tags?
    2. You do NOT "open a drive (C: or D: ...)", instead you open some "\\\\.\\PhysicalDrive%d"
    Victor Nijegorodov

  9. #9
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,538

    Re: How to use CreateFile for windows7

    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
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  10. #10
    Join Date
    Jul 2006
    Posts
    203

    Re: How to use CreateFile for windows7

    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

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,359

    Re: How to use CreateFile for windows7

    Quote Originally Posted by MFCQuery View Post
    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
    Last edited by VictorN; September 17th, 2011 at 05:38 AM.
    Victor Nijegorodov

  12. #12
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,538

    Re: How to use CreateFile for windows7

    Quote Originally Posted by MFCQuery View Post
    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

    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...
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  13. #13
    Join Date
    Jul 2006
    Posts
    203

    Re: How to use CreateFile for windows7

    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

  14. #14
    Join Date
    Jul 2006
    Posts
    203

    Re: How to use CreateFile for windows7

    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.
    Attached Files Attached Files
    Last edited by MFCQuery; September 20th, 2011 at 05:27 AM.

  15. #15
    Join Date
    Jul 2006
    Posts
    203

    Re: How to use CreateFile for windows7

    Hi Victor,

    I have attached the source code to write data on disk. The code failed towrite data.
    Please suggest How to correct it.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center