

#pragma once
#include <vector>
#include "PIXDFLT.H"

#include "ComSurrogateScanning_i.h"



#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

using namespace ATL;




class ATL_NO_VTABLE CSystemInfo :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CSystemInfo, &CLSID_SystemInfo>,
	public ISupportErrorInfo,
	public IDispatchImpl<ISystemInfo, &IID_ISystemInfo, &LIBID_ComSurrogateScanningLib,  1,  0>
{
public:
	CSystemInfo()
		: m_pRegMonitor(nullptr)
	{
	}

	virtual ~CSystemInfo()
	{
	}

	DECLARE_CLASSFACTORY_SINGLETON(CSystemInfo)
	DECLARE_REGISTRY_RESOURCEID(109)


	BEGIN_COM_MAP(CSystemInfo)
		COM_INTERFACE_ENTRY(ISystemInfo)
		COM_INTERFACE_ENTRY(IDispatch)
		COM_INTERFACE_ENTRY(ISupportErrorInfo)
	END_COM_MAP()

	
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID) override;

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct() 
	{
		InitDriverList();
		InitScannerList();
		StartMonitoring();
		return S_OK;
	}

	void FinalRelease()
	{
		for each (auto inf in m_driverList)
		{
			delete inf;
		}
		for each (auto inf in m_scannerList)
		{
			delete inf;
		}
		if (m_pRegMonitor)
		{
			PixConnectedScannerMonitorStop(&m_pRegMonitor);
			m_pRegMonitor = nullptr;
		}
	}

	STDMETHOD(GetInstalledScanners)(SAFEARRAY** ppResult) override;
	STDMETHOD(GetConnectedScanners)(SAFEARRAY** ppResult) override;
	bool IsScannerPresent(BSTR ScannerName) const;
	friend void _stdcall OnConnectedScannerListChanged(void FAR* pvCustomParam);	
	STDMETHOD(DriverDescriptionToDriverName)(BSTR driverDescription, BSTR* pResult) override;

private:
	void InitDriverList();
	void InitScannerList();
	std::vector<PIXDRIVERINFO*> m_driverList;
	std::vector<PIXDRIVERINFO*> m_scannerList;
	void StartMonitoring();
	PIXCONNECTEDSCANNER m_pRegMonitor;


};

OBJECT_ENTRY_NON_CREATEABLE_EX_AUTO(__uuidof(SystemInfo), CSystemInfo)

