CreateFile returns err 5 on 2000, but works fine of XP

This is a discussion on CreateFile returns err 5 on 2000, but works fine of XP within the Windows Programming forums, part of the Platform Specific Boards category; Code: CreateFile(pszFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL); works fine on XP, but fails with Access Denied error on 2000. And ...

  1. #1
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597

    Exclamation CreateFile returns err 5 on 2000, but works fine of XP

    Code:
    CreateFile(pszFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL);
    works fine on XP, but fails with Access Denied error on 2000. And this is not with a protected file here, but with any file I try! What should I do?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    And so what are you expecting us to magic out of one line of code?

    How did you declare ....
    How did you initialise ....
    Does it compile without any errors?
    Have you been mucking about with ASCII/UNICODE?
    What is the current directory?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    And so what are you expecting us to magic out of one line of code?
    >> No, the code is so simple, thats the only line I could think that would be relevant.

    How did you declare ....
    >> Declare what exactly?

    How did you initialise ....
    >> Initialize what exactly?

    Does it compile without any errors?
    >> Of course.

    Have you been mucking about with ASCII/UNICODE?
    >> You mean combining them? No, the program is pure Unicode.

    What is the current directory?
    >> What ever the dir of the program I think, but it shouldn't make a difference anyway, pszFile is always an absolute path.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    What might be relevant would be a small complete program which demonstrates the problem. As you seem to be saying it does it for all files, it should be about a 10 line program.

    > thats the only line I could think that would be relevant.
    http://www.catb.org/~esr/faqs/smart-....html#symptoms

    > Initialize what exactly?
    I dunno, let's say that pszFile is an array of 20 wide chars, and ypu've tried to put 50 in there.
    Or it's just a pointer you didn't even bother to initialise before writing 50 chars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    CreateFile has 7 parameters - not 6
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > CreateFile has 7 parameters - not 6
    ROFLMA - so it does!

    > Does it compile without any errors?
    > >> Of course.
    Makes me wonder how this can be true then.

    Seems like a typical garbage question. Just scribble down any old rubbish from memory rather than posting quality information like the actual code tried.

    In too big a hurry to post something, anything, just get it posted and be damned the quality. Go waste someone elses time Yarin.

    *PLONK*
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    Hey Salem, take it easy. It was mistake.
    Here's code:

    Code:
    BOOL doMethod(wchar_t* pszFile)
    {
       HANDLE h = CreateFile(pszFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); // All 7 params! :)
       if(h != INVALID_HANDLE_VALUE)
       {
          DWORD size = GetFileSize(h, NULL);
          DWORD a, rite = 0;
          BYTE buff[ME_BUFFSIZE];
          ZeroMemory(buff, ME_BUFFSIZE);
          for(a = 0; a < size; a += ME_BUFFSIZE)
             WriteFile(h, buff, ME_BUFFSIZE, &rite, NULL);
          a = size - (a - ME_BUFFSIZE);
          if(a > 0)
             WriteFile(h, buff, a, &rite, NULL);
          CloseHandle(h);
          return TRUE;
       }
       return FALSE;
    }
    
    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char* pcCommand, int nShow)
    {
       wchar_t buff[MAX_PATH];
       lstrcpy(buff, L"\\\\?\\C:\\Documents and Settings\\Yarin\\Desktop\\TESTFILE.txt");
       if(!doMethod(buff))
       {
          if(GetLastError() == 5)
             Beep(2500, 100);
       }
       else
          Beep(1000, 50);
       return 0;
    }
    As you can see, there are no memory overruns.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > if(GetLastError() == 5)

    Magic numbers, eh? You should know that magic numbers might change from OS to OS.

  9. #9
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,597
    >> Magic numbers, eh? You should know that magic numbers might change from OS to OS.
    I know, but I'm pretty sure Access Denied is 5 on 2000, XP, and Vista.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Yarin View Post
    >> Magic numbers, eh? You should know that magic numbers might change from OS to OS.
    I know, but I'm pretty sure Access Denied is 5 on 2000, XP, and Vista.
    Even if it doesn't change from one OS version to the next, it still means nothing to 99% of people who read it.

  11. #11
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by cpjust View Post
    Even if it doesn't change from one OS version to the next, it still means nothing to 99&#37; of people who read it.
    Not if the number is in the title. Doing extra work to look up what is the textual represention of the error takes time, time is money.

    By the way, error codes don't change much between Windows versions, otherwise applications compiled under Win98 wouldn't run on XP. Somehow they do. That's because error codes are never replaced, but only added to the end or into empty gaps. And I have a feeling that ACCESS_DENIED error exists since the very first release of Windows.
    Last edited by maxorator; 08-07-2008 at 04:07 AM.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by maxorator View Post
    Not if the number is in the title. Doing extra work to look up what is the textual represention of the error takes time, time is money.
    I'm not sure what you mean about the number being in the title?
    Macro & variable names can't be all numbers, and having a name like ACCESS_DENIED_5 is pointless.

    If I see:
    Code:
    if ( GetLastError() == 5 )
    then I'd have to look up what 5 means (which wastes time), but if I see:
    Code:
    if ( GetLastError() == ACCESS_DENIED )
    I know exactly what the code is doing and don't have to waste any more time.

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by cpjust View Post
    I'm not sure what you mean about the number being in the title?
    Macro & variable names can't be all numbers, and having a name like ACCESS_DENIED_5 is pointless.
    In the title as in the name of this thread. I'm sure everyone who has read the thread title and first post, knows what "5" stands for.

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by maxorator View Post
    In the title as in the name of this thread. I'm sure everyone who has read the thread title and first post, knows what "5" stands for.
    Yes, but I meant in general, programs shouldn't use magic numbers; they should use descriptive variable/constant names.

  15. #15
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by cpjust View Post
    Yes, but I meant in general, programs shouldn't use magic numbers; they should use descriptive variable/constant names.
    Let me correct you. Open source or shared code shouldn't use magic numbers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. It works just fine but
    By jcmhex in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2005, 06:53 PM
  2. Fstream problem 2000 and xp
    By ~Kyo~ in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2004, 09:05 PM
  3. Wont run in X, works fine in alt+F1 terminal
    By zmerlinz in forum Linux Programming
    Replies: 5
    Last Post: 04-28-2004, 11:58 AM
  4. My computer works just fine
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 07-02-2002, 08:51 AM
  5. Replies: 6
    Last Post: 01-07-2002, 01:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21