HEre is the working test code
- Code: Select all
struct DIRINFO{
char *diPath;
char *diFile;
int isSubDirSearch;
}; //structure to hold information for the SearchDir function
void DirThread()
{
Clear();
//Allocating a pointer to the structure for later reallocating it.
DIRINFO *dirInfo = Malloc(sizeof(DIRINFO));
//if(dirInfo == NULL){ return;}
HANDLE hThread = CreateThread( "RanThread", (DWORD)dirInfo);
if(!hThread){
CloseHandle(hThread);
return;
}
CloseHandle(hThread);
return;
}
DWORD RanThread( DWORD dwParam )
{
//ReAllocate the structure we allocated earlier.
DIRINFO *dInfo = ReAlloc(&dwParam,sizeof(DIRINFO));
dInfo->diPath = "C:\\";
dInfo->diFile = "*.mp3";
dInfo->isSubDirSearch = 0;
PrintF("Info on dirInfo: diInfo->diPath = %s , diInfo->diFile = %s, diInfo.Subs = %i",dInfo->diPath, dInfo->diFile, dInfo->isSubDirSearch);
return 0;
}
VOID On_HK_0()
{
DirThread();
}
It can be cleaned up and fixed to work better, but i only have a couple of hours between morning and work
And here is the modified for searchdir that compiles but crashes.
- Code: Select all
struct DIRINFO{
char *diPath;
char *diFile;
int isSubDirSearch;
}; //structure to hold information for the SearchDir function
void DirThread()
{
Clear();
//Allocating a pointer to the structure for later reallocating it.
DIRINFO *dirInfo = Malloc(sizeof(DIRINFO));
//if(dirInfo == NULL){ return;}
HANDLE hThread = CreateThread( "RanThread", (DWORD)dirInfo);
if(!hThread){
CloseHandle(hThread);
return;
}
CloseHandle(hThread);
return;
}
DWORD RanThread( DWORD dwParam )
{
//ReAllocate the structure we allocated earlier.
DIRINFO *dInfo = ReAlloc(&dwParam,sizeof(DIRINFO));
dInfo->diPath = "C:\\";
dInfo->diFile = "*.mp3";
dInfo->isSubDirSearch = 0;
//Now we pass the newly reallocated structure filled in to modified SearchDir.
int i = SearchDir(dInfo);
return 0;
}
INT SearchDir( DIRINFO *SInfo)
{
//Reaching near the end i realized it would have been better to create a new copy of the structure so as to not modify the one passed
//But thats for YOU to do!
CHAR szFinal[MAX_PATH];
SNPrintF( szFinal, MAX_PATH, "%s%s", SInfo->diPath, SInfo->diFile);
WIN32_FIND_DATA wfdFind;
INT iFound = 0;
HANDLE hFind = FindFirstFile(szFinal, &wfdFind);
//if handle is not invalid continue
if( (DWORD)hFind != (DWORD)INVALID_HANDLE_VALUE)
{
while(hFind)
{
if( StrCmp(wfdFind.cFileName, ".") != 0 && StrCmp( wfdFind.cFileName, "..") != 0 )
{
if((wfdFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{
iFound++;
char szFile[MAX_PATH];
SNPrintF(szFile, MAX_PATH, "%s%s", SInfo->diPath, wfdFind.cFileName);
PrintF("%s", szFile );
}
}
//Get Next File
if(!FindNextFile(hFind, &wfdFind) )
{
FindClose(hFind);
hFind = NULL;
break;
}
}
}
if( SInfo->isSubDirSearch == 0 ) {return iFound;}
SNPrintF( szFinal, MAX_PATH, "%s*", SInfo->diPath);
hFind = FindFirstFile( szFinal, &wfdFind );
if ( (DWORD)hFind != (DWORD)INVALID_HANDLE_VALUE ) {
while ( hFind ) {
if ( StrCmp( wfdFind.cFileName, "." ) != 0 && StrCmp( wfdFind.cFileName, ".." ) != 0 ) {
// This time we only care about directories
if ( wfdFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) {
// Recompile the string and pass it right back to this function in a
// recursive call.
CHAR szDir[MAX_PATH];
SNPrintF( szDir, MAX_PATH, "%s%s\\", SInfo->diPath, wfdFind.cFileName );
//PrintF( "%s", szDir );
// Tricky part here. Gotta modify the structure abit to accomodate the subdir variable.
// I went through two methods but i will choose this one >.<
SInfo->isSubDirSearch--;
iFound += SearchDir( SInfo );
}
}
// Get the next file.
if ( !FindNextFile( hFind, &wfdFind ) ) {
FindClose( hFind );
hFind = NULL;
break;
}
}
} // hFind != INVALID_HANDLE_VALUE
// We are done, and iFound contains the total matching files.
return iFound;
}
VOID On_HK_0()
{
DirThread();
}
I didnt realize the function used recursion..poo me.