aspnet_wp will not release file handle

  • Thread starter Thread starter fernandez.dan
  • Start date Start date
F

fernandez.dan

I have a umangaged dll ( I have the source code) which I use in my web
app using pinvoke. The dll creates a file, writes to it, and then
closes it. It works the first time through but the second time through
it can't open file so my dll fails and returns an error number.

I then try to delete the file manually but it says that I can not
delete it because it is use by other program or user. I used process
explorer utility from sysinternals to find out who had the file handle.
It seems that the aspnet_wp has the file handle.

It's weird if I wait for a long period of time, I can finally manually
delete the file. I'm not sure if it takes a period of time for the
aspnet_wp process to release resources.

Just an high overview of the file call sequence in my unmanaged dll
int pMode = 0x20;
hFile = CreateFile(filename,GENERIC_WRITE,0, NULL, CREATE_ALWAYS,
pMode, NULL);
WriteFile(hFile,cmdstring,strlen(cmdstring), &bytes,NULL);
CloseHandle(hFile);

I appreciate any advice.
 
Does your .NET code use the file at all? Or just the unmanaged dll? It
appears to be closed correctly, assuming there are no branches or
exceptions that would cause the execution path to miss the CloseHandle
API.
 
Yeah,

The filename is based on the user information provided in a web form so
the filename can change from user to user. So I just use the
Directory.GetFiles call to get the files in a directory and search for
my file by a keyword from the web form. When I find the file I'm
looking for, I get the full path name so I can set up a URL for the
user to download it if they choose.

But there is a chance that information provided by the user can lead to
the same filename, so before calling the unmanaged dll I try to delete
the file with the same keyword. When the file gets locked this
opeartion throws an exception because it can't delete the file.

I know it's weird but even if I have the source for the dll, I
constraint what I can do to it.

Here is the sequence
User enters info in web form and submits
I check if any other file with same keyword if so delete
Call unmanaged dll with user info
Lookup file with keyword and set up URL path

Thanks
 
Interesting, because it really does sound like it should work. I was
thinking perhaps the code opened the file to send results back to the
user but didn't Close or Dispose the File object and left the file
open.
 
Yeah it is weird,

Plus if I wait long enough I can then delete the file. It's like it's
taking aspnet_wp process awhile to let go of the handle. I am curious
if there is a way to force aspnet_wp to let go of the file handle or if
I can force a delete on a file.

I am open for any options. Thanks

Danny
 
Thanks Guys,

I was using someone else code to log my entry in a database. My friend
forgot to close the file. He read the file and log the file in a
database. I think he owe's me a red bull, I spent hours on my code
trying to figure out what I did wrong.

Thanks for all the help.

Danny
 
Back
Top