PDA

Click to See Complete Forum and Search --> : String Buffer sizing


JasonLpz
Jan 20th, 2003, 05:24 PM
Im using a LB_GETTEXTLEN to get the length on of the string but how do i make the buffer ?

{ADDED}
char *sBuf = "";
int iList;
int iSel;
int iSel2;

iSel = ::SendMessage(list,LB_GETCURSEL,0,0);
iSel2 = ::SendMessage(list,LB_GETTEXTLEN,iSel,0);

::SendMessage(list,LB_GETTEXT,iSel,sBuf);

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

::SetWindowText(edit,sBuf);

jim mcnamara
Jan 20th, 2003, 05:28 PM
use either:

new

or calloc() (malloc() in disguise)

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

JasonLpz
Jan 20th, 2003, 05:29 PM
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

JasonLpz
Jan 20th, 2003, 05:37 PM
Look at the attached youll see my error

http://www.vbforums.com/attachment.php?s=&postid=1335196

Chris
Jan 20th, 2003, 06:19 PM
{ADDED}
char *sBuf = NULL;
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(LPTR, iSel2+1);
memset(sBuf, '\0', iSel2+1);

::SendMessage(list, LB_GETTEXT, iSel, (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.

JasonLpz
Jan 20th, 2003, 07:01 PM
Thats the error i get with your code:

Error C2137: Empty character constant

ON:
memset(sBuf, '', iSel2+1);

Chris
Jan 20th, 2003, 07:13 PM
The sample code is filered by the PHP tag,

it should be


memset(sBuf, '\0', iSel2+1);

JasonLpz
Jan 20th, 2003, 07:22 PM
YES Great thanks

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










..........I'd be rich! lol

CornedBee
Jan 21st, 2003, 10:37 AM
Learn C++.

JasonLpz
Jan 21st, 2003, 12:08 PM
are you talking to me? Cuz if you are you must not be very smart, because what do you think im trying to do?

CornedBee
Jan 22nd, 2003, 04:59 AM
You are using MFC without knowing enough C++ for it. That's what you're trying to do, and it's a bad thing.

made_of_asp
Jan 26th, 2003, 07:13 AM
Isn't LocalAlloc depreciated science Windows 3.1?

Use HeapAlloc() instead.

parksie
Jan 26th, 2003, 07:17 AM
Deprecated ;) Depreciated means losing its value...in this case partially correct though.

{Local,Global}Alloc were designed more around 16-bit programming, yes.

CornedBee
Jan 26th, 2003, 03:31 PM
Yep, those two were built around the special memory model of Winx.x. I'm happy I didn't program back then.

Chris
Jan 26th, 2003, 06:56 PM
:p, i should change my code to use HeapAlloc from now.

CornedBee
Jan 27th, 2003, 10:45 AM
The only one of those function that should still be used is LocalFree, because FormatMessage allocates with LocalAlloc if requested to allocate memory.