09-17-2016 01:49 AM
Hi,
I am trying to delete a .txt file using DeleteFile() (Windows SDK one) function.
I am making sure that the file is closed by callinf fflush() and fclose() before deleting it.
However DeleteFile() is returning -6 (Access Denied) and it is not deleted.
Please suggest what are the other possibilities.
Thanks in advance
09-18-2016 03:53 PM
From what you are saying it seems you are not actually calling the SDK function but the CVI one instead: only this one can return -6 if failing, the SDK function returns 0 in this case.
To be sure to use the SDK function you must include windows.h before utility.h, as stated in the CVI function help. This other help page can also be useful.
Which OS are you running on and where is located your file? Some paths are reserved under some versions of Windows.
Finally, how are you building the file path to pass to the function? Can you post your code? Since the backslash is a special character in C, pathnames must be created appropriately to be recognised by the IDE: MakePathname () function can be useful in this respect.
09-19-2016 12:09 AM
Hi Roberto
The order of header file inclusion is as follows.
#include <Windows.h>
#include <utility.h>
The OS used to run the executable is Windows 7 Professional.
We are not making use of MakePathname() to build the path as the file to be deleted is within the project directory(same folder where executable is located).
The line from the code: DeleteFile("temp.txt"); where temp.txt is the file to be deleted.
09-19-2016 01:52 AM
I am quoting here part of the help page on conflicts:
Some Windows SDK functions have the same name as some LabWindows/CVI functions, causing a naming conflict. You must include the Windows SDK include files before the LabWindows/CVI include files. LabWindows/CVI automatically resolves the conflict by using the LabWindows/CVI implementation of the function. To use the Windows SDK implementation of the function instead, define the SDK_CONFLICT_PRIORITY macro in theCompiler Defines section of the Build Options dialog box.
Highlight in red is mine, the sentence immediately following it shows how to resolve confilts to access the SDK function. However, this is a global setting, valid for all functions in the list.
09-19-2016 02:01 AM
Even if the file to delete is in the application folder, passing a simple filename to DeleteFile (CVI function) will operate on the active folder at the moment of the call; now, the active folder is not necessarily the application one: using e.g. FileSelectPopup or similar functions can change the active folder so that DeleteFile may fail not finding the file or accessing protected paths.
In case of the SDK function, it should search for the file in the OS search order, which should include the application folder. Even though, I would use the full pathname to be sure of the file I am accessing. GetProjectDir + MakePathname are a simple solution to this.
09-19-2016 02:11 AM
Thanks for the suggestion.
I tried filling '/DSDK_CONFLICT_PRIORITY' in Compiler defines field of Build options within my project.
Still the file is not deleted.
Is there something to include in the program? #if or #ifdef preprocessor directive?
09-19-2016 02:22 AM
First of all, to understand which function you are actually using execute Options >> Preprocess Source File menu function while the source file is open in the CVI IDE: this will create a new file with all preprocessing of include files done. Search in this *huge* file your deleting line: you are using the CVI function if you see CVI_DeleteFile("temp.txt"); if you are using the SDK function you should see DeleteFileA ("temp.txt");
Having clarified this, use the appropriate error checking strategy to understand the problems in the function: return code in case of the CVI function of GetLastError () in case of the SDK one.
09-19-2016 02:24 AM
Additionally, an attempt to delete file immediately following fclose may fail if the system has not yet committed the close: in this case adding a pause may help.
09-19-2016 05:36 AM
Thank you very much for the detailed explanation. I did the 'Processor Source File' option and found following lines.
int __stdcall CVI_DeleteFile (const char fileName[]);
CVI_DeleteFile("temp.txt");
So this confirms that the CVI function DeleteFile() is called but not the SDK one.
I have put Windows.h before utility.h still it is taking the CVI one rather the Windows function.The next step is to include the error checking and try Which I am going to do now.
09-19-2016 06:45 AM
Hi,
Now I am able to generate preprocess source file with DeleteFileA() i.e. SDK function.
I have inserted delay of 1 second between closing of file and deleting of file.
Now also file is not deleting.
I inserted the function GetLastError() which returned 32. What does that mean?