twain2/hugaotwainds/hugaotwainds.cpp

241 lines
11 KiB
C++
Raw Permalink Normal View History

2020-03-11 02:53:30 +00:00
<EFBFBD><EFBFBD>// hugaotwainds.cpp: <EFBFBD>[IN DLL <EFBFBD>vR<EFBFBD>YS<EFBFBD>O z0
//
#include "stdafx.h"
#include "hugaotwainds.h"
#include "afxshellmanager.h"
#include "afxvisualmanager.h"
#include "afxvisualmanagerwindows.h"
#include "TwainUIDlg.h"
#include "PublicFunc.h"
//#include "UI_INI.h"
#include "JsonConfig.h"
#include <memory>
#include <io.h>
#include <list>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
typedef struct _DS_inst
{
TW_IDENTITY AppId;
CTWAINDS_Base *pDS;
}DS_inst;
typedef list<DS_inst> lstDS;
lstDS g_lstDS;
#ifdef TWH_CMP_MSC
/**
* gloadbal Windows Instance handle for the DSM DLL...
*/
HINSTANCE g_hinstance = 0;
#endif
//
//TODO: <EFBFBD>Y<EFBFBD>gdk DLL <EFBFBD>v<EFBFBD>[<EFBFBD>N MFC DLL /f<EFBFBD>R`<EFBFBD><EFBFBD><EFBFBD>c<EFBFBD>v <EFBFBD>
// R<EFBFBD>Ndk DLL <EFBFBD>[<EFBFBD>Q<EFBFBD>v<EFBFBD>NUO<EFBFBD>eQ
// MFC <EFBFBD>v<EFBFBD>Qpe<EFBFBD>_{<EFBFBD>\ AFX_MANAGE_STATE <EFBFBD>[<EFBFBD>m<EFBFBD>R0R
// <EFBFBD><EFBFBD><EFBFBD>Qpe<EFBFBD>vgMRb<EFBFBD>0
//
// <EFBFBD>O<EFBFBD>Y:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // dkY:Nnf<EFBFBD><EFBFBD>QpeSO
// }
//
// dk<EFBFBD>[HQ<EFBFBD>N<EFBFBD>NUO MFC <EFBFBD>(u
// <EFBFBD>Q<EFBFBD>s(W<EFBFBD>k*N<EFBFBD>Qpe-NAS<EFBFBD><EFBFBD>0 ُasT@w
// <EFBFBD>[<EFBFBD>_{<EFBFBD>\O:N<EFBFBD>N Ny<EFBFBD>-N<EFBFBD>v,{N*N<EFBFBD><EFBFBD><EFBFBD>S:
// <EFBFBD>Q<EFBFBD>s <EFBFBD>u<EFBFBD><EFBFBD>HQ<EFBFBD>N@b g<EFBFBD>[a<EFBFBD><EFBFBD><EFBFBD>Xf <EFBFBD>
// ُ/f<EFBFBD>V:N<EFBFBD>[<EFBFBD>N<EFBFBD>v<EFBFBD>g <EFBFBD><EFBFBD>Qpe<EFBFBD>S<EFBFBD><EFBFBD>ub MFC
// DLL <EFBFBD>(u0
//
// gsQvQ<EFBFBD>N<EFBFBD><EFBFBD><EFBFBD>~<EFBFBD>Oo` <EFBFBD>
// <EFBFBD><EFBFBD><EFBFBD>S<EFBFBD> MFC <EFBFBD>b/g<EFBFBD><EFBFBD>f 33 <EFBFBD>T 580
//
// ChugaotwaindsApp
BEGIN_MESSAGE_MAP(ChugaotwaindsApp, CWinApp)
END_MESSAGE_MAP()
// ChugaotwaindsApp <EFBFBD>g <EFBFBD>
ChugaotwaindsApp::ChugaotwaindsApp()
{
// TODO: (WdkY<EFBFBD>m<EFBFBD>R<EFBFBD>g <EFBFBD><EFBFBD>Nx <EFBFBD>
// \@b <EFBFBD><EFBFBD><EFBFBD>vR<EFBFBD>YS>en(W InitInstance -N
}
// /UN<EFBFBD>v ChugaotwaindsApp <EFBFBD>[a<EFBFBD>
ChugaotwaindsApp theApp;
// ChugaotwaindsApp R<EFBFBD>YS
//CUI_INI* m_pUI_INI;
BOOL ChugaotwaindsApp::InitInstance()
{
CWinApp::InitInstance();
TCHAR szIniFile[MAX_PATH] = { 0 };
TCHAR szIniData[MAX_PATH]={0};
//if (m_pUI_INI==NULL)
{
DWORD dwRet = 0x0L;
SHGetSpecialFolderPath(NULL, szIniFile, CSIDL_LOCAL_APPDATA, TRUE);//CSIDL_COMMON_DOCUMENTS
_tcscat(szIniFile, HUAGAO_SCAN);
strcpy(szIniData,szIniFile);
BOOL rlt=access(szIniFile, 0);
if (rlt!=0)//*g~b0R_
{
if (!CreateDirectory(szIniFile,NULL))
{
return FALSE;
}
_tcscat(szIniData,TWAIN_DATA_PATH);
if (!CreateDirectory(szIniData,NULL))
{
return FALSE;
}
_tcscat(szIniFile,TWAIN_INIPATH);
if (CreateDirectory(szIniFile,NULL))
{
_tcscat(szIniFile, TEXT("\\"));
_tcscat(szIniFile, TWAIN_JSON_NAME);
if (rlt!=1)
{
JsonConfig js;
CONFIGPARAMS cfp=js.GetDefaultConfigParams();
std::string path(szIniFile);
js.WriteToJson(&cfp,path,false);
}
}
else
{
return FALSE;
}
}
}
return TRUE;
}
BOOL ChugaotwaindsApp::PreTranslateMessage(MSG * pMsg)
{
return CWinApp::PreTranslateMessage(pMsg);
}
//////////////////////////////////////////////////////////////////////////////
//<EFBFBD><EFBFBD>Q z<EFBFBD>^
int ChugaotwaindsApp::ExitInstance()
{
return CWinApp::ExitInstance();
}
//////////////////////////////////////////////////////////////////////////////
// This is the main entry point. This function is dlsym'd by the DSM.
#ifdef TWH_CMP_MSC
TW_UINT16 FAR PASCAL
#else
FAR PASCAL TW_UINT16
#endif
DS_Entry(pTW_IDENTITY _pOrigin,
TW_UINT32 _DG,
TW_UINT16 _DAT,
TW_UINT16 _MSG,
TW_MEMREF _pData)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CTWAINDS_Base* pTWAINLayer = 0;
if (_pOrigin)
{
lstDS::iterator llIter = g_lstDS.begin();
for (; llIter != g_lstDS.end(); llIter++)
{
if ((*llIter).AppId.Id == _pOrigin->Id)
{
pTWAINLayer = (*llIter).pDS;
}
}
}
// Curently we are not open
if (0 == pTWAINLayer)
{
// Special case DSM can request to get identity information about
// DS before it is open. In this special case, where the DS is not
// open, we return this static Idenity.
if (DG_CONTROL == _DG && DAT_IDENTITY == _DAT && MSG_GET == _MSG)
{
// Copy the ID assigned by the DSM eventhough the spec states
// that the id will not be assigned until MSG_OPENDS
CTWAINDS_Base::m_TheIdentity.Id = ((pTW_IDENTITY)_pData)->Id;
memcpy(_pData, &CTWAINDS_Base::m_TheIdentity, sizeof(CTWAINDS_Base::m_TheIdentity));
return TWRC_SUCCESS;
}
// The DS is not open. If we get a request to close DS do not open
// just to close, instead return that it is success closed.
if (DG_CONTROL == _DG && DAT_IDENTITY == _DAT && MSG_CLOSEDS == _MSG)
{
return TWRC_SUCCESS;
}
// Open the DS
pTWAINLayer = new CTWAINDS_FreeImage(*_pOrigin);
if (NULL == pTWAINLayer
|| TWRC_SUCCESS != pTWAINLayer->Initialize())
{
// Failed to create the DS
//setConditionCode(TWCC_LOWMEMORY);
if (pTWAINLayer)
{
// Created but could not Initialize
delete pTWAINLayer;
}
return TWRC_FAILURE;
}
DS_inst _DS;
_DS.pDS = pTWAINLayer;
_DS.AppId = *_pOrigin;
g_lstDS.push_back(_DS);
}
// If we were not open before, we are now, so continue with the TWAIN call
TW_INT16 result = pTWAINLayer->DS_Entry(_pOrigin, _DG, _DAT, _MSG, _pData);
/**
* Special case - free memory if closing DS
* @todo keep track of what apps are connecting to the ds and only
* delete when count goes down to 0
*/
if (TWRC_SUCCESS == result &&
DG_CONTROL == _DG && DAT_IDENTITY == _DAT && MSG_CLOSEDS == _MSG &&
NULL != pTWAINLayer)
{
lstDS::iterator llIter = g_lstDS.begin();
for (; llIter != g_lstDS.end();)
{
if ((*llIter).AppId.Id == _pOrigin->Id)
{
delete (*llIter).pDS;
llIter = g_lstDS.erase(llIter);
continue;
}
llIter++;
}
}
return result;
}