Results 1 to 16 of 16

Thread: String Buffer sizing

  1. #1

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335

    String Buffer sizing

    Im using a LB_GETTEXTLEN to get the length on of the string but how do i make the buffer ?

    VB Code:
    1. [b]{ADDED}
    2.     char *sBuf = "";
    3. int iList;
    4. int iSel;
    5. int iSel2;
    6.                            
    7. iSel = ::SendMessage(list,LB_GETCURSEL,0,0);
    8. iSel2 = ::SendMessage(list,LB_GETTEXTLEN,iSel,0);
    9.  
    10. ::SendMessage(list,LB_GETTEXT,iSel,sBuf);
    11.  
    12. //  iList = ::GetWindowTextLength(list);
    13. //  ::GetWindowText(list,sBuf,iList);
    14.  
    15. ::SetWindowText(edit,sBuf);[/b]
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    use either:

    new

    or calloc() (malloc() in disguise)

    after using the buffer, call either delete or free(). delete goes with new, free() with calloc().

  3. #3

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    Forgot to say ima newB how do i do it? can i get a sample. Ill research on msdn but can u gimme a sample please thanks
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  4. #4

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    Look at the attached youll see my error

    Attached Images Attached Images  
    Last edited by JasonLpz; Jan 20th, 2003 at 06:40 PM.
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    PHP Code:
    {ADDED}
    char *sBuf = [color=red]NULL[/color];
    int iList;
    int iSel;
    int iSel2;
                                
    iSel = ::SendMessage(list,LB_GETCURSEL,0,0);
    iSel2 = ::SendMessage(list,LB_GETTEXTLEN,iSel,0);

    // Allocate & Initialize the string buffer
    sBuf = (char*)LocalAlloc(LPTRiSel2+1);
    memset(sBuf'\0'iSel2+1);

    ::
    SendMessage(list, LB_GETTEXTiSel, (LPARAM)sBuf);

    //    iList = ::GetWindowTextLength(list);
    //    ::GetWindowText(list,sBuf,iList);

    ::SetWindowText(edit,sBuf);

    // Free the used resource
    LocalFree((char*)sBuf); 
    You can use others function to allocate the string buffer like mention by jim mcnamara.

  6. #6

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    Thats the error i get with your code:

    VB Code:
    1. [color="#0000A0"]Error[/color] C2137: [color="#0000A0"]Empty[/color] character constant

    ON:
    VB Code:
    1. memset(sBuf, [color="#00A000"]'', iSel2+1);[/color]
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    The sample code is filered by the PHP tag,

    it should be

    VB Code:
    1. memset(sBuf, '\0', iSel2+1);

  8. #8

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    YES Great thanks

    if i had a million dollars.......










    ..........I'd be rich! lol
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Learn C++.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Frenzied Member JasonLpz's Avatar
    Join Date
    Mar 2001
    Location
    Brooklyn, NY
    Posts
    1,335
    are you talking to me? Cuz if you are you must not be very smart, because what do you think im trying to do?
    - JayWare
    Live to love. Not to Hate

    Im to busy to have a site. But I got one and still working on it.

    http://dre3k.net/

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You are using MFC without knowing enough C++ for it. That's what you're trying to do, and it's a bad thing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Hyperactive Member made_of_asp's Avatar
    Join Date
    Jul 2001
    Location
    123 Fake Street
    Posts
    394
    Isn't LocalAlloc depreciated science Windows 3.1?

    Use HeapAlloc() instead.
    VS.NET 2003

    Need to email me?

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Deprecated Depreciated means losing its value...in this case partially correct though.

    {Local,Global}Alloc were designed more around 16-bit programming, yes.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yep, those two were built around the special memory model of Winx.x. I'm happy I didn't program back then.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  15. #15
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    , i should change my code to use HeapAlloc from now.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The only one of those function that should still be used is LocalFree, because FormatMessage allocates with LocalAlloc if requested to allocate memory.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width