Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IAutoComplete custom source with IEnumString

I am trying to implement auto-suggest for a comboxbox (win32, c++) or an edit-control. But I dont know how to use the interface IAutoComplete correctly. I need a custom list of strings where the matches for auto-suggest should be taken from. But how to implement this with IEnumString? I found this link but it doesnt reveal everything: http://msdn.microsoft.com/en-us/library/windows/desktop/hh127437%28v=vs.85%29.aspx

Has anybody ever implemented this? Thx in advance Michael

like image 699
Michbeckable Avatar asked Feb 22 '23 17:02

Michbeckable


2 Answers

As I noted in my comment, ATL has one pre-written for you.

typedef CComEnum<IEnumString,
                 &IID_IEnumString,
                 LPOLESTR,
                 _Copy<LPOLESTR> > CComEnumString;

CComObject<CComEnumString> *pes;
HRESULT hr = CComObject<CComEnumString>::CreateInstance(&pes);

That code was basically stolen from the CComEnum documentation.

like image 54
Raymond Chen Avatar answered Mar 03 '23 10:03

Raymond Chen


Ok, with the help of all answers I got it so far:

            IAutoComplete *pac;

        HRESULT hr = CoCreateInstance(CLSID_AutoComplete, 
                                        NULL, 
                                      CLSCTX_INPROC_SERVER,
                                      IID_PPV_ARGS(&pac));

        typedef CComEnum<IEnumString,
             &IID_IEnumString,
             LPOLESTR,
             _Copy<LPOLESTR> > CComEnumString;

        CComObject<CComEnumString> *pes;
        HRESULT hRes = CComObject<CComEnumString>::CreateInstance(&pes);

        // hRes = pes->Init(

        IUnknown* pUnk;
        hRes = pes->QueryInterface(IID_IEnumString, (void**) &pUnk);

        pac->Init(hEdit, pUnk, NULL, NULL);

        // maybe we release ?
        pUnk->Release();

        IAutoComplete2 *pac2;

        if (SUCCEEDED(pac->QueryInterface(IID_IAutoComplete2, (LPVOID*)&pac2)))
        {
            pac2->SetOptions(ACO_AUTOSUGGEST);
            pac2->Release();
        }

There is just one thing remaining:

The initialisation of the CComObject *pes. Lets assume I have an array like this:

std::string myArray[] = { string("abc"), string("foo"), string("muh") };

Now I want these strings to be stuffed into the pes->Init(...) method. How is the conversion to LPOLESTR actually done here? The Init(...) method taes a pointer to the begin and end of this array. The end should be one position BEYOND the last array element, so would this be myArray[3] here? I am just asking because I think this is actually out of bounds in this array?

Thx very much!

like image 27
Michbeckable Avatar answered Mar 03 '23 08:03

Michbeckable