123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629 |
- #include "stdafx.h"
- #include <tlhelp32.h>
- #include "SysLib.h"
- #include <io.h>
- #include <odbcinst.h>
- #include <afxdb.h>
- #include "resource.h"
- #include "SpreadSheet.h"
- #include <stdio.h>
- #include <windows.h>
- #include <sql.h>
- #include <sqlext.h>
- //增加自动启动注册表项
- INT AddAutoSysRun(CHAR *strExeFile, INT iAutoRunSystem)
- {
- HKEY hKey = NULL;
- DWORD dwErrorCode = 0;
- //创建注册表,有该键则读取,无则创建
- dwErrorCode = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
- _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
- 0,
- NULL,
- REG_OPTION_NON_VOLATILE,
- KEY_ALL_ACCESS,
- NULL, //Security
- &hKey,
- NULL);
- if (dwErrorCode == ERROR_SUCCESS)
- {
- if (iAutoRunSystem)
- {
- dwErrorCode = RegSetValueEx(hKey, _T("lyfzAttendance"), NULL, REG_SZ, (BYTE*)strExeFile, strlen(strExeFile));
- }
- else
- {
- dwErrorCode = RegDeleteValue(hKey, _T("lyfzAttendance"));
- dwErrorCode = RegDeleteKey(hKey, _T("lyfzAttendance"));
- }
- }
- if (NULL != hKey)
- RegCloseKey(hKey);
- hKey = NULL;
- return 0;
- }
- //-------------------------------------------------------------------------------------
- //Description:
- // This function maps a character string to a wide-character (Unicode) string
- //
- //Parameters:
- // lpcszStr: [in] Pointer to the character string to be converted
- // lpwszStr: [out] Pointer to a buffer that receives the translated string.
- // dwSize: [in] Size of the buffer
- //
- //Return Values:
- // TRUE: Succeed
- // FALSE: Failed
- //
- //Example:
- // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
- //---------------------------------------------------------------------------------------
- BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
- {
- // Get the required size of the buffer that receives the Unicode
- // string.
- DWORD dwMinSize;
- dwMinSize = MultiByteToWideChar(CP_ACP, 0, lpcszStr, -1, NULL, 0);
- if (dwSize < dwMinSize)
- {
- return FALSE;
- }
- // Convert headers from ASCII to Unicode.
- MultiByteToWideChar(CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);
- return TRUE;
- }
- //-------------------------------------------------------------------------------------
- //Description:
- // This function maps a wide-character string to a new character string
- //
- //Parameters:
- // lpcwszStr: [in] Pointer to the character string to be converted
- // lpszStr: [out] Pointer to a buffer that receives the translated string.
- // dwSize: [in] Size of the buffer
- //
- //Return Values:
- // TRUE: Succeed
- // FALSE: Failed
- //
- //Example:
- // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
- //---------------------------------------------------------------------------------------
- BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
- {
- DWORD dwMinSize;
- dwMinSize = WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, NULL, 0, NULL, FALSE);
- if (dwSize < dwMinSize)
- {
- return FALSE;
- }
- WideCharToMultiByte(CP_OEMCP, NULL, lpcwszStr, -1, lpszStr, dwSize, NULL, FALSE);
- return TRUE;
- }
- //////////////////////////////////////////////////////////////////////////////
- //名称:GetExcelDriver
- //功能:获取ODBC中Excel驱动
- //作者:徐景周(jingzhou_xu@163.net)
- //组织:未来工作室(Future Studio)
- //日期:2002.9.1
- /////////////////////////////////////////////////////////////////////////////
- int GetExcelDriver(CString& sDriver)
- {
- WCHAR wszBuf[1024] = {0};
- WORD cbBufMax = 2000;
- WORD cbBufOut;
- // 获取已安装驱动的名称(涵数在odbcinst.h里)
- if (!SQLGetInstalledDrivers(wszBuf, cbBufMax, &cbBufOut))
- {
- ::AfxMessageBox(_T("Failed!"));
- return 0;
- }
- // 检索已安装的驱动是否有Excel...
- int nlen = 0;
- int npos = 0;
- char szBuf[512] = {0};
- WCHAR* pBuf = wszBuf;
- CString strMsg;
- while(1)
- {
- memset(szBuf, 0, 512);
- nlen = WChar2Char(szBuf, pBuf + npos);
- if(nlen == 0)
- break;
- npos += (nlen + 1);
- printf("发现 %s\n", szBuf);
- if(strcmp(szBuf, "Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)") == 0
- || strcmp(szBuf, "Microsoft Excel-Treiber (*.xls)") == 0
- || strcmp(szBuf, "Microsoft Excel Driver (*.xls)") == 0
- || strcmp(szBuf, "Driver do Microsoft Excel (*.xls)") == 0)
- {
- WCHAR wsztmp[512] = {0};
- Char2WChar(wsztmp, szBuf);
- sDriver = wsztmp;
- break;
- }
- }
- return 1;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // BOOL MakeSurePathExists( CString &Path,bool FilenameIncluded)
- // 参数:
- // Path 路径
- // FilenameIncluded 路径是否包含文件名
- // 返回值:
- // 文件是否存在
- // 说明:
- // 判断Path文件(FilenameIncluded=true)是否存在,存在返回TURE,不存在返回FALSE
- // 自动创建目录
- //
- ///////////////////////////////////////////////////////////////////////////////
- BOOL MakeSurePathExists(CString &Path, bool FilenameIncluded)
- {
- int Pos = 0;
- while ((Pos = Path.Find(_T('\\'), Pos + 1)) != -1)
- CreateDirectory(Path.Left(Pos), NULL);
- if (!FilenameIncluded)
- CreateDirectory(Path, NULL);
- // return ((!FilenameIncluded)?!_access(Path,0):
- // !_access(Path.Left(Path.ReverseFind('\\')),0));
- char szPath[MAX_PATH+1] = {0};
- WChar2Char(szPath, Path);
- return !_access(szPath, 0);
- }
- //获得默认的文件名
- BOOL GetDefaultXlsFileName(CString& sExcelFile)
- {
- ///默认文件名:yyyymmddhhmmss.xls
- CString timeStr;
- CTime day;
- day = CTime::GetCurrentTime();
- int filenameday, filenamemonth, filenameyear, filehour, filemin, filesec;
- filenameday = day.GetDay();//dd
- filenamemonth = day.GetMonth();//mm月份
- filenameyear = day.GetYear();//yyyy
- filehour = day.GetHour();//hh
- filemin = day.GetMinute();//mm分钟
- filesec = day.GetSecond();//ss
- timeStr.Format(_T("%04d%02d%02d%02d%02d%02d"), filenameyear, filenamemonth, filenameday, filehour, filemin, filesec);
- sExcelFile = timeStr + _T(".xls");
- // prompt the user (with all document templates)
- CFileDialog dlgFile(FALSE, _T(".xls"), sExcelFile);
- CString title;
- CString strFilter;
- title = _T("指纹机数据");
- strFilter = _T("Excel文件(*.xls)");
- strFilter += (TCHAR)'\0'; // next string please
- strFilter += _T("*.xls");
- strFilter += (TCHAR)'\0'; // last string
- dlgFile.m_ofn.nMaxCustFilter++;
- dlgFile.m_ofn.nFilterIndex = 1;
- // append the "*.*" all files filter
- CString allFilter;
- VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
- strFilter += allFilter;
- strFilter += (TCHAR)'\0'; // next string please
- strFilter += _T("*.*");
- strFilter += (TCHAR)'\0'; // last string
- dlgFile.m_ofn.nMaxCustFilter++;
- dlgFile.m_ofn.lpstrFilter = strFilter;
- dlgFile.m_ofn.lpstrTitle = title;
- if (dlgFile.DoModal() == IDCANCEL)
- return FALSE; // open cancelled
- CString sn = dlgFile.GetPathName();
- sExcelFile = sn;
- if (MakeSurePathExists(sExcelFile, true))
- {
- if (!DeleteFile(sExcelFile))
- { // delete the file
- MessageBox(NULL, _T("覆盖文件时出错!"), _T("提示"), MB_ICONINFORMATION);
- return FALSE;
- }
- }
- return TRUE;
- }
- CString InterceptString(int qlen, CString strSource)
- {
- int len, i, y;
- CString sTemp, sreturn;
- strSource.TrimLeft();
- strSource.TrimRight();
- len = strSource.GetLength();
- y = 0;
- sTemp = strSource.Right(len - qlen);
- for (i = 0; i < len; i++)
- {
- //if(sTemp[y]<0 || sTemp[y]>255)
- //if(sTemp[i]<0 || sTemp[i]>=128)
- //if(sTemp.GetAt(y) & 0x80 )
- if (::IsDBCSLeadByte(sTemp.GetAt(y)))
- y = y + 2;
- else
- y = y + 1;
- if (y >= 70)
- break;
- }
- sreturn = sTemp.Left(y);
- return sreturn;
- }
- //显示选择文件夹窗口
- //参数 sFolderPath:用于返回用户选择的文件夹的路径
- //参数 sTitle:用于指定选择文件夹窗口的标题
- //返回值 :操作结果,用户取消选择或操作失败返回FALSE,否则TRUE
- BOOL BrowseForFolder(CString & sFolderPath, CString sTitle)
- {
- BROWSEINFO bi;
- WCHAR Buffer[_MAX_PATH];
- bi.hwndOwner = NULL;
- bi.pidlRoot = NULL;
- bi.pszDisplayName = Buffer;
- bi.lpszTitle = sTitle;
- bi.ulFlags = 0;
- bi.lpfn = NULL;
- LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);
- if (!pIDList)
- return FALSE;
- SHGetPathFromIDList(pIDList, Buffer);
- sFolderPath = Buffer;
- LPMALLOC lpMalloc;
- if (FAILED(SHGetMalloc(&lpMalloc)))
- return FALSE;
- //释放内存
- lpMalloc->Free(pIDList);
- lpMalloc->Release();
- return TRUE;
- }
- ///////////////////////////////////////////////////////////////////////////////
- // void ExportListToExcel(CListCtrl* pList, CString strTitle)
- // 参数:
- // pList 需要导出的List控件指针
- // strTitle 导出的数据表标题
- // 说明:
- // 导出CListCtrl控件的全部数据到Excel文件。Excel文件名由用户通过“另存为”
- // 对话框输入指定。创建名为strTitle的工作表,将List控件内的所有数据(包括
- // 列名和数据项)以文本的形式保存到Excel工作表中。保持行列关系。
- //
- // edit by [r]@dotlive.cnblogs.com
- ///////////////////////////////////////////////////////////////////////////////
- CString ExportListToExcel(CListCtrl* pList, CString strTitle)
- {
- CString warningStr;
- CString sExcelFile;
- if (pList->GetItemCount() > 0)
- {
- CDatabase database;
- CString sDriver;
- CString sSql;
- CString tableName = strTitle;
- // 检索是否安装有Excel驱动 "Microsoft Excel Driver (*.xls)"
- GetExcelDriver(sDriver);
- if (sDriver.IsEmpty())
- {
- // 没有发现Excel驱动
- AfxMessageBox(_T("没有安装Excel!"));
- return _T("");
- }
- ///默认文件名
- if (!GetDefaultXlsFileName(sExcelFile))
- {
- return _T("");
- }
- // 创建进行存取的字符串
- sSql.Format(_T("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s"), sDriver, sExcelFile, sExcelFile);
-
- // 创建数据库 (既Excel表格文件)
- if (database.OpenEx(sSql, CDatabase::noOdbcDialog))
- {
- // 创建表结构
- int i;
- LVCOLUMN columnData;
- CString columnName;
- int columnNum = 0;
- CString strH;
- CString strV;
- sSql = _T("");
- strH = _T("");
- columnData.mask = LVCF_TEXT;
- columnData.cchTextMax = 100;
- columnData.pszText = columnName.GetBuffer(100);
- for (i = 0; pList->GetColumn(i, &columnData); i++)
- {
- if (i != 0)
- {
- sSql = sSql + _T(", ");
- strH = strH + _T(", ");
- }
- sSql = sSql + _T(" ") + columnData.pszText + _T(" TEXT");
- strH = strH + _T(" ") + columnData.pszText + _T(" ");
- }
- columnName.ReleaseBuffer();
- columnNum = i;
- sSql = _T("CREATE TABLE ") + tableName + _T(" ( ") + sSql + _T(" ) ");
- database.ExecuteSQL(sSql);
- // 插入数据项
- int nItemIndex;
- for (nItemIndex = 0; nItemIndex < pList->GetItemCount(); nItemIndex++){
- strV = "";
- for (i = 0; i < columnNum; i++)
- {
- if (i != 0)
- {
- strV = strV + _T(", ");
- }
- strV = strV + _T(" '") + pList->GetItemText(nItemIndex, i) + _T("' ");
- }
- sSql = _T("INSERT INTO ") + tableName
- + _T(" (") + strH + _T(")")
- + _T(" VALUES(") + strV + _T(")");
- database.ExecuteSQL(sSql);
- }
- }
- // 关闭数据库
- database.Close();
- warningStr.Format(_T("Excel文件成功保存至%s"), sExcelFile);
- AfxMessageBox(warningStr);
- return sExcelFile;
- }
- return _T("");
- }
- void ReadExcelToList(CListCtrl* pList, CString strTitle, DWORD &dwListCount)
- {
- //strTitle+=".xls";
- CStringArray Rows, Column;
- CSpreadSheet varSheet(strTitle, _T("LoginData"));
- //pList->DeleteAllItems();//首先清空listview
- DWORD oldListCount = dwListCount;
- for (int i = 0; i < varSheet.GetTotalRows() - 1; i++)
- {
- // 读取一行
- varSheet.ReadRow(Rows, i + 2);
- CString strContents = _T("");
- pList->InsertItem(i + oldListCount, strContents);
- for (int j = 0; j < Rows.GetSize(); j++)
- {
- strContents = Rows.GetAt(j);
- pList->SetItemText(i + oldListCount, j, strContents);
- }
- dwListCount++;
- }
- }
- void TimeTicksToInt(CString strSrc, unsigned long &nDest)
- {
- CString strLeft;
- int nIndex = strSrc.Find(_T(":"));
- if (nIndex != -1)
- {
- strLeft = strSrc.Left(nIndex - 1);
- char szLeft[64] = {0};
- WChar2Char(szLeft, strLeft);
- nDest = atoi(szLeft);
- }
- }
- ///目录是否存在的检查:
- bool CheckFolderExist(CString strPath)
- {
- WIN32_FIND_DATA wfd;
- bool rValue = false;
- HANDLE hFind = FindFirstFile((LPCTSTR)strPath, &wfd);
- if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
- {
- rValue = true;
- }
- FindClose(hFind);
- return rValue;
- }
- BOOL BrowseFile(CString strFile, CString strFilter,
- CStringList *pFileList,
- CStringList *pFileNameList,
- CStringList *pShowNameList) //查找文件函数
- {
- CFileFind FileFind;
- CString SearchTarget = strFile; // + "\\" +strFilter;
- if (SearchTarget.Right(1) != "\\")
- SearchTarget += "\\";
- SearchTarget += "*.*";
- BOOL res = FileFind.FindFile(SearchTarget);
- while (res)
- {
- res = FileFind.FindNextFile();
- if (FileFind.IsDirectory() && !FileFind.IsDots())
- {
- //如果是一个子目录,用递归继续往深一层找
- CString strPath = FileFind.GetFilePath();
- CString strTitle = FileFind.GetFileTitle();
- BrowseFile(strPath, strFilter, pFileList, pFileNameList, pShowNameList);
- }
- else if (!FileFind.IsDirectory() && !FileFind.IsDots())
- {
- //显示当前访问的文件
- CString strExtName;
- CString strName;
- strName = FileFind.GetFileName();
- strExtName = strName.Right(strName.GetLength() - strName.Find('.') - 1);
- strExtName.MakeUpper();
- if (!strExtName.CompareNoCase(strFilter))//扩展名正确
- {
- CString strPath = FileFind.GetFilePath();
- pFileList->AddTail(strPath);
- pFileNameList->AddTail(strName);
- strName = strName.Left(strName.Find('.'));
- pShowNameList->AddTail(strName);
- }
- }
- }
- FileFind.Close();//关闭
- return TRUE;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////
- ////// 函数名称:GetTokens ///////
- ////// 函数说明:根据sDelimiter变量的拆分规则,把sSource源字符串拆分成多个 ///////
- ////// 字符串,并传入pTokens变量 ///////
- ////// 参数列表:pTokens -- 目标字符串存放指针 ///////
- ////// sSource -- 源字符串 ///////
- ////// sDelimiter -- 指定的拆分字符 ///////
- ////// 作 者:sailing ///////
- ////// 创者时间:2008-11-24 ///////
- ///////////////////////////////////////////////////////////////////////////////////////////////
- void GetTokens(CStringArray * pTokens, CString sSource, CString sDelimiter)
- {
- pTokens->RemoveAll();
- //开始解析
- int nStart = 0;
- int nLen = sDelimiter.GetLength();
- while (true)
- {
- int i = sSource.Find(sDelimiter, nStart);
- if (i < 0)
- break;
- if (i >= nStart)
- {
- CString s = sSource.Mid(nStart, i - nStart);
- pTokens->Add(s);
- }
- nStart = i + nLen;
- }
- //剩下的字符串
- if (nStart <= sSource.GetLength())
- pTokens->Add(sSource.Mid(nStart));
- }
- int SplitStr(CHAR *strSource, CHAR cChar, int iItem, CHAR *strGet)
- {
- int iIndex = 0;
- int iIndexThis = -1;
- int iIndexNext = -1;
- int iLength = 0;
- int iItemThis = 0;
- CHAR strFileName[MAX_FILE_LENGTH + 1] = "";
- iLength = strlen(strSource);
- while (iIndex < iLength)
- {
- if (strSource[iIndex] == cChar)
- {
- iItemThis++;
- if (iItemThis == iItem)
- iIndexThis = iIndex;
- else if (iItemThis == iItem + 1)
- {
- iIndexNext = iIndex;
- break;
- }
- }
- iIndex++;
- }
- if (iIndexNext > 0 && iIndexNext > iIndexThis)
- {
- memcpy(strGet, strSource + iIndexThis + 1, iIndexNext - iIndexThis - 1);
- strGet[iIndexNext - iIndexThis - 1] = '\0';
- }
- else
- strcpy_s(strGet, strlen(strGet), "");
- return 0;
- }
- /*
- void GetSysInfo()
- {
- WCHAR szFile[MAX_PATH + 1] = _T("");
- // 1.
- wsprintf(szFile, _T("%s\\lyfzServiceInfo.ini"), g_ModulePath);
- GetPrivateProfileString(_T("ServerInfo"), _T("dbServer"), _T(""), g_szDBServer, MAX_PATH, szFile);
- g_dwDBServerPort = GetPrivateProfileInt(_T("ServerInfo"), _T("dbServerPort"), 0, szFile);
- GetPrivateProfileString(_T("ServerInfo"), _T("dbAccount"), _T(""), g_szDBAccount, MAX_PATH, szFile);
- GetPrivateProfileString(_T("ServerInfo"), _T("dbPassWord"), _T(""), g_szDBPassWord, MAX_PATH, szFile);
- GetPrivateProfileString(_T("ServerInfo"), _T("dbName"), _T(""), g_szDBName, MAX_PATH, szFile);
- // 2.
- wsprintf(szFile, _T("%s\\GC0307.ini"), g_ModulePath);
- // 1.公共信息;
- g_nType = GetPrivateProfileInt(_T("CommonInfo"), _T("ConnectType"), 0, szFile);
- WCHAR wszMachineNumber[MAX_PATH + 1] = { 0 };
- GetPrivateProfileString(_T("CommonInfo"), _T("MachineNumber"), _T(""), wszMachineNumber, MAX_PATH, szFile);
- char szMachineNumber[MAX_PATH + 1] = { 0 };
- WChar2Char(szMachineNumber, wszMachineNumber);
- g_nMachineNumber = atol(szMachineNumber);
- WCHAR wszLicense[MAX_PATH + 1] = { 0 };
- GetPrivateProfileString(_T("CommonInfo"), _T("License"), _T(""), wszLicense, MAX_PATH, szFile);
- char szLicense[MAX_PATH + 1] = { 0 };
- WChar2Char(szLicense, wszLicense);
- g_nLicense = atol(szLicense);
- // 2.串口信息;
- g_nComPort = GetPrivateProfileInt(_T("SerialInfo"), _T("SerialPort"), 0, szFile);
- g_nBaudRate = GetPrivateProfileInt(_T("SerialInfo"), _T("Rate"), 0, szFile);
- g_nWaitDialTime = GetPrivateProfileInt(_T("SerialInfo"), _T("WaitDialTime"), 0, szFile);
- GetPrivateProfileString(_T("SerialInfo"), _T("TelNumber"), _T(""), g_szTelNumber, MAX_PATH, szFile);
- // 3.网络信息;
- GetPrivateProfileString(_T("NetInfo"), _T("IP"), _T(""), g_szIpAddress, MAX_PATH, szFile);
- g_nPort = GetPrivateProfileInt(_T("NetInfo"), _T("Port"), 0, szFile);
- g_nTimeOut = GetPrivateProfileInt(_T("NetInfo"), _T("TimeOut"), 0, szFile);
- g_nProtocolType = GetPrivateProfileInt(_T("NetInfo"), _T("ProtocolType"), 0, szFile);
- g_nNetPassword = GetPrivateProfileInt(_T("NetInfo"), _T("NetPassword"), 0, szFile);
- }
- */
|