CHAR szBuf[MAX_PATH + 1];
CHAR* pszBuf = szBuf;
DWORD dwBuf = MAX_PATH + 1;
DWORD dwMaxBufSize = 1024;
LPSTR szVariableName = "ALL_RAW";
if ( !pECB->GetServerVariable(pECB->ConnID,
szVariableName,
pszBuf,
&dwBuf) )
{
if ( GetLastError() == ERROR_INSUFFICIENT_BUFFER )
{
//
// Not large enough buffer. Reallocate.
// Make sure to not reallocate size blindly (DoS)...
//
if ( dwBuf > dwMaxBufSize )
{
goto Failed;
}
pszBuf = new CHAR[dwBuf];
if ( pszBuf != NULL )
{
//
// Try GetServerVariable again
//
if ( !pECB->GetServerVariable(pECB->ConnID,
szVariableName,
pszBuf,
&dwBuf) )
{
//
// Unexpected failure. Fail
//
goto Failed;
}
//
// Successfully fetched value into heap buffer
//
}
else
{
//
// Failed to allocate memory. Fail
//
goto Failed;
}
}
else
{
//
// Deal with GetLastError() however you wish
// May optionally decide to fail
//
// GetLastError() == ERROR_INVALID_INDEX
// szVariableName is not a valid server variable name
//
goto Failed;
}
}
//
// pszBuf points to variable value. Use it.
// dwBuf indicates how big the buffer is (NULL included)
//
//
// Rest of code
//
//
// If we've allocated a buffer, cleanup
//
Failed:
if ( pszBuf != NULL &&
pszBuf != szBuf )
{
delete [] pszBuf;
}