||SQL Server Tips by Gama and Naughter
This is the corollary function to XP_INI_WRITESTRING. The underlying
SDK function used is “GetPrivateProfileString”. This XP takes 5
parameters, namely the ini filename, the section to use, the key to
read from, the default value to return if not found and finally the
actual output parameter, which contains the string that was read.
Because we are reading in data, we need to allocate memory to store
the received data. This is handled using appropriate exception
handling to catch memory allocation failures. We also need to handle
the fact that we do not know the full size of the string value we
are reading a priori. We achieve this by first calling the
“GetPrivateProfileString” function with an initial buffer size of 1
kilobyte and if the function fails due to lack of buffer space then
we retry the function using a buffer size which is doubled. When the
function eventually succeeds the string data read is returned in the
output parameter using “CExtendedStoredProcedure::ParamSetOutput”.
The code also looks at the data type of the output parameter to
ensure it handles differences between UNICODE and ASCII strings.
The above book excerpt is from:
Turbocharge Database Performance with C++ External Procedures
Joseph Gama, P. J. Naughter