// hugaotwainds.cpp: 定义 DLL 的初始化例程。 // #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 #include #include #ifdef _DEBUG #define new DEBUG_NEW #endif typedef struct _DS_inst { TW_IDENTITY AppId; CTWAINDS_Base *pDS; }DS_inst; typedef list lstDS; lstDS g_lstDS; #ifdef TWH_CMP_MSC /** * gloadbal Windows Instance handle for the DSM DLL... */ HINSTANCE g_hinstance = 0; #endif // //TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的, // 则从此 DLL 导出的任何调入 // MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到 // 该函数的最前面。 // // 例如: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // 此处为普通函数体 // } // // 此宏先于任何 MFC 调用 // 出现在每个函数中十分重要。 这意味着 // 它必须作为以下项中的第一个语句: // 出现,甚至先于所有对象变量声明, // 这是因为它们的构造函数可能生成 MFC // DLL 调用。 // // 有关其他详细信息, // 请参阅 MFC 技术说明 33 和 58。 // // ChugaotwaindsApp BEGIN_MESSAGE_MAP(ChugaotwaindsApp, CWinApp) END_MESSAGE_MAP() // ChugaotwaindsApp 构造 ChugaotwaindsApp::ChugaotwaindsApp() { // TODO: 在此处添加构造代码, // 将所有重要的初始化放置在 InitInstance 中 } // 唯一的 ChugaotwaindsApp 对象 ChugaotwaindsApp theApp; // ChugaotwaindsApp 初始化 //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)//未找到路径 { 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); } ////////////////////////////////////////////////////////////////////////////// //退出程序 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; }