AppInstance.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:7k
- #include "stdafx.h"
- #include "trfAgent.h"
- #include "ConfigEnv.h"
- #include "AppInstance.h"
- #include "..publicHdHelper.h"
- #include "..publicShortCut.h"
- CAppInstance::CAppInstance()
- {
- this->m_hAppMutex = NULL;
- this->m_binadminmode = false;
- memset(&m_authorinfo, 0x0, sizeof(AUTHORINFO));
- memset(m_sModuleFile, 0x0, sizeof(m_sModuleFile));
- ::GetModuleFileName(NULL, m_sModuleFile, MAX_PATH);
- }
- CAppInstance::~CAppInstance()
- {
- //this->UnInitApp();
- }
- BOOL CAppInstance::Exists()
- {
- if(this->m_hAppMutex) return TRUE;
- this->m_hAppMutex = CreateMutex(NULL, FALSE, _T("843F00BA81D649649D529594B27D8760"));
-
- if(this->m_hAppMutex)
- {
- if(ERROR_ALREADY_EXISTS == GetLastError())
- {
- this->m_hAppMutex = NULL;
- return TRUE;
- }
- }
- return FALSE;
- }
- //initializing system environment.
- //for example : creating system directory,
- // extracting system files from applicaton's resource and so on.
- BOOL CAppInstance::InitApp(void)
- {
- //get the client hard disk id information.
- memset(m_sHDID, 0x0, sizeof(m_sHDID));
- if(!GetAgentSerialNo(m_sHDID, sizeof(m_sHDID)))
- {
- return FALSE;
- }
- // check the shell "send-to" link whether or not exists.
- // if link not exists then create it.
- CShortcut ssc;
- CString shortcutname;
- shortcutname.Format("%s %s", SYS_APPANEM, SYS_VERSION_NO);
- if(!ssc.isLinkAvailable(shortcutname, CSIDL_SENDTO))
- {
- ssc.CreateShortCut("_this", shortcutname, CSIDL_SENDTO,
- _LoadString(IDS_SHLSENDTODESC), m_sModuleFile, 0);
- }
- // initialize the windows socket2 library
- if(!::Initialize())
- {
- MSGBOX_ERROR(_LoadString(IDS_INITSOCKLIBFAILED).GetBuffer(0));
- return FALSE;
- }
-
- // check the system whether or not installed ?
- CRegister regkey;
- if(!regkey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY)) return FALSE;
- BOOL binited = regkey.Read_Bool_Value(REG_BSYSINITIALIZED);
- regkey.Close();
- if(!binited && !this->SysFirstInit())
- {
- MSGBOX_ERROR(_LoadString(IDS_FIRSTINITSYSFAIL).GetBuffer(0));
- return FALSE;
- }
- // initialize the system ado connection.
- char directory[_MAX_PATH], adoconnstr[MAX_SQL_SIZE];
- _snprintf(directory, _MAX_PATH-1, "%s%s", ENV_SYSROOTDIR, ENV_SYSDATAFLEDIR);
-
- //_snprintf(adoconnstr, MAX_SQL_SIZE-1, "Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=%s;Mode=Share Deny None;Extended Properties=Paradox 7.x;", directory);
- _snprintf(adoconnstr, MAX_SQL_SIZE-1, "Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=%s\fMail.mdb;Persist Security Info=True;", directory);
-
- if(!this->m_adoconn.Open(adoconnstr))
- {
- MSGBOX_ERROR(_LoadString(IDS_INITDBCONNFAIL).GetBuffer(0));
- return FALSE;
- }
- return TRUE;
- }
- void CAppInstance::UnInitApp(void)
- {
- //close the ado db connection.
- if(this->m_adoconn.IsConnect())
- {
- this->m_adoconn.Close();
- }
- //uninitialize the windows socket2 library
- ::UnInitialize();
- if(NULL != this->m_hAppMutex)
- {
- CloseHandle(this->m_hAppMutex);
- }
- }
- BOOL CAppInstance::SysFirstInit(void)
- {
- CRegister regkey;
- //0---initializing system parameters.
- if(MSGBOX_CONFIRM(_LoadString(IDS_SYSINITCONFIRM)) == IDYES)
- {
- CConfigEnv sheet_SysConfig(_LoadString(IDS_SYSCONFIG));
- sheet_SysConfig.DoModal();
- }
- //1---create system directories.
- char directory[_MAX_PATH];
- _snprintf(directory, _MAX_PATH-1, "%s%s", ENV_SYSROOTDIR, ENV_DEFAULT_RECVFLEDIR);
-
- if(!DirectoryExists(directory))
- {
- if(false == CreateDirectorys(directory)) return FALSE;
- }
-
- _snprintf(directory, _MAX_PATH-1, "%s%s", ENV_SYSROOTDIR, ENV_SYSDATAFLEDIR);
- if(!DirectoryExists(directory))
- {
- if(false == CreateDirectorys(directory)) return FALSE;
- }
-
- //2---export databse table files from application's resource.
- HRSRC hrsrc = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDF_DBFILES), "DBFiles");
- if(NULL == hrsrc) return FALSE;
-
- DWORD dwsize = SizeofResource(GetModuleHandle(NULL), hrsrc), writen = 0;
-
- HGLOBAL hglobal = LoadResource(GetModuleHandle(NULL), hrsrc);
- if(NULL == hglobal) return FALSE;
-
- LPVOID pres = LockResource(hglobal);
- if(NULL == pres) return FALSE;
-
- char datafile[_MAX_PATH];
- _snprintf(datafile, _MAX_PATH-1, "%s\dbfiles.dat", directory);
- HANDLE hresfile = CreateFile(datafile, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
- NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-
- if(INVALID_HANDLE_VALUE == hresfile) return FALSE;
-
- WriteFile(hresfile, pres, dwsize, &writen, NULL);
- CloseHandle(hresfile);
-
- //3---extracting databse table files.
- if(FALSE == ExtractAllFileFromAG(datafile, directory)) return FALSE;
-
- DeleteFile(datafile);
-
- if(!regkey.Open(HKEY_CURRENT_USER, REG_SSYSSETTINGKEY)) return FALSE;
- regkey.Write_Bool_Value(REG_BSYSINITIALIZED, TRUE);
-
- return TRUE;
- }
- BOOL CAppInstance::ExtractAllFileFromAG(LPCSTR lpcsSrcFilename, LPCSTR lpcsDestDir)
- {
- try{
- //@1 --- 打开源文件
- HANDLE hAGLibFile = ::CreateFile((char*)lpcsSrcFilename, GENERIC_READ, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
-
- if (hAGLibFile == INVALID_HANDLE_VALUE) return FALSE;
-
- //@2 --- 读取源文件的真实大小和附加文件个数.
- DWORD readed = 0, writen = 0;
-
- char filename[_szFilename+1],
- flesize[_szFilesize+1],
- //
- agrelsize[_szFilesize+1],
- extraflenum[_szExtraFileCount+1];
-
- //@2.1
- int offset = _szFilesize;
-
- memset((char*)agrelsize, 0x0, _szFilesize+1);
- SetFilePointer(hAGLibFile, -offset, NULL, FILE_END);
- ReadFile(hAGLibFile, (char*)agrelsize, _szFilesize, &readed, NULL);
-
- //@2.2
- offset += _szExtraFileCount;
- memset((char*)extraflenum, 0x0, _szExtraFileCount+1);
- SetFilePointer(hAGLibFile, -offset, NULL, FILE_END);
- ReadFile(hAGLibFile, (char*)extraflenum, _szExtraFileCount, &readed, NULL);
-
- //@3 --- 定位到第一个用户文件开始处,并依次检查是否是指定的文件
- // 如果是指定的文件则将文件内容写入指定的目标文件中.
- SetFilePointer(hAGLibFile, atoi((char*)agrelsize), NULL, FILE_BEGIN);
-
- int nextraflenum = atoi((char*)extraflenum);
- for(int i = 0; i < nextraflenum; ++i)
- {
- memset((char*)filename, 0x0, sizeof(filename));
-
- ReadFile(hAGLibFile, (char*)filename, _szFilename, &readed, NULL);
- ReadFile(hAGLibFile, (char*)flesize, _szFilesize, &readed, NULL);
-
- //@4.1
- char dffn[_MAX_PATH];
- _snprintf(dffn, _MAX_PATH-1, "%s\%s", lpcsDestDir, (char*)filename);
- HANDLE hDestFile = CreateFile((char*)dffn,
- GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,
- NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-
- if(INVALID_HANDLE_VALUE == hDestFile)
- {
- CloseHandle(hAGLibFile);
- return FALSE;
- }
-
- //@4.2 --- 写目标文件内容.
- int leftsize = atoi((char*)flesize), maxsize = 0;
-
- char lpBuf[MASS_BLOCK_SIZE];
-
- while(leftsize > 0)
- {
- memset(lpBuf, 0x0, sizeof(lpBuf));
-
- maxsize = (leftsize > MASS_BLOCK_SIZE ? MASS_BLOCK_SIZE : leftsize);
-
- ReadFile(hAGLibFile, lpBuf, maxsize, &readed, NULL);
- WriteFile(hDestFile, lpBuf, readed, &writen, NULL);
-
- leftsize -= readed;
- }
- CloseHandle(hDestFile);
- //break;
- }
- //@over
- CloseHandle(hAGLibFile);
-
- return TRUE;
- }
- catch (...) {
- return FALSE;
- }
- }