Application crash even on a try catch block

Yanis 20 Reputation points
2024-05-30T03:16:00.1533333+00:00

When i comment out the line GetViewsByZOrder i get the following exception:

Exception thrown at 0x00007FF96E36EE19 (rpcrt4.dll) in : 0xC0000005: Access violation reading location 0x00000000094F0122.

On the line if (collection->GetViewForHwnd(hwnd, &view) != S_OK)

#include <Windows.h>
#include <objectarray.h>
#include <atlbase.h>
#include <iostream>

struct __declspec(uuid("372E1D3B-38D3-42E4-A15B-8AB2B178F513")) IApplicationView : public IUnknown
{
};

struct __declspec(uuid("1841c6d7-4f9d-42c0-af41-8747538f10e5")) IApplicationViewCollection : public IUnknown
{
  public:
    virtual HRESULT STDMETHODCALLTYPE GetViews(IObjectArray **pArray) = 0;
    //virtual HRESULT STDMETHODCALLTYPE GetViewsByZOrder(IObjectArray **pArray) = 0;
    virtual HRESULT STDMETHODCALLTYPE GetViewsByAppUserModelId(BSTR pId, IObjectArray **pArray) = 0;
    virtual HRESULT STDMETHODCALLTYPE GetViewForHwnd(HWND pHwnd, IApplicationView **pView) = 0;
};

int main(int argc, char *argv[])
{
	static const CLSID CLSID_ImmersiveShell = { 0xC2F03A33, 0x21F5, 0x47FA, 0xB4, 0xBB, 0x15, 0x63, 0x62, 0xA2, 0xF2, 0x39 };

	CoInitialize(NULL);

    IServiceProvider* pServiceProvider = nullptr;
    if (CoCreateInstance(CLSID_ImmersiveShell, NULL, CLSCTX_LOCAL_SERVER, __uuidof(IServiceProvider), (PVOID*)&pServiceProvider) != S_OK)
        return 0;

    CComPtr<IApplicationViewCollection> collection = nullptr;
    if (pServiceProvider->QueryService(__uuidof(IApplicationViewCollection), &collection) != S_OK)
        return 0;

	CComPtr<IApplicationView> view;
    try
    {
        HWND hwnd  = FindWindowA(NULL, "Untitled - Notepad");
	    if (collection->GetViewForHwnd(hwnd, &view) != S_OK)  // <-- application crashes even being in the try block
		    return 0;
    }
    catch (...)
    {
        std::cout << "Exception" << std::endl;
    }
    return 0;
}

Without commenting the GetViewsByZOrder line it does work correctly.

I commented it just to ilustrate the issue, as these virtual functions can change between win builds.

I have also tried:

	CComPtr<IApplicationView> view;
    __try
    {
        HWND hwnd  = FindWindowA(NULL, "Untitled - Notepad");
	    if (collection->GetViewForHwnd(hwnd, &view) != S_OK)
		    return 0;
    }
    __except (EXCEPTION_EXECUTE_HANDLER)
    {
        std::cout << "Exception" << std::endl;
    }

But it doesnt compile, i get: error C2712: Cannot use __try in functions that require object unwinding

How i could safely read the GetViewForHwnd without getting an application crash?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,764 questions
{count} votes

Accepted answer
  1. RLWA32 45,701 Reputation points
    2024-05-30T09:54:44.89+00:00

    Refer to Handle structured exceptions in C++ for information on catching an access violation or other exception.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.