Board index » cppbuilder » using QueryDosDevice()

using QueryDosDevice()


2007-09-19 05:36:52 AM
cppbuilder88
Hi all, trying to use QueryDosDevice() to return a list of all Symbolic
names and the Logical device that they correlate too.
DWORD QueryDosDevice(
LPCTSTR lpDeviceName, // address of MS-DOS device name string
LPTSTR lpTargetPath, // address of buffer for storing query results
DWORD ucchMax // maximum storage capacity of buffer
);
I'm trying to do this, initially by passing a NULL value for the
lpDeviceName. However, i'm struggling to find a way to determine that
required size of the lpTargetPath. If i pass a buffer that is too small,
the function returns a 0 and GetLastError reports "The data area passed to a
system call is too small". However there seems to be no way to bet the size
of the required buffer. Usually, in API's, the size parameter is modified
if the buffer is too small, but in this case there seems to be no way to
determine the required buffer size.
Any suggestions?
Thanks in advance,
Mike C
 
 

Re:using QueryDosDevice()

"Mike Collins" <its@TheBottomOfThePost>wrote in message
Quote
However, i'm struggling to find a way to determine that
required size of the lpTargetPath.
You have to allocate a buffer of an initial size and then call the function
in a loop, increasing the size when needed, until the buffer is eventually
large enough, or an error occurs. For example:
DWORD dwLen = 1024;
LPTSTR lpBuffer = LocalAlloc(LPTR, dwLen);
while( lpBuffer != NULL )
{
if( QueryDosDevice(NULL, lpBuffer, dwLen) )
{
// use lpBuffer as needed ...
break;
}
if( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
{
// as error occured...
break;
}
dwLen += 1024;
LPTSTR lpNewBuffer = LocalReAlloc(lpBuffer, dwLen, LPTR);
if( lpNewBuffer == NULL )
{
// could not resize memory, original memory is still valid...
break;
}
lpBuffer = lpNewBuffer;
}
if( lpBuffer )
LocalFree(lpBuffer);
Quote
However there seems to be no way to bet the size of the
required buffer.
You have to increase the buffer by an arbitrary size of your own choosing.
The function will not tell you the size to use.
Quote
Usually, in API's, the size parameter is modified if the buffer
is too small
Not always. There are a several API functions that don't.
Gambit