2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
23 ==============================================================================
29 class WindowsUIAWrapper
: public DeletedAtShutdown
32 bool isLoaded() const noexcept
34 return uiaReturnRawElementProvider
!= nullptr
35 && uiaHostProviderFromHwnd
!= nullptr
36 && uiaRaiseAutomationPropertyChangedEvent
!= nullptr
37 && uiaRaiseAutomationEvent
!= nullptr
38 && uiaClientsAreListening
!= nullptr
39 && uiaDisconnectProvider
!= nullptr
40 && uiaDisconnectAllProviders
!= nullptr;
43 //==============================================================================
44 LRESULT
returnRawElementProvider (HWND hwnd
, WPARAM wParam
, LPARAM lParam
, IRawElementProviderSimple
* provider
)
46 return uiaReturnRawElementProvider
!= nullptr ? uiaReturnRawElementProvider (hwnd
, wParam
, lParam
, provider
)
50 JUCE_COMRESULT
hostProviderFromHwnd (HWND hwnd
, IRawElementProviderSimple
** provider
)
52 return uiaHostProviderFromHwnd
!= nullptr ? uiaHostProviderFromHwnd (hwnd
, provider
)
53 : (HRESULT
) UIA_E_NOTSUPPORTED
;
56 JUCE_COMRESULT
raiseAutomationPropertyChangedEvent (IRawElementProviderSimple
* provider
, PROPERTYID propID
, VARIANT oldValue
, VARIANT newValue
)
58 return uiaRaiseAutomationPropertyChangedEvent
!= nullptr ? uiaRaiseAutomationPropertyChangedEvent (provider
, propID
, oldValue
, newValue
)
59 : (HRESULT
) UIA_E_NOTSUPPORTED
;
62 JUCE_COMRESULT
raiseAutomationEvent (IRawElementProviderSimple
* provider
, EVENTID eventID
)
64 return uiaRaiseAutomationEvent
!= nullptr ? uiaRaiseAutomationEvent (provider
, eventID
)
65 : (HRESULT
) UIA_E_NOTSUPPORTED
;
68 BOOL
clientsAreListening()
70 return uiaClientsAreListening
!= nullptr ? uiaClientsAreListening()
74 JUCE_COMRESULT
disconnectProvider (IRawElementProviderSimple
* provider
)
76 if (uiaDisconnectProvider
!= nullptr)
78 const ScopedValueSetter
<IRawElementProviderSimple
*> disconnectingProviderSetter (disconnectingProvider
, provider
);
79 return uiaDisconnectProvider (provider
);
82 return (HRESULT
) UIA_E_NOTSUPPORTED
;
85 JUCE_COMRESULT
disconnectAllProviders()
87 if (uiaDisconnectAllProviders
!= nullptr)
89 const ScopedValueSetter
<bool> disconnectingAllProvidersSetter (disconnectingAllProviders
, true);
90 return uiaDisconnectAllProviders();
93 return (HRESULT
) UIA_E_NOTSUPPORTED
;
96 //==============================================================================
97 bool isProviderDisconnecting (IRawElementProviderSimple
* provider
)
99 return disconnectingProvider
== provider
|| disconnectingAllProviders
;
102 //==============================================================================
103 JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (WindowsUIAWrapper
)
106 //==============================================================================
109 // force UIA COM library initialisation here to prevent an exception when calling methods from SendMessage()
111 returnRawElementProvider (nullptr, 0, 0, nullptr);
113 jassertfalse
; // UIAutomationCore could not be loaded!
118 disconnectAllProviders();
120 if (uiaHandle
!= nullptr)
121 ::FreeLibrary (uiaHandle
);
123 clearSingletonInstance();
126 //==============================================================================
127 template <typename FuncType
>
128 static FuncType
getUiaFunction (HMODULE module
, LPCSTR funcName
)
130 JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wcast-function-type")
131 return (FuncType
) GetProcAddress (module
, funcName
);
132 JUCE_END_IGNORE_WARNINGS_GCC_LIKE
135 //==============================================================================
136 using UiaReturnRawElementProviderFunc
= LRESULT (WINAPI
*) (HWND
, WPARAM
, LPARAM
, IRawElementProviderSimple
*);
137 using UiaHostProviderFromHwndFunc
= HRESULT (WINAPI
*) (HWND
, IRawElementProviderSimple
**);
138 using UiaRaiseAutomationPropertyChangedEventFunc
= HRESULT (WINAPI
*) (IRawElementProviderSimple
*, PROPERTYID
, VARIANT
, VARIANT
);
139 using UiaRaiseAutomationEventFunc
= HRESULT (WINAPI
*) (IRawElementProviderSimple
*, EVENTID
);
140 using UiaClientsAreListeningFunc
= BOOL (WINAPI
*) ();
141 using UiaDisconnectProviderFunc
= HRESULT (WINAPI
*) (IRawElementProviderSimple
*);
142 using UiaDisconnectAllProvidersFunc
= HRESULT (WINAPI
*) ();
144 HMODULE uiaHandle
= ::LoadLibraryA ("UIAutomationCore.dll");
145 UiaReturnRawElementProviderFunc uiaReturnRawElementProvider
= getUiaFunction
<UiaReturnRawElementProviderFunc
> (uiaHandle
, "UiaReturnRawElementProvider");
146 UiaHostProviderFromHwndFunc uiaHostProviderFromHwnd
= getUiaFunction
<UiaHostProviderFromHwndFunc
> (uiaHandle
, "UiaHostProviderFromHwnd");
147 UiaRaiseAutomationPropertyChangedEventFunc uiaRaiseAutomationPropertyChangedEvent
= getUiaFunction
<UiaRaiseAutomationPropertyChangedEventFunc
> (uiaHandle
, "UiaRaiseAutomationPropertyChangedEvent");
148 UiaRaiseAutomationEventFunc uiaRaiseAutomationEvent
= getUiaFunction
<UiaRaiseAutomationEventFunc
> (uiaHandle
, "UiaRaiseAutomationEvent");
149 UiaClientsAreListeningFunc uiaClientsAreListening
= getUiaFunction
<UiaClientsAreListeningFunc
> (uiaHandle
, "UiaClientsAreListening");
150 UiaDisconnectProviderFunc uiaDisconnectProvider
= getUiaFunction
<UiaDisconnectProviderFunc
> (uiaHandle
, "UiaDisconnectProvider");
151 UiaDisconnectAllProvidersFunc uiaDisconnectAllProviders
= getUiaFunction
<UiaDisconnectAllProvidersFunc
> (uiaHandle
, "UiaDisconnectAllProviders");
153 IRawElementProviderSimple
* disconnectingProvider
= nullptr;
154 bool disconnectingAllProviders
= false;
156 //==============================================================================
157 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowsUIAWrapper
)