OK. I'm very new to the whole gui program thing so bear with me.
I'm writing a program which uses an address book from which a user designates the receipts of a text message. However, I'm having a problem in setting the people in the send-to list. Below is some code I use to initialise the address-book listbox (for testing). The initialisation works fine, but I want to use std::strings instead of char*'s. So I replace all the char*'s with std::strings in the first code segment below and use std::string's .c_str() member function where appropriate and it looks fine on the initial address-book side.
Now. I select whomever I want to send the message to and click on the 'add' button when I use std::strings the text that's copied over is ... not right, see second link. But with char *'s it's fine ...
Does anyone know why this happens? The .c_str() returns a const char* so technically the methods I use to populate the lists are near on identical, right? But one works with the copy over while the other doesn't ... All help appreciated.
Screenshots of char* and std::string builds running:
Listbox initialization code:
Code:
const char *strs[] = { "one", "two", "three", "four", "five", "six" };
for ( int i=0, id; i<sizeof strs/sizeof(char*); i++ ) {
id = (int)SendDlgItemMessage( hwnd, IDC_LIST_ADDRESS, LB_ADDSTRING, 0, (LPARAM)strs[i] );
if ( SendDlgItemMessage( hwnd, IDC_LIST_ADDRESS, LB_SETITEMDATA,
(WPARAM)id, (LPARAM)strs[i] ) == LB_ERR )
MessageBox(hwnd, "Error in LB_GETITEMDATA", "Error", MB_OK);
}
Listbox copy code:
Code:
HWND hList_a( GetDlgItem( hwnd, IDC_LIST_ADDRESS ) ),
hList_s( GetDlgItem( hwnd, IDC_LIST_SEND_TO ) ); // Handle to address books
// get the number of selected items
int count_selected( (int)SendMessage( hList_a, LB_GETSELCOUNT, 0, 0 ) );
if( count_selected != LB_ERR && count_selected != 0 ) {
// And then allocate room to store the list of selected items.
int *buf ( (int*)GlobalAlloc( GPTR, sizeof(int) * count_selected ) );
SendMessage( hList_a, LB_GETSELITEMS, (WPARAM)count_selected, (LPARAM)buf );
for( int i=0, sel_id, iresult; i<count_selected; i++ ) {
// Add to send-book
sel_id = (int)SendDlgItemMessage( hwnd, IDC_LIST_ADDRESS, LB_GETCURSEL, 0, 0 );
iresult = (int)SendDlgItemMessage( hwnd, IDC_LIST_ADDRESS, LB_GETITEMDATA, (WPARAM)buf[i], 0 );
// If not already in the current send-list.
if ( SendMessage( hList_s, LB_FINDSTRING, 0, (LPARAM)(iresult) ) == LB_ERR )
SendDlgItemMessage( hwnd, IDC_LIST_SEND_TO, LB_ADDSTRING, 0, (WPARAM)iresult );
}
GlobalFree( buf );
}