The reason for using LPSTR with integer values is that the code I am working on uses TextOut to write out the values.
There is also a function to insert commas into the numeric values for thousands, millions etc.
Magos -
xGlobalAlloc is used to allocate memory for the variable.
I think I understand what you mean. This would be the reason for using LPSTR rather than just using char.
This is the code I am working from:
Code:
/****************************************************************/
/* FormatAndPrint - print vpData at nXpos, nYpos in hDCDisplay */
/****************************************************************/
void FormatAndPrint(HDC hDCDisplay,
void _far *vpData, // Numeric value
int nXpos,
int nYpos,
BOOL bNumbers)
{
LPSTR lpszNewString = NULL;
HANDLE hNewString = 0;
if (hNewString = xGlobalAlloc(GMEM_MOVEABLE | GMEM_DISCARDABLE | GMEM_ZEROINIT, 255))
{
if (lpszNewString = GlobalLock(hNewString))
{
if (bNumbers)
{
InsertCommas(&lpszNewString, vpData);
TextOut(hDCDisplay, nXpos, nYpos, lpszNewString, lstrlen(lpszNewString));
}
else
TextOut(hDCDisplay, nXpos, nYpos, (LPSTR)vpData, lstrlen(vpData));
GlobalUnlock(hNewString);
}
xGlobalFree(hNewString);
}
}
/********************************************************************/
/* InsertCommas - formats a numeric string (with commas) and passes */
/* it back via the FormatString variable */
/* */
/* input - lppszFormatString - the string which will contain */
/* the modified number */
/* plNumber - the number we wish to format */
/* */
/********************************************************************/
void InsertCommas(LPSTR _far *lppszFormatString, LONG _far *plNumber)
{
LONG lCopyNumber = labs(*plNumber);
LPSTR lpszWorkString = NULL;
HANDLE hWorkString = 0;
if (hWorkString = xGlobalAlloc(GMEM_MOVEABLE | GMEM_DISCARDABLE, 255))
{
if (lpszWorkString = GlobalLock(hWorkString))
{
int nCommas = 0,
nLastComma = 0,
nStrLen = 0,
nCommaCnt = 0,
nCounter = 0;
/* copy it, flip it and get the stats */
wsprintf(lpszWorkString, "%ld", lCopyNumber);
_fstrrev(lpszWorkString);
nStrLen = lstrlen(lpszWorkString);
nCommaCnt = nStrLen / 3;
nLastComma = nCommaCnt * 3;
for (nCounter = 0; nCounter < nStrLen; nCounter++)
{
/* comma position ? */
if ((nCounter / 3) * 3 == nCounter && nCounter != 0)
{
*(*lppszFormatString + nCounter + nCommas) = ',';
nCommas++;
}
*(*lppszFormatString + nCounter + nCommas) = *(lpszWorkString + nCounter);
}
/* adjust for negatives */
if (*plNumber < 0)
lstrcat(*lppszFormatString, "-\0");
_fstrrev(*lppszFormatString);
GlobalUnlock(hWorkString);
}
xGlobalFree(hWorkString);
}
else
lstrcpy(*lppszFormatString, "!Error!");
}
(FYI: These are the actual variable names used in this code - handy eh!)
My concern with this is that we are losing some accuracy with basically frigging around with the data, and I was unsure as to whether the entire integer value would be passed into TextOut.
Cheers (or sorry?)
DS