Thread: Adding Strings to ListBox in a Dialog

  1. #1
    Registered User
    Join Date
    Sep 2008

    Adding Strings to ListBox in a Dialog

    Hi I have a quick question, I am trying to add strings to a listbox in that is in a dialog window, I can do it if I already define a buffer as char * buf = L"Some Text"; but I am trying to combine serveral things into one string and then add it to the listbox but when I use strcat(buf, (char *)L"Some Text"), I get a access error.

    I already tried using the buffer with out the L in front of the quotations but I just get garbage in the listbox when that is done. Here is the code that I have:
    char * buf;
    int index;
    buf = (char *)malloc(sizeof(char *) * 50);
    strcpy(buf, (char*)L"{");
    strcat(buf, (char *)L"Something");
    strcat(buf, (char *)L"}");
    index = SendDlgItemMessage(hwnd, IDC_LIST1, LB_ADDSTRING, (WPARAM)0, (LPARAM)(char*)buf);
    SendDlgItemMessage(hwnd, IDC_LIST1, LB_SETITEMDATA, (WPARAM)0, (LPARAM)index);
    ** So what I am trying to get it to do is add a string to the listbox in the following format: "{ Something }" I just get garbage when I try to enter it as a char *. But when I try to combine L"String" to my format using strcat() I get write access errors it is a constant.

    Can anyone help?


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    What you have posted should work.
    char buf1[] = "This one can't be added to.";
    char *buf2;
    buf2 = malloc(50 * sizeof(char)); /*Note char, not char *; and since sizeof(char)=1, don't need it*/
    strcpy(buf2, "This one can be.");
    strcat(buf2, "Like this.");

  3. #3
    Registered User
    Join Date
    Oct 2001
    char * buf;
    int index;
    buf = (char *)malloc(sizeof(char *) * 50);
    The cast is unnecessary in C. Also, you're allocating space for 50 char *s instead of 50 chars.

  4. #4
    Registered User
    Join Date
    Sep 2008
    sorry I my malloc code is
    buf = (char *)malloc(sizeof(char) * 50);
    It seems like it would work but when the list box is filled it gives all kinds of weird charactors and sorts... I think it is because after each letter you are suppose to have a null so,
    buf = "text" should be
    buf = {'t',0,'e',0,'x',0,'t',0,0,0}
    or atleast that is what I get when I extract a string of text from the dialog controls... So is there anyway to convert the normal char * to the LPCSTR format? and also how to expand the string, since you can't use L"text" with strcat()?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    If you are using unicode text (L"something"), then you probably should use wstrcpy/wstrcat instead of strcat/strcpy.

    You also should use wchar_t instead of char.

    If you cast L"something" with (char *) to call strcpy(), then it will copy the first character, and then stop. strcpy works on ascii/ANSI strings, not on unicode (or other multibyte character sets).

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Visalia, CA, USA
    You should probably be using TCHAR instead of char or wchar_t since that is defined by a windows header according to whether you are using Unicode or ANSI strings. You are probably actually compiling the code to use ANSI, but casting your Unicode strings to be copied over (of course, it won't be pretty since it should just copy over a single '\0'.

    Why is this not on the Windows board is really what I am wondering.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add a dialog box to a tab
    By axr0284 in forum Windows Programming
    Replies: 0
    Last Post: 01-10-2005, 08:38 AM
  2. Adding stuff to strings from files..
    By R@m0n in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2002, 02:38 PM
  3. Adding Default values to controls in Dialog boxes
    By juhigarg in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2001, 12:44 AM
  4. Adding to strings..
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 10-25-2001, 04:22 PM
  5. Adding A Toolbox in A dialog box.
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 10-25-2001, 08:34 AM