From: seljuk on
I write a code to see files and subdirectories of a directory.

But if a directory has no subdirectory or any file, findClose does not
release directory handle. So I cannot remove that directories.
(It does not enter if block pointed by 4). But filefinddata shows ".."
as cfilename. But handle shows 0xFFFFFF.Although it's value equals to
INVALID_HANDLE_VALUE It has lock over directory that I want to list.

but if there is any file or sub directory in the directory it enters if
block and handle shows valid value and findClose works and releases
lock over the directory.

Note1: I check locks over directories with "Unlocker 1.7.1"
Note2: I work with platform builder and x86 emulator. So I can control
directories on my disk.

SOURCE CODE

void listView::ReadDirectory(LPTSTR dir,HWND listHandle)
{
LPTSTR retVal=(LPTSTR)LocalAlloc(LPTR, 500);
1--> WIN32_FIND_DATA FindFileData;
2--> HANDLE hFind;
retVal=wcscat(retVal,dir);
int listNumber;
if(wcslen(dir)!=0){
insertDefaultDir(listHandle);
listNumber=0;
}else{
listNumber=-1;
}
if(hWnd!=listHandle){
otherList->activeDir=dir;
}else{
activeDir=dir;
}
retVal=wcscat(retVal,_T("\\*.*"));
3--> hFind = FindFirstFile(retVal, &FindFileData);
SendMessage(listHandle,WM_SETREDRAW ,false,NULL);
4--> if(INVALID_HANDLE_VALUE !=hFind)
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
listNumber++;
LV_ITEM litem=createListItem(listNumber,FindFileData.cFileName);
ListView_InsertItem(listHandle, &litem);
ListView_SetItemText(listHandle, listNumber, 1, _T("<dir>"));
SYSTEMTIME myTime;
FileTimeToSystemTime(&FindFileData.ftLastWriteTime,&myTime);
LPTSTR dateValue=convertDateToString(myTime);
ListView_SetItemText(listHandle,listNumber,3,dateValue);
LocalFree(dateValue);
}
else if(! (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
{
listNumber++;
int pos=findR(FindFileData.cFileName,'.');
LPTSTR
filename=pos!=-1?mid(FindFileData.cFileName,0,pos):FindFileData.cFileName;
LPTSTR
ext=pos==-1?_T(""):mid(FindFileData.cFileName,pos+1,wcslen(FindFileData.cFileName)-(pos+1));
LV_ITEM litem=createListItem(listNumber,filename);
ListView_InsertItem(listHandle, &litem);
ListView_SetItemText(listHandle, listNumber, 1, ext);
long fileSize=(FindFileData.nFileSizeHigh * MAXDWORD+1) +
FindFileData.nFileSizeLow;
ListView_SetItemText(listHandle,listNumber,2,convertToString(fileSize,10));
SYSTEMTIME myTime;
FileTimeToSystemTime(&FindFileData.ftLastWriteTime,&myTime);
LPTSTR dateValue=convertDateToString(myTime);
ListView_SetItemText(listHandle,listNumber,3,dateValue);
if(pos!=-1){
LocalFree(filename);
LocalFree(ext);
}
LocalFree(dateValue);
}

6 --> }while (FindNextFile(hFind, &FindFileData) != 0);
7--> FindClose(hFind);
LocalFree(retVal);
SendMessage(listHandle,WM_SETREDRAW ,true,NULL);
UpdateWindow(listHandle);
focusedItem=0;
ListView_SetItemState(listHandle,focusedItem,LVIS_FOCUSED
,LVIS_FOCUSED); // select item
}

THANKS IN ADVANCE

From: David Lowndes on
>I write a code to see files and subdirectories of a directory.
>
>But if a directory has no subdirectory or any file, findClose does not
>release directory handle. So I cannot remove that directories.

You've presented a lot of code, most of which is superfluous to the
nature of your problem.

Can you post a minimal example that illustrates just what the problem
is - it's hard to see the wood for the trees in your current example.

From what you mention, are you using a pocket PC device emulator? Does
the same problem exist for a Win32 application run on your desktop?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
From: seljuk on
SIMPLIFIED VERSION

void listView::ReadDirectory(LPTSTR dir,HWND listHandle)
{
LPTSTR retVal=(LPTSTR)LocalAlloc(LPTR, 500);
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
retVal=wcscat(retVal,_T("\\*.*"));
hFind = FindFirstFile(retVal, &FindFileData);
if(INVALID_HANDLE_VALUE !=hFind)
do
{
//PROCESS FILE INFO
}while (FindNextFile(hFind, &FindFileData) != 0);
FindClose(hFind);
}

I restate the problem :

If there is no any file or sub directory in my search directory hFind
get the INVALID HANDLE VALUE
and findclose does not close that handle but when I look with unlocker
directory is locked by CESVCH~1.exe.
If there is any file in the directory handle gets valid value and
findclose works.

From: seljuk on
SIMPLIFIED VERSION

void listView::ReadDirectory(LPTSTR dir,HWND listHandle)
{
LPTSTR retVal=(LPTSTR)LocalAlloc(LPTR, 500);
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
retVal=wcscat(retVal,_T("\\*.*"));
hFind = FindFirstFile(retVal, &FindFileData);
if(INVALID_HANDLE_VALUE !=hFind)
do
{
//PROCESS FILE INFO
}while (FindNextFile(hFind, &FindFileData) != 0);
FindClose(hFind);
}

I restate the problem :

If there is no any file or sub directory in my search directory hFind
get the INVALID HANDLE VALUE
and findclose does not close that handle but when I look with unlocker
directory is locked by CESVCH~1.exe.
If there is any file in the directory handle gets valid value and
findclose works.

Note: I work with win CE 5.0
I didn't try with desktop application.

From: David Lowndes on
>If there is no any file or sub directory in my search directory hFind
>get the INVALID HANDLE VALUE
>and findclose does not close that handle but when I look with unlocker
>directory is locked by CESVCH~1.exe.
>If there is any file in the directory handle gets valid value and
>findclose works.
>
>Note: I work with win CE 5.0
>I didn't try with desktop application.

I've tried the essence of your code like this:

void ReadDirectory()
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile( "C:\\Test1\\*.*", &FindFileData);
if(INVALID_HANDLE_VALUE !=hFind)
do
{
//PROCESS FILE INFO
}
while (FindNextFile(hFind, &FindFileData) != 0);
FindClose(hFind);
}

int _tmain(int argc, _TCHAR* argv[])
{
ReadDirectory( );
return 0;
}

.... and don't have a problem on Win XP SP2. I get a valid hFind value
for an empty sub-directory.

While the handle is still open, an attempt to delete the folder fails
with:

---------------------------
Error Deleting File or Folder
---------------------------
Cannot remove folder Test1: It is being used by another person or
program.

Close any programs that might be using the file and try again.

.... and once the FileClose call has been done, I can delete the
folder.

Are you using a device emulator or a real-device?

I can only suggest that you check your code on a desktop OS to verify
things.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq