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?Code:CreateFile(pszFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL);
Printable View
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?Code:CreateFile(pszFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL);
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?
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.
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.
CreateFile has 7 parameters - not 6
> 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*
Hey Salem, take it easy. It was mistake.
Here's code:
As you can see, there are no memory overruns.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;
}
> if(GetLastError() == 5)
Magic numbers, eh? You should know that magic numbers might change from OS to OS.
>> 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. ;)
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.
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:
then I'd have to look up what 5 means (which wastes time), but if I see:Code:if ( GetLastError() == 5 )
I know exactly what the code is doing and don't have to waste any more time.Code:if ( GetLastError() == ACCESS_DENIED )
Let me correct *you*. I've inherited a lot of code in my time, and I still get that will to strangle people who leave me with poorly written code. You should always assume that your code will be looked after by someone else.... and that that someone else can find out where you live... you've been warned ;)
QuantumPete
No worries there. I only use magic numbers for temporary or debug code which I would delete in 10 minutes anyway. :)
Actually, I've run into error 5 so much, I know what it means, even from month to month or year to year. Kind of like everyone knows what 0xC0000005 means.Quote:
And even if it's just you that will be maintaining it; unless you have an extremely good memory, you'll probably forget what the magic number meant a few weeks/months later.
But that's besides the point, why would this happen? Does anyone know of any previous API calls that could do this?
I would suggest that you check the security properties of the Documents and Settings folder on your Win2k machine to verify that you have the necessary permissions to modify (write to) TESTFILE.txtCode:C:\\Documents and Settings\\Yarin\\Desktop\\TESTFILE.txt");
Yes, full rights, all are set to allow.
Both code paths have beeps. Are you sure you're hearing the one you think you're hearing?
Well, considering that you are invoking so many API calls (which can clear and set an error themselves) after CreateFile before calling GetLastError, the code you're getting may not relate to CreateFile at all...
CreateFile() would set the last error to 0 (ERROR_SUCCESS) then.
the docs say that some functions set ERROR_SUCCESS, and some don't. CreateFile doesn't say whether it does or doesn't, so it might not.
If the function succeeds with CREATE_ALWAYS or OPEN_ALWAYS and the file already existed, GetLastError() returns ERROR_ALREADY_EXISTS, though the function has succeeded. Otherwise, on success, it returns 0. On failure it always returns the error code.
ok, right you are, except if it's not a valid path.