I am using a method from a library file (compiled) to convert a file to another format. I have created an instance of this library object in a class and convert the file. The method in the library class converts the file and copies it to another location. Everything works great, but when I try to delete the converted file I get an error message telling me that the file is being used by another process. The object that I created is destroyed so I would think that it is no longer locking the file. I am not sure how to release this lock on the file (other than exiting the program). Also I do not have access to the library file source code and no documentation on it.
Any help is appreciated,
Thanks,
Peter
Loading
Sam HobbsPosted Mar 11, 2010, 11:21 PM
Except here is another thing to think about. It could be that the file does not get released until the ActiveX object processes another file. I think that would be relevant only if you are processing more than one file during a single execution. I hope that makes sense to you. If that is happening then you could keep previous files someplace in your program and try later to delete the file. There might be a prblem with the last file of an execution, but perhaps this explains what is happening at least.
Andrzej GrzegorczykPosted Sep 23, 2014, 12:28 PM
I know it was very old post but did you manage to solve the problem described there (see copy below)? I am asking becouse I would like to use the same library with Pascal lazarus but there is no good documentation. So I am trying to get as much information as possible.
Rgs
Andrzej
Library Object will not release file
Posted on: 05 Mar 2010
I am using a method from a library file (compiled) to convert a file to another format. I have created an instance of this library object in a class and convert the file. The method in the library class converts the file and copies it to another location. Everything works great, but when I try to delete the converted file I get an error message telling me that the file is being used by another process. The object that I created is destroyed so I would think that it is no longer locking the file. I am not sure how to release this lock on the file (other than exiting the program). Also I do not have access to the library file source code and no documentation on it.
Any help is appreciated,
Thanks,
Peter
theLizardPosted Mar 19, 2010, 12:14 AM
Sam HobbsPosted Mar 18, 2010, 11:28 PM
theLizardPosted Mar 17, 2010, 7:42 PM
You have not got a clue about what you are talking about.
There is either a lock on the file or there is not, these are the only possibilities.
Either the file lock was released or it was not, these are the only two possibilities.
Either there was an error or there was not, these are the only two possibilities
with these tree statements there are only two possibilities, the file can be deleted or it cannot
this final statement means the handle was released or it was not and this means there are only two possibilities
If he cannot delete the file there is only ONE possibility
THE HANDLE WAS NOT RELEASED because of one of the two possibilities.
What does it matter! is this how you solve problems you have no idea about?
Sorry Peter, people should not answer questions they obviously know nothing about.
Sam HobbsPosted Mar 17, 2010, 5:15 PM
Note that the behavior resulting from killing the process is not likely to be relevant, since as I said, it makes sense to me that a process and Windows ae unable to release a handle when a process is killed.
It might be true that the files you are unable to delete have encountered an error. Another possibility is that the files you are able to delete have encountered an error. It could be that the error processing for the file closes the file properly (or it never opens and therefore there is never a lock) but normal processing does not. It could be that no error is encountered but there are two paths through the program, one that releases the file and another that does not. The question is what can you do; I think there is nothing you can do so it is not worth speculating.
theLizardPosted Mar 17, 2010, 5:10 PM
You also said that some of the files are being deleted but not all!
What format are the files you are converting, binary, txt's image?
If the conversion process is not entirely correct and upsets the apple cart so to speak you will get unpredictable errors and if the type of error does not fall within the catch error range the error will not be caught.
since the problem exists in a process you have no control over and you do not have the source to the active x then you may never be able to solve the problem.
The key to solving this problem is knowing the format of the files you want to convert FROM and TO then maybe someone could help writing a new conversion routine...
PeterPosted Mar 17, 2010, 2:38 PM
The file will remained locked whether I abort the process or if the process completes on its own. I am beginning to wonder as mentioned in a previous thread (thelizard) that maybe the CloseFile() method has an untrapped error and not throwing an exception.
Thanks,
Peter
Sam HobbsPosted Mar 13, 2010, 1:59 AM
Go to Windows Sysinternals. There are many utilities there. I don't know which one to suggest, but hopefully you will find something useful and relevant. Also look at Debugging Tools for Windows. I know that that page does not explain how to download it but I assume you can find that.
theLizardPosted Mar 12, 2010, 5:31 PM
1 the file is not being properly closed in the copy file function
2 an un-trapped error has occurred in the copy function
If the handle to the file is not properly released you will have this type of problem, when you exit the application all resources are released allowing you to delete the file.
I would go with an un-trapped error as being the cause.
In the loop where you are able to delete some but not all, this could be a case where the files you are able to delete have had the handles released properly and those that you could not delete have thrown an error.
PeterPosted Mar 12, 2010, 10:33 AM
Lots of good input. Very strange to me. I actually am running this on a separate thread as well and even if I kill the thread the file still remains locked. You would think that if you create the ActiveX object on a separate thread, open the file with that object then abort the thread that the object would no longer be running and therefore unlock the file. Oh well, I am sure I figure this thing out. Thanks for the help anyways you guys gave me some things to think on.
Peter
PeterPosted Mar 11, 2010, 10:19 AM
I tried setting this to null as you suggested and got some interesting results. I have set up up the code to collect all the file names to delete as it is processing each file. After the files have been processed the program will go through each file and try to delete it if it is not locked. When I set the object to null as you suggested some of the files are getting deleted but not all of them and the files that are getting delete are processed the exact same way so I am not sure what is going on here.
Thanks,
Peter
bool IsFileOpen(string fileName)
{
// Assumption: File exists.
bool rtnvalue = false;
try
{
System.IO.FileStream fs = System.IO.File.OpenWrite(fileName);
fs.Close();
}
catch (System.IO.IOException e)
{
rtnvalue = true;
}
return (rtnvalue);
}
private void DeleteOldFiles()
{
// I collect the files to be deleted in a list
//go through and delete the closed files
for (int i = 0; i < DeleteFileList.Count; i++)
{
if (!IsFileOpen(DeleteFileList[i]))
{
File.Delete(DeleteFileList[i]);
DeleteFileList.Remove(DeleteFileList[i]);
}
}
}
PeterPosted Mar 9, 2010, 5:20 PM
I will try setting it to null.
So far I am very impressed with the responses from this forum. Great knowledge base.
Thanks for the help,
Peter
Sam HobbsPosted Mar 9, 2010, 5:10 PM
Sam HobbsPosted Mar 9, 2010, 5:04 PM
PeterPosted Mar 9, 2010, 11:24 AM
This is not the code that I am using in my program this is the function that the supplier of the library file was using to open and convert the binary files. My code is something like this:
public void ConvertFile(String FilePath, String NewFileName)
{
//creates the new object from the ActiveX library
CAixDataLogLocalAccessClass TempAixAccess = new CAixDataLogLocalAccessClass();
//this loads in the original file
TempAixAccess.LoadFile(m_FileName, "NSTYPE=TechLog");
String TempFileName;
String Extension;
String FileNameWoExt;
//file naming stuff
TempFileName = Path.GetFileName(m_FileName);
Extension = Path.GetExtension(m_FileName);
FileNameWoExt = TempFileName.Replace(Extension, ".dlg");
TempFileName = FilePath + FileNameWoExt;
if (NewFileName == "")
NewFileName = FileNameWoExt;
// --------------
try
{
//makes the file in the new location
TempAixAccess.MakeFile(FilePath + NewFileName, "");
//converts the data and puts it into the new file
TempAixAccess.ConvertOldFile(m_FileName);
}
catch (Exception ex)
{
throw new Exception("Error converting file: " + ex.Message);
}
finally
{
//I think this closes the original file only?
TempAixAccess.CloseFile();
}
}
Thanks for the help,
Peter
Mahesh ChandPosted Mar 9, 2010, 10:04 AM
{
var dlgfile = null;
try {
var dlLocal = new ActiveXObject("AixDataLogLocal.Access");
if (null != dlLocal)
{
var idx = logfile.lastIndexOf(".log");
var dlgfile = logfile.substring(0, idx) + ".dlg";
alert("ConvertOldFile: " + logfile.replace(/^[a-zA-Z]:\/([^\/]*\/)*/,"") + " -> " + dlgfile.replace(/^[a-zA-Z]:\/([^\/]*\/)*/,"") + "\nPlease wait");
dlLocal.MakeFile(dlgfile, "");
dlLocal.ConvertOldFile(logfile);
dlLocal.CloseFile(); // waits until all data is written
}
}
catch (e) {
alert("ConvertOldFile exception: " + e.description);
}
return dlgfile;
}
What if there is an exception in MakeFile or ConvertOldFile method? In that case, CloseFile() method will execute.
You need to implement try..catch..finally and in finally call CloseFile. For this, you will have to define dlLocal before try call. Finally will execute no matter what.
try {}
catch{}
finall{ dlLocal.CloseFile(); // waits until all data is written }
PeterPosted Mar 9, 2010, 9:45 AM
Thanks for your response. When I right click on the library file and select the properties, the library type is ActiveX. The company that made these libraries created their own browser based program (I think JScript) to read data from these binary files and plot data. I was able to read the script and trace down these library files used. There are three methods used to convert the files. Here is the script that I found from their app:
function ConvertOldFile(logfile)
{
var dlgfile = null;
try {
var dlLocal = new ActiveXObject("AixDataLogLocal.Access");
if (null != dlLocal) {
var idx = logfile.lastIndexOf(".log");
var dlgfile = logfile.substring(0, idx) + ".dlg";
alert("ConvertOldFile: " + logfile.replace(/^[a-zA-Z]:\/([^\/]*\/)*/,"") + " -> " + dlgfile.replace(/^[a-zA-Z]:\/([^\/]*\/)*/,"") + "\nPlease wait");
dlLocal.MakeFile(dlgfile, "");
dlLocal.ConvertOldFile(logfile);
dlLocal.CloseFile(); // waits until all data is written
}
}
catch (e) {
alert("ConvertOldFile exception: " + e.description);
}
return dlgfile;
}
The MakeFile method allows you to copy the file anywhere you like. I assumed the CloseFile will release the file, but I thinking maybe this just the file that is being worked on. Anyway the method seems to lock the file that was converted and copied and I would like to delete it after I use it. The program that I am trying to write will be on all the time collecting new files every couple of hours so I would like to clean up the folder otherwise it will fill up fast.
Thanks again for any help,
Peter
Sam HobbsPosted Mar 9, 2010, 2:53 AM
You do say that the "library files are written in .NET because I can view them in my object browsers" but that also might not be true. Do you have a reference to the library in your project's references? If so, then right-click on the reference and select Properties. What type of File Type is it?
You say that the "library class converts the file and copies it to another location". Which one are you trying to delete? The "from" file or the "to" file? I probably understand what you are saying there but I want to be sure. Are you able to specify where the files are created? If you can specify a directory for them, then it would be easy to delete the files later.
PeterPosted Mar 8, 2010, 9:32 AM
Thanks for the reply. I work with a company that processes material with special equipment. The tools output data from the machines in a compressed binary file format. My task was to try to access this data and put it into the database. The tools have some software to open the files and look at the data and convert it to ASCII. The library files are written in .NET because I can view them in my object browsers. I can use one of the library classes to extract data and it works great. They have a method that should close the file for me, but when I call this method the file is still locked by my app. I thought that if you create an object in a method and that method goes out of scope that the object is automatically destroyed. I was wondering if I need to use the IDispose interface to clean the object.
Thanks,
Peter
Amit ChoudharyPosted Mar 6, 2010, 12:38 AM
what kind of files you are converting are you using any third party tool to open the files. if it is so then you have to dispose the third party application for eg: "Ms-Excel will still run in memory when you create a file using MS Interop assembly". so check there is no such situation
there.
Hope it helps you.
Please mark the answer if it helps you