Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6368585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:41:41+00:00 2026-05-25T00:41:41+00:00

I have an EditBox HWND tbLog , and the following function (which doesn’t work):

  • 0

I have an EditBox HWND tbLog, and the following function (which doesn’t work):

void appendLogText(char* newText)
{
  int len = GetWindowTextLength(tbLog);

  char* logText = new char[len + strlen(newText) + 1];
  GetWindowText(tbLog, logText, len);

  strcat(logText, newText);

  SendMessage(tbLog, WM_SETTEXT, NULL, (LPARAM)TEXT(logText));

  delete[] logText;
}

and I call it like so:

appendLogText("Put something in the Edit box.\r\n");
appendLogText("Put something else in the Edit box.\r\n");

First of all, what does TEXT() actually do? I have tried with/without it: (LPARAM)logText and (LPARAM)TEXT(logText) and there is no difference as far as I can see.

Second, what am I doing wrong in my append function? If I comment out my delete line, then the first time I run the append function, I get garbage coming up in my Editbox, followed by the message. The second time I run it, the program crashes. If I don’t have it commented out, then it crashes even the first time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:41:42+00:00Added an answer on May 25, 2026 at 12:41 am

    I would rewrite the function, in C, like this:

    void appendLogText(LPCTSTR newText)
    {
      DWORD l,r;
      SendMessage(tbLog, EM_GETSEL,(WPARAM)&l,(LPARAM)&r);
      SendMessage(tbLog, EM_SETSEL, -1, -1);
      SendMessage(tbLog, EM_REPLACESEL, 0, (LPARAM)newText);
      SendMessage(tbLog, EM_SETSEL,l,r);
    }
    

    Its important to save and restore the existing selection or the control becomes very annoying for anyone to use who wants to select and copy some text out of the control.

    Also, the use of LPCTSTR ensures that the function can be called when you build with either a multibyte or unicode character set.

    The TEXT macro was out of place, and should be used to wrap string literals:

    LPCTSTR someString = TEXT("string literal");
    

    Windows NT Operating systems are natively unicode so building multibyte applications is inefficient. Using TEXT() on string literals, and LPTSTR in place of ‘char*’ helps with this conversion to unicode. But really it would probably be most efficient to just switch explicitly to unicode programming on windows: in place of char, strlen, std::string, use wchar_t, std::wstring, wsclen, and L”string literals”.

    Switching your projects build settings to Unicode will make all the windows API functions expect unicode strings.


    It has been brought to my very belated attention that passing -1 as the WPARAM of EM_SETSEL merely unselects any selection but does not move the insertion point. So the answer should be ammended to (also untested):

    void appendLogText(HWND hWndLog, LPCTSTR newText)
    {
      int left,right;
      int len = GetWindowTextLength(hWndLog);
      SendMessage(hWndLog, EM_GETSEL,(WPARAM)&left,(LPARAM)&right);
      SendMessage(hWndLog, EM_SETSEL, len, len);
      SendMessage(hWndLog, EM_REPLACESEL, 0, (LPARAM)newText);
      SendMessage(hWndLog, EM_SETSEL,left,right);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you managed to get Aptana Studio debugging to work? I tried following this,
in my app i have two editbox in which the user types the email
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have three questions about editbox control in WINAPI (i can't find information on
I am having a Password textbox which will have empty value. when the user
I have the following aspx : <div id=columns runat=server> <ul id=column1 class=column > <!--
I have a WebView which has a form containing 10 editboxes. When I type
I have the following class public class Car { public Name {get; set;} }
i am new to android help me pls,i have 4 editbox and i button
In Android I have the following code to blank an EditText box when clicked.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.