How do I add CB_SETITEMDATA to a comboBox with wstring variable as the LPARAM?

Posted on 2006-04-30
Last Modified: 2013-12-14
Also, is it correct and OK to put the declaration of the wstring in side of this loop?  Currently, with this code I'm getting the following error message from the compiler.
Error      1      error C2440: 'initializing' : cannot convert from 'std::wstring *' to 'std::basic_string<_Elem,_Traits,_Ax>'      c:\Projects\UnityExtProperty\CUserPage.Cpp      884

//while (loop condition is true)

                                    // Add the context cn attribute
                                    std::wstring wsGroup = new wstring;
                                    wsGroup = (colSearchColumnDN.pADsValues->CaseIgnoreString);
                                    SendMessage(hwndPrimGroup,CB_SETITEMDATA, index, (LPARAM)T2CW(wsGroup.c_str()));
                                    padsSearchContainer->FreeColumn( &colSearchColumnDN );

//end while loop
Question by:lapucca
    LVL 48

    Accepted Solution

    Allocate strung dynamically.

                                  std::wstring* wsGroup = new wstring(colSearchColumnDN.pADsValues->CaseIgnoreString);
                                  SendMessage(hwndPrimGroup,CB_SETITEMDATA, index, (LPARAM)wsGroup);

    When you read string using CB_GETITEMDATA, cast result to wstring*. Don't forget to release all strings associated with items.

    If you want to keep plain string and not wstring*, don't use T2CW macro, all these macros create pointers which are destroyed when function exits. In this case you need allocate memory block dynamically using new wchar and copy data to it.

    Author Comment

    The problem is that the vector doesn't like stroing pointers.  I had code before to store pointers to object of my class and the program would crash randomly.  And I also get memory uninitilize in Rational Purfiy.  Are you sure that it would store wstring*?  Isn't wstring already a pointer of wchar_t*?
    LVL 17

    Expert Comment

    This questions needs to be cross-referenced with your other question at http:/Q_21833678.html

    I recommend that you close one of these questions, lapucca, and invite the experts from it to the other question.

    We need to see what code you currently have that's causing you grief in the vector. It is hard to see what the state of play is. Please can you show us you current code with current error messages.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now