123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- // WatchService.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "WatchService.h"
- #include "WinService.h"
- #include "Worker.h"
- #include "IMGCommon.h"
- #include <Shlwapi.h>
- #include "Global.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 唯一的应用程序对象
- CWinApp theApp;
- using namespace std;
- bool g_bwork = true;
- // 工作启动回调函数;
- void CALLBACK WorkStart()
- {
- // 创建工作线程;
- CWorker *pWorker = NULL;
- CWorker *pTail = NULL;
- for ( int i = 0; i < 10; i++ )
- {
- if (Global::g_szDirectory[i] != NULL )
- {
- if ( pWorker == NULL )
- {
- pWorker = pTail = (CWorker*)malloc(sizeof(CWorker));
- pWorker->m_pNext = NULL;
- _stprintf_s(pWorker->m_szDirectory, _T("%s"), Global::g_szDirectory[i]);
- pWorker->StartThread();
- free(Global::g_szDirectory[i]);
- }
- else
- {
- CWorker *p = (CWorker*)malloc(sizeof(CWorker));
- p->m_pNext = NULL;
- pTail->m_pNext = p;
- pTail = p;
- _stprintf_s(pTail->m_szDirectory, _T("%s"), Global::g_szDirectory[i]);
- pTail->StartThread();
- free(Global::g_szDirectory[i]);
- }
-
- }
- }
- // 取队列数据;
- int nIndex = 0;
- IMGCommon hImg;
- std::wstring dir;
- std::wstring name;
- std::wstring file;
- std::wstring filenew;
- std::wstring strit;
- #if 0
- std::vector<std::wstring>::iterator it;
- TCHAR szMsg[MAX_PATH];
- while (true)
- {
- it = CWorker::_files.begin();
- if ( it != CWorker::_files.end())
- {
- strit = it->c_str();
- nIndex = strit.find_last_of(_T('\\'));
- if ( nIndex != std::wstring::npos )
- {
- name = strit.substr(nIndex+1);
- // 去除ok,modifytime,thumbs.db文件;
- if ( _tcsicmp(name.c_str(), _T("ok")) == 0 || _tcsicmp(name.c_str(), _T("modifytime")) == 0 || _tcsicmp(name.c_str(), _T("thumbs.db")) == 0)
- {
- AutoThreadSection aSection(&CWorker::_critSection);
- CWorker::_files.erase(CWorker::_files.begin()); // 不能使用it,因为在push_back时,it会自动无效;
- continue;
- }
- dir = strit.substr(0, nIndex);
- // 是否是s,m文件;
- if ( name.at(0) == _T('s') || name.at(0) == _T('m') )
- {
- file = name;
- file.erase(0,1);
- filenew = dir;
- filenew.append(_T("\\"));
- filenew.append(file);
- if ( PathFileExists(filenew.c_str()) )
- {
- AutoThreadSection aSection(&CWorker::_critSection);
- CWorker::_files.erase(CWorker::_files.begin()); // 不能使用it,因为在push_back时,it会自动无效;
- continue;
- }
- }
- // 生成s图;
- filenew = dir;
- filenew.append(_T("\\s"));
- filenew.append(name);
- if (!PathFileExists(filenew.c_str()))
- hImg.SimpleImgThumbnail(strit.c_str(), SET_PIX(100,100), filenew.c_str(), 80);
- // 生成m图;
- filenew = dir;
- filenew.append(_T("\\m"));
- filenew.append(name);
- if (!PathFileExists(filenew.c_str()))
- hImg.SimpleImgThumbnail(strit.c_str(), SET_PIX(2000,2000), filenew.c_str(), 80);
- // 移除;
- AutoThreadSection aSection(&CWorker::_critSection);
- CWorker::_files.erase(CWorker::_files.begin()); // 不能使用it,因为在push_back时,it会自动无效;
- }
- }
- Sleep(300);
- }
- #else
- while (g_bwork)
- {
- if ( CWorker::m_phead )
- {
- strit = CWorker::m_phead->szfile;
- nIndex = strit.find_last_of(_T('\\'));
- if ( nIndex != std::wstring::npos )
- {
- name = strit.substr(nIndex+1);
- // 去除ok,modifytime,thumbs.db文件;
- if ( _tcsicmp(name.c_str(), _T("ok")) == 0 || _tcsicmp(name.c_str(), _T("modifytime")) == 0 || _tcsicmp(name.c_str(), _T("thumbs.db")) == 0)
- {
- AutoThreadSection aSection(&CWorker::_critSection);
- Watchfile *pnode = CWorker::m_phead;
- CWorker::m_phead = pnode->next;
- free(pnode);
- continue;
- }
- dir = strit.substr(0, nIndex);
- // 是否是s,m文件;
- if ( name.at(0) == _T('s') || name.at(0) == _T('m') )
- {
- file = name;
- file.erase(0,1);
- filenew = dir;
- filenew.append(_T("\\"));
- filenew.append(file);
- if ( PathFileExists(filenew.c_str()) )
- {
- AutoThreadSection aSection(&CWorker::_critSection);
- Watchfile *pnode = CWorker::m_phead;
- CWorker::m_phead = pnode->next;
- free(pnode);
- continue;
- }
- }
- // 生成s图;
- if ( Global::g_bThumbnails )
- {
- filenew = dir;
- filenew.append(_T("\\s"));
- filenew.append(name);
- if (!PathFileExists(filenew.c_str()) || Global::g_boverwrite)
- hImg.SimpleImgThumbnail(strit.c_str(), SET_PIX(100,100), filenew.c_str(), 80);
- }
- // 生成m图;
- if ( Global::g_bThumbnailm )
- {
- filenew = dir;
- filenew.append(_T("\\m"));
- filenew.append(name);
- if (!PathFileExists(filenew.c_str()) || Global::g_boverwrite)
- hImg.SimpleImgThumbnail(strit.c_str(), SET_PIX(2000,2000), filenew.c_str(), 80);
- }
- // 移除;
- AutoThreadSection aSection(&CWorker::_critSection);
- Watchfile *pnode = CWorker::m_phead;
- CWorker::m_phead = pnode->next;
- free(pnode);
- }
- }
- Sleep(Global::g_dwTimeout);
- }
- CWorker *pNode = NULL;
- while( pWorker )
- {
- pNode = pWorker;
- pWorker = pWorker->m_pNext;
- free(pNode);
- }
- #endif
- }
- // 工作结束回调函数;
- void CALLBACK WorkEndof()
- {
- g_bwork = false;
- }
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- HWND hwnd = GetForegroundWindow(); // 直接获得前景窗口的句柄
- SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
- HMODULE hModule = ::GetModuleHandle(NULL);
- Global::GetIniInfo();
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- _tprintf(_T("错误: MFC 初始化失败\n"));
- nRetCode = 1;
- }
- else
- {
- #ifdef _DEBUG
- /*
- // 定义环境句柄;
- SQLHENV henv = SQL_NULL_HENV;
- // 定义数据库连接句柄;
- SQLHDBC hdbc = SQL_NULL_HDBC;
- // 定义语句句柄;
- SQLHSTMT hstmt = SQL_NULL_HSTMT;
- // 1.初始化环境句柄;
- RETCODE retCode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
- if ( retCode == SQL_SUCCESS )
- {
- retCode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
- if ( retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO )
- {
- OutputDebugString(_T("设置Odbc环境失败\n"));
- return 0;
- }
- }
- // 2.动态配置数据源 //不要把UID和PWD加入,否则失败;//在SQLConnect中传入;
- if ( !SQLConfigDataSource(NULL, ODBC_ADD_DSN, _T("SQL Server"),_T("DSN=mydb\0SERVER=127.0.0.1\0DATABASE=db\0\0")) )
- {
- OutputDebugString(_T("创建数据源失败\n"));
- }
- // 3.连接数据库源;
- retCode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
- if ( retCode == SQL_SUCCESS || retCode == SQL_SUCCESS_WITH_INFO )
- {
- retCode = SQLConnect(hdbc, _T("mydb"), SQL_NTS, _T("sa"), SQL_NTS, _T("ly1234"), SQL_NTS);
- if ( retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO )
- {
- OutputDebugString(_T("连接数据库失败\n"));
- return 0;
- }
- }
- // 4.创建语句句柄;
- retCode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
- if (retCode == SQL_SUCCESS || retCode == SQL_SUCCESS_WITH_INFO )
- {
- #if 1 // 方式一;
- TCHAR szSql[1024] = {0};
- _stprintf_s(szSql, _T("select * from NetShareInfo"));
- retCode = SQLExecDirect(hstmt, szSql, _tcslen(szSql));
- if (retCode == SQL_SUCCESS || retCode == SQL_SUCCESS_WITH_INFO )
- {
- }
- #else // 方式二;
- #endif
- // end.释放语句句柄;
- SQLCloseCursor(hstmt);
- SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
- }
-
- // 5.断开数据源;
- SQLDisconnect(hdbc);
- SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
- SQLFreeHandle(SQL_HANDLE_ENV,henv);
- */
- WorkStart();
- system("pause");
- WorkEndof();
- #else
- // TODO: 在此处为应用程序的行为编写代码。
- // 1.先判断服务是否存在;
- WinService::InitServiceName();
- if ( WinService::IsInstalled())
- {
- if ( argc > 1)
- {
- // 卸载服务
- if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
- WinService::Uninstall();
- // 启动服务
- if (_tcscmp((TCHAR *)argv[1], _T("/startsvc")) == 0)
- WinService::StartSvc();
- // 停止服务
- if (_tcscmp((TCHAR *)argv[1], _T("/stopsvc")) == 0)
- WinService::StopSvc();
- return 0;
- }
- else
- {
- WinService::SetCallBack(WorkStart, WorkEndof);
- SERVICE_TABLE_ENTRY st[] =
- {
- { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
- { NULL, NULL }
- };
- if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败;
- {
- //LOG4C((LOG_NOTICE,"服务已安装,须系统运行控制"));
- //lyfzGlobal::WriteTextLog(_T("服务已安装,须系统运行控制"));
- WinService::LogEvent(_T("Register Service Main Function Error!"));
- return 0;
- }
- }
- }
- else
- {
- // 2.不存在,安装服务;
- printf("服务不存在,正在安装……\n");
- WinService::SetCallBack(WorkStart, WorkEndof);
- SERVICE_TABLE_ENTRY st[] =
- {
- { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
- { NULL, NULL }
- };
- if ( WinService::Install() )
- printf("服务安装成功……\n");
- else
- printf("服务安装失败……\n");
- system("pause");
- }
- #endif
- }
- return nRetCode;
- }
|