View Full Version : wsprintf() and lstrcpyn()--what's the problem!


Bowser
02-14-02, 10:38 AM
I have been playing with strings using Window's API <i>TextOut()</i> function. I can print text to the screen when I pass the characters from the keyboard into a character array (<i>char abuffer[]</i>) and then pass that array to the <i>TextOut()</i> function...

/////////////////////////////////////////////////////////////////////
<i>int charCnt; //Keeps track of number of characters
int textPosX = 1, textPosY = 1; //Client window start print text
char abuffer[]; //Holds string
.
.
.
//Keystroke
case WM_CHAR:

hdc = GetDC(hwnd); //Get handle to video device

GetTextMetrics(hdc, &txtMetrics);
abuffer[charCnt] = (char) wParam; //Load character in buffer
TextOut(hdc, textPosX, textPosY, abuffer, charCnt);
charCnt += 1;

ReleaseDC(hwnd, hdc); //Release DC
break;
.
.
. </i>
/////////////////////////////////////////////////////////////////////

But I run into problems when I try to copy text from one character array into another character array using <i>lstrcpyn()</i> or the <i>wsprintf()</i> function...

<i>wsprintf(typedChar, "%s", abuffer);
lstrcpyn(typedChar, abuffer, charCnt);</i>

When I pass the receiving array to the <i>TextOut()</i> function, that function prints "|" (bars) to the screen rather than the typed text...

<i>TextOut(hdc, textPosX, textPosY, typedChar, charCnt); </i>


///////////////////////////////////////////////////////////////////
<i>int charCnt; //Keeps track of number of characters
int textPosX = 1, textPosY = 1; //Client window start print text
char abuffer[]; //Holds string

char typedChar[]; //Holds copy of string
.
.
.
//Keystroke
case WM_CHAR:

hdc = GetDC(hwnd); //Get handle to video device

GetTextMetrics(hdc, &txtMetrics);
abuffer[charCnt] = (char) wParam; //Load character in buffer

//*****************************************
//wsprintf(typedChar, "%s", abuffer);//Copy string
// ***********or*************
//lstrcpyn(typedChar, abuffer, charCnt);//Copy string
//******************************************

TextOut(hdc, textPosX, textPosY, typedChar, charCnt);
charCnt += 1;

ReleaseDC(hwnd, hdc); //Release DC
break;
.
.
. </i>
/////////////////////////////////////////////////////////////////////

Maybe someone can tell me why the <i>lstrcpyn()</i> and <i>wsprintf()</i> functions are causing problems, and why, when I use them in the above code, the <i>TextOut()</i> function prints "|" (bars) to the screen and not the character typed on the keyboard.

It looked like a simple task to copy a string from one buffer to another using one of those functions, but it has proven to be not so simple.

I have written my own function that replaces <i>wsprintf()</i> and <i>lstrcpyn(),</i>. It does work as expected, but I'm still curious as to why the other functions wouldn't work.

//////////////////////////////////////////////////////////////////////
/////////My answer to the <i>wsprintf()</i> and <i>lstrcpyn()</i>function////////////
//////////////////////////////////////////////////////////////////////
<i>
copyStr(char ReceivBuff[], char giveBuff[], int numChr)
{
int i;
for(i=0;i!=numChr;i++)
{
ReceivBuff[i] = giveBuff[i];
}
return 0;
}
</i>
//////////////////////////////////////////////////////////////////////

I can't find anything in my books that offers a clue to an answer.

Bowser
02-16-02, 02:11 AM
Someone emailed me with the answer to my code problem. It's always where I never look--the most obvious place and the silliest of errors.

Adam
02-18-02, 12:30 AM
So what was the mistake? I can't see it. But then I'm an expert at making mistakes that NOBODY can find, especially not me.

Bowser
02-18-02, 04:55 AM
You would need to see the whole source to see the error. It was so simple, but I was looking in all the wrong places. The "break;" statement was missing from my WM_CREATE message handler. I must have deleted it while cleaning out scrap code. I never thought to look there.