

#pragma once
#include "ComSurrogateScanning_i.h"
#include "_IScannerEvents_CP.h"
#include "PIXDFLT.H"
#include <string>
#include <vector>

#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;


struct ISystemInfo;



class ATL_NO_VTABLE CIsisScanner :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CIsisScanner, &CLSID_IsisScanner>,
	public ISupportErrorInfo,
	public IConnectionPointContainerImpl<CIsisScanner>,
	public CProxy_IScannerEvents<CIsisScanner>,
	public IDispatchImpl<IIsisScanner, &IID_IIsisScanner, &LIBID_ComSurrogateScanningLib,  1,  0>
{
public:
	CIsisScanner();
	virtual ~CIsisScanner();

	DECLARE_REGISTRY_RESOURCEID(106)


	BEGIN_COM_MAP(CIsisScanner)
		COM_INTERFACE_ENTRY(IIsisScanner)
		COM_INTERFACE_ENTRY(IDispatch)
		COM_INTERFACE_ENTRY(ISupportErrorInfo)
		COM_INTERFACE_ENTRY(IConnectionPointContainer)
	END_COM_MAP()

	BEGIN_CONNECTION_POINT_MAP(CIsisScanner)
		CONNECTION_POINT_ENTRY(__uuidof(_IScannerEvents))
	END_CONNECTION_POINT_MAP()

	
	STDMETHOD(FinalRelease)();

	
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID) override;

	
	STDMETHOD(Init)(BSTR ScannerModel, BSTR ConfigFilename);
	STDMETHOD(ScanToFiles)(BSTR Directory) override;
	STDMETHOD(ConfigureViaGui(LONG hWnd)) override;
	STDMETHOD(ScanToByteArrays)(LONG parentHwnd) override;
	STDMETHOD(get_ConfigFilename)(BSTR * pPath) override;
	STDMETHOD(get_SystemInfo)(ISystemInfo** ppResult);
	

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	STDMETHOD(FinalConstruct());


public:
	STDMETHOD(InitByDriverName)(BSTR DriverName, BSTR ConfigFilename);

private:
	static HRESULT ScanBatchToFiles(HWND hWnd, long Orientation);
	HRESULT ScanBatchToByteArrays(long Orientation);
	
	PixDrvHandle hFileWriter;
	BSTR m_driverName;
	BSTR m_scanningDataTempLocation;
	BSTR m_configFilename;
	HRESULT ApplyConfiguration() const;
	INT32 myPixRunZone(PixDrvHandlePtr lpScan, char * buf, int size);
	BOOL CancelHit() const;
	BOOL bCancelScanning = FALSE;
	HRESULT ChkPixError(PixDrvHandle hDrv, INT32 lCode);
	HRESULT LoadScanner();
	HRESULT ModelNameToDriverName(std::wstring modelName, BSTR* pResult) const;
	HRESULT DriverDescriptionToDriverName(BSTR driverDescription, BSTR* pResult) const;
	bool SubscriberExists();
	BOOL m_scannerLoaded;
	HRESULT CreateByteArray(const char* filename, SAFEARRAY **ppResult);
	HRESULT CreateByteArray(char* bytes, size_t size, SAFEARRAY **ppResult) const;
	char* m_fileBuffer;
	DWORD m_fileBufferSize;
	char m_outputName1[MAX_PATH];
	char m_outputName2[MAX_PATH];
	ISystemInfo* m_pSystemInfo;
	HRESULT CreateDataAndFireScannedEvent(const char* filename1, const char* filename2, VARIANT_BOOL areDeskewed, VARIANT_BOOL* pCancel);
	STDMETHOD(ReleaseScanner)() override;
	HWND m_hwnd;
	
	void CreateDummyTopmostWindow();
	SAFEARRAY *m_page1 = nullptr, *m_page2 = nullptr;	
	CComAutoCriticalSection m_criticalSection;
	HHOOK m_hook = nullptr;
	void StartLookingForConfigDialog();
	void StopLookingForConfigDialog();

protected:
	PixDrvHandle hScan;
	

	
};

OBJECT_ENTRY_NON_CREATEABLE_EX_AUTO(__uuidof(IsisScanner), CIsisScanner)


