Re: WCHAR conversion problem

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Iñaki" <Iaki@xxxxxxxxxxxxxxxxxxxxxxxxx> ha scritto nel messaggio
news:943EDD98-C7FF-404E-B54C-53A7A76485E0@xxxxxxxxxxxxxxxx

The first time I call the function it returns to me that it has not enough
memory space and what is the memory space that it needs, so then I make a
second call with the memory that it needs.

result_Find_First = FbwfFindFirst(szDrive,Cache_detail,&size);
while(result_Find_First == ERROR_MORE_DATA)&&(errors<2))
{
free(Cache_detail);
Cache_detail = (PFbwfCacheDetail) malloc (size);
result_Find_First = FbwfFindFirst(szDrive,Cache_detail,&size);
}

now result_Find_First is no error, so is supposed that size and
cache_detail
are correct?

if result_Find_First returned NO_ERROR, according to the documentation of
FbwfFindFirst here:

http://msdn.microsoft.com/en-us/library/aa940814.aspx

the operation completed successfully.

I would not use a 'while' in that context, I would just do something like
this:

<code>

//
// First try call (assume a "big enough" file name buffer)
//
const int initFileNameLength = 200; // in WCHAR's
ULONG cacheDetailSize = FIELD_OFFSET( FbwfCacheDetail, fileName[
initFileNameLength ] );
PFbwfCacheDetail cacheDetail = (PFbwfCacheDetail) malloc(
cacheDetailSize );

result = FbwfFindFirst( drive, cacheDetail, &cacheDetailSize );
if ( result == ERROR_MORE_DATA )
{
//
// We need to allocate a bigger structure...
//

// Release old allocation
free( cacheDetail ); cacheDetail = NULL;

// Allocate with specified size
cacheDetail = (PFbwfCacheDetail) malloc( cacheDetailSize );

result = FbwfFindFirst( drive, cacheDetail, &cacheDetailSize );
}

if ( result == NO_ERROR )
{
all right...
}
else
{
... some error occurred ...
}

</code>

Note that I would prefer to use std::vector instead of malloc()...
std::vector is exception-safe, and you don't need to bother with
free/delete[]. vector destructor will release its own resources.
e.g.

std::vector< BYTE > cacheDetailBuffer( cacheDetailSize );
PFbwfCacheDetail cacheDetail = reinterpret_cast< PFbwfCacheDetail >(
&cacheDetailBuffer[0] );

and if you want to change the size of vector, just use resize() method.

HTH,
Giovanni


.