How to fix error atl/mfc atlcomcli.h line 291 vscode 2022

Phan Hoài Sơn 40 Reputation points
2023-06-20T09:34:40.05+00:00

hello guys,

I'm new developer and I want to get my active url for google chrome for my project tracking.

I run code in this answer :

https://stackoverflow.com/questions/48504300/get-active-tab-url-in-chrome-with-c

But when run code, I got error from atl library :

File: C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.36.32532\atlmfc\include\atlcomcli.h
Line: 291
Expression: p == 0

Please help me to fix it.

Another , I hope to can build project like this

gcc mycode.cpp -o mycode -latl

Thank you

Developer technologies | C++
{count} votes

Accepted answer
  1. RLWA32 49,636 Reputation points
    2023-06-20T10:56:24.53+00:00

    The ATL source code ASSERTS because uia.CoCreateInstance is called twice. After the first successful call the uia smart pointer variable contains an IUIAutomation interface pointer in member variable p. In a debug build the ATL source code call ATLASSERT to alert that the CComPtr::CoCreateInstance member function has been called on a smart pointer that has already been initialized.

    Adjust the code so that uia.CoCreateInstance is only called once if it is successful.


2 additional answers

Sort by: Most helpful
  1. Phan Hoài Sơn 40 Reputation points
    2023-06-20T10:08:10.1566667+00:00

    here is my code

    #include "iostream"
    
    #include <Windows.h>
    #include <stdio.h>
    #include <AtlBase.h>
    #include <AtlCom.h>
    #include <UIAutomation.h>
    
    bool find_url(IUIAutomation* uia, IUIAutomationElement* root)
    {
        std::cout << "find_url starting ..." << std::endl;
    
        // The root window has several childs, 
        // one of them is a "pane" named "Google Chrome"
        // This contains the toolbar. Find this "Google Chrome" pane:
        CComPtr<IUIAutomationElement> pane;
        CComPtr<IUIAutomationCondition> pane_cond;
        uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
            CComVariant(UIA_PaneControlTypeId), &pane_cond);
    
        CComPtr<IUIAutomationElementArray> arr;
        if FAILED(root->FindAll(TreeScope_Children, pane_cond, &arr))
            return false;
    
        int count = 0;
        arr->get_Length(&count);
        std::cout << "start find_url count " << count << std::endl;
    
        for (int i = 0; i < count; i++)
        {
            CComBSTR name;
            if SUCCEEDED(arr->GetElement(i, &pane))
                std::cout << "start find_url pane  " << pane << std::endl;
            if SUCCEEDED(pane->get_CurrentName(&name))
                std::wcout << "start find_url pane name " << name.m_str << std::endl;
            if (wcscmp(name, L"Chrome Legacy Window") == 0)
                        break;
            pane.Release();
        }
    
        std::cout << "find_url panel chrome " << pane << std::endl;
        if (!pane)
            return false;
    
        std::cout << "find_url gg chrome panel existed " << pane << " count : " << count << std::endl;
    
        //look for first UIA_EditControlTypeId under "Google Chrome" pane
        CComPtr<IUIAutomationElement> url;
        CComPtr<IUIAutomationCondition> url_cond;
        uia->CreatePropertyCondition(UIA_ControlTypePropertyId,
            CComVariant(UIA_EditControlTypeId), &url_cond);
        if FAILED(pane->FindFirst(TreeScope_Descendants, url_cond, &url))
            return false;
        std::cout << "CreatePropertyCondition url " << url << std::endl;
    
        //get value of `url`
        CComVariant var;
        if FAILED(url->GetCurrentPropertyValue(UIA_ValueValuePropertyId, &var))
            return false;
        if (!var.bstrVal)
            return false;
        wprintf(L"find_url: %s\n", var.bstrVal);
    
        //set new address ...
        IValueProvider* pattern = nullptr;
        if (FAILED(url->GetCurrentPattern(UIA_ValuePatternId, (IUnknown**)&pattern)))
            return false;
        //pattern->SetValue(L"somewhere.com");
        pattern->Release();
    
        INPUT input[2] = { INPUT_KEYBOARD };
        input[0].ki.wVk = VK_RETURN;
        input[1] = input[0];
        input[1].ki.dwFlags |= KEYEVENTF_KEYUP;
        SendInput(2, input, sizeof(INPUT));
    
        return true;
    }
    
    int main() {
    	std::cout << "hello world" << std::endl;
    
        HWND hwnd = nullptr;
        while (true)
        {
            hwnd = FindWindowEx(nullptr, hwnd, L"Chrome_WidgetWin_1", nullptr);
            if (!hwnd)
                return 0;
            if (IsWindowVisible(hwnd) && GetWindowTextLength(hwnd) > 0)
                break;
        }
        std::cout << "CoInitializeEx" << std::endl;
    
        //CoInitializeEx(nullptr, COINIT_MULTITHREADED);//<- pick the right one
        CoInitializeEx(nullptr, COINIT_MULTITHREADED);
        CComPtr<IUIAutomation> uia;
        while (true) {
            unsigned int miliseconds = 1000;
            Sleep(miliseconds);
            
            std::cout << "SUCCEEDED start : " << SUCCEEDED(uia.CoCreateInstance(CLSID_CUIAutomation)) << std::endl;
    
            if SUCCEEDED(uia.CoCreateInstance(CLSID_CUIAutomation))
            {
                CComPtr<IUIAutomationElement> root;
                if SUCCEEDED(uia->ElementFromHandle(hwnd, &root))
                    find_url(uia, root);
            }
        }
    
         uia.Release();
    
        CoUninitialize();
    	return 1;
    }
    
    
    0 comments No comments

  2. DÊIVIS SANCIULIS DE FREITAS 75 Reputation points
    2023-06-21T02:39:30.94+00:00

    O erro que você está vendo é provavelmente um erro de assertiva de tempo de execução. Em C++, assertivas são usadas para declarar condições que são verdadeiras, de modo que, se a condição for falsa, o programa irá parar.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.