question about getfileattributes function
-
i have been able to get my program to compile correctly and added in code to view the returned result of the dword value from GetFileAttributes() which should display the file attributes of the given file but when the function is called all it returns is an error message saying "the process cannot access the file because it is being used by another process." yet i made sure that before it was called i had closed the file before the function was called and even tried the function on a file that was never opened and it still gave the same error. here is the code for my function:
ReportAttributes(char filename[]) { LPCTSTR pszCaption = "Debug Results:"; DWORD attribList = GetFileAttributes(filename); if(!attribList) { MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, attribList, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox((LPCSTR)pTextBuffer, pszCaption, MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox("Unknown error", pszCaption, MB_OK); } } }
i varied the icons in the messagebox calls to see which one came up when the error was displayed. -
i have been able to get my program to compile correctly and added in code to view the returned result of the dword value from GetFileAttributes() which should display the file attributes of the given file but when the function is called all it returns is an error message saying "the process cannot access the file because it is being used by another process." yet i made sure that before it was called i had closed the file before the function was called and even tried the function on a file that was never opened and it still gave the same error. here is the code for my function:
ReportAttributes(char filename[]) { LPCTSTR pszCaption = "Debug Results:"; DWORD attribList = GetFileAttributes(filename); if(!attribList) { MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, attribList, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox((LPCSTR)pTextBuffer, pszCaption, MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox("Unknown error", pszCaption, MB_OK); } } }
i varied the icons in the messagebox calls to see which one came up when the error was displayed.swatgodjr wrote:
"the process cannot access the file because it is being used by another process."
Exactly this is the reason. Make sure that this file is not being used by another application or background process, or create a temporary file and call this function on this file and see what happens. If the problem persists then make sure that you have not opened this file before this call.
Nibu thomas A Developer Programming tips[^] My site[^]
-
swatgodjr wrote:
"the process cannot access the file because it is being used by another process."
Exactly this is the reason. Make sure that this file is not being used by another application or background process, or create a temporary file and call this function on this file and see what happens. If the problem persists then make sure that you have not opened this file before this call.
Nibu thomas A Developer Programming tips[^] My site[^]
-
yea as i explained i also tried this on a file i had not even touched at all and got the same error in doing so. is there maybe something wrong with my code i posted?
swatgodjr wrote:
yea as i explained i also tried this on a file i had not even touched at all and got the same error in doing so. is there maybe something wrong with my code i posted?
Well this I got from MSDN... If the function fails,
**the return value is**
**INVALID_FILE_ATTRIBUTES**
. To get extended error information, callGetLastError
. Do check for this value instead of !dwFileAttributes.
Nibu thomas A Developer Programming tips[^] My site[^]
-
swatgodjr wrote:
yea as i explained i also tried this on a file i had not even touched at all and got the same error in doing so. is there maybe something wrong with my code i posted?
Well this I got from MSDN... If the function fails,
**the return value is**
**INVALID_FILE_ATTRIBUTES**
. To get extended error information, callGetLastError
. Do check for this value instead of !dwFileAttributes.
Nibu thomas A Developer Programming tips[^] My site[^]
actually i originally had it checking for INVALID_FILE_ATTRIBUTES but no matter what i did it kept giving me an error saying it was an undeclared identifier so i just put !attribList so that it would compile so that i could at least test it out. i included the files it needed and everything and after quite a bit of looking around i just left it the way i have it now. the code i posted was actually modified from my getlasterror function which works nicely.
-
i have been able to get my program to compile correctly and added in code to view the returned result of the dword value from GetFileAttributes() which should display the file attributes of the given file but when the function is called all it returns is an error message saying "the process cannot access the file because it is being used by another process." yet i made sure that before it was called i had closed the file before the function was called and even tried the function on a file that was never opened and it still gave the same error. here is the code for my function:
ReportAttributes(char filename[]) { LPCTSTR pszCaption = "Debug Results:"; DWORD attribList = GetFileAttributes(filename); if(!attribList) { MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, attribList, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox((LPCSTR)pTextBuffer, pszCaption, MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox("Unknown error", pszCaption, MB_OK); } } }
i varied the icons in the messagebox calls to see which one came up when the error was displayed.I don't get your code. First, check the return value from GetFileAttributes(). If it's 0xFFFFFFFF, then call GetLastError() to get the error code. Then, do a FormatMessage with the return value from GetLastError()! (It's natural you always gets the same error message when you always calls FormatMessage with the same value, 0xFFFFFFFF).
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
-
i have been able to get my program to compile correctly and added in code to view the returned result of the dword value from GetFileAttributes() which should display the file attributes of the given file but when the function is called all it returns is an error message saying "the process cannot access the file because it is being used by another process." yet i made sure that before it was called i had closed the file before the function was called and even tried the function on a file that was never opened and it still gave the same error. here is the code for my function:
ReportAttributes(char filename[]) { LPCTSTR pszCaption = "Debug Results:"; DWORD attribList = GetFileAttributes(filename); if(!attribList) { MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, attribList, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox((LPCSTR)pTextBuffer, pszCaption, MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox("Unknown error", pszCaption, MB_OK); } } }
i varied the icons in the messagebox calls to see which one came up when the error was displayed.swatgodjr wrote:
if(!attribList)
You must read the docs a bit closer:
if (INVALID_FILE_ATTRIBUTES == attribList)
{
// use GetLastError() to see what the error is
MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING);
}
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
-
swatgodjr wrote:
if(!attribList)
You must read the docs a bit closer:
if (INVALID_FILE_ATTRIBUTES == attribList)
{
// use GetLastError() to see what the error is
MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING);
}
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
i tried to use INVALID_FILE_ATTRIBUTES and it returns an error on compiling saying that it was an undeclared identifier and also tried checking for the other error code but that just gives me the same error message i have been having when the function is called in my program. i have the correct files being included into my program so i really am not sure why it wotn let me use INVALID_FILE_ATTRIBUTES. also from what i have noticed, it never returns either error message as it goes to the main body of the function like it should.