0
votes

I am going to use this handle for WriteFile function but GetLastError gives me #5 . I know it is Access Denied but i don't know what to do. I check this for a while and i saw its about User Rights but i already in as Administrator .

And the other thing is : I'm coding at VS2010 and when i execute code GetLastError Gives me #5 but when i execute exe(C://blah/blah/PROJECT_NAME/debug/PROJECT.exe) it gives me #6 .

I just confused . Many thanks for your helps.

HANDLE hAndle = CreateFile("test.txt",
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL
    );
char msge[]="\nsomething\n";
DWORD gb;

if( WriteFile(hAndle,msge,strlen(msge),&gb,NULL) != TRUE )
    cout << GetLastError() << endl;

EDIT : what a stupid i am . I fixed that problem with this : GENERIC_WRITE | GENERIC_READ

Really thank you...

1
ERROR_ACCESS_DENIED means you don't have sufficient rights. After all, you are trying to write to a handle that you only asked permission to read from. ERROR_INVALID_HANDLE means that your handle is invalid. That's a consequence of your failure to check for errors when calling CreateFile. - David Heffernan

1 Answers

1
votes

error codd 6 is: The handle is invalid. when you try to execute CreateFile with OPEN_EXISTING on an invalid path, you get back INVALID_HANDLE_VALUE. If you pass this value to WriteFile, it will return with error code 6.

However, you are calling CreateFile with GENERIC_READ and with the handle you get, you are trying to write to the file. This doesn't work this way. You have to use GENERIC_WRITE and