wsprintf() and lstrcpyn()--what's the problem!

Discussion in 'Computer Science & Culture' started by Bowser, Feb 14, 2002.

Thread Status:
Not open for further replies.
  1. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    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 = giveBuff;
    }
    return 0;
    }
    </i>
    //////////////////////////////////////////////////////////////////////

    I can't find anything in my books that offers a clue to an answer.
     
    Last edited: Feb 14, 2002
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    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.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    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.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Bowser Namaste Valued Senior Member

    Messages:
    8,828
    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.
     
Thread Status:
Not open for further replies.

Share This Page