Board index » cppbuilder » Re: BCB6: Creating a file HANDLE

Re: BCB6: Creating a file HANDLE


2006-02-07 06:22:15 AM
cppbuilder61
"poojo hackma" <poojo.com/mail>wrote:
Quote
I feel silly asking this, but I don't seem to find
how to initialize a file HANDLE for a call to
GetFileSize().
Use the CreateFile() function, ie:
void TForm1::GetSize(char* fullPath)
{
HANDLE hnd = CreateFile(fullPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( hnd != INVALID_HANDLE_VALUE )
{
DWORD dwSize = GetFileSize(hnd, NULL);
CloseHandle(hnd);
if( dwSize != 0xFFFFFFFF )
{
Label1->Caption = dwSize;
return;
}
}
// display an error
}
Alternatively, you can use FindFirstFile() instead. Then you do not have to open the file at all, ie:
void TForm1::GetSize(char* fullPath)
{
WIN32_FIND_DATA fd = {0};
HANDLE hnd = FindFirstFile(fullPath, &fd);
if( hnd != INVALID_HANDLE_VALUE )
{
CloseHandle(hnd);
Label1->Caption = fd.nFileSizeLow;
return;
}
// display an error
}
Gambit
 
 

Re:Re: BCB6: Creating a file HANDLE

I feel silly asking this, but I don't seem to find how to initialize a file
HANDLE for a call to GetFileSize().
void TForm1::GetSize(char* fullPath)
{
HANDLE hnd;
DWORD dwSize;
AnsiString aStr;
hnd = MagicHandleCreator(fullPath); // ???
dwSize = GetFileSize(hnd, NULL);
if (dwSize == NULL) // ...magic failed
{
// display an error
return;
}
aStr.printf("%lu", dwSize);
Label1->Caption = aStr;
}
What is the correct MagicHandleCreator() that I need?
 

Re:Re: BCB6: Creating a file HANDLE

Thanks Gambit. That got it.
You know, I like to say that I know how to program, but at the end of the
day it boils down to the fact that there are a few people in these groups
who are kind enough to show me the way.
 

{smallsort}