WatchService.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // WatchService.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "WatchService.h"
  5. #include "WinService.h"
  6. #include "Worker.h"
  7. #include "IMGCommon.h"
  8. #include <Shlwapi.h>
  9. #include "Global.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #endif
  13. // 唯一的应用程序对象
  14. CWinApp theApp;
  15. using namespace std;
  16. bool g_bwork = true;
  17. // 工作启动回调函数;
  18. void CALLBACK WorkStart()
  19. {
  20. // 创建工作线程;
  21. CWorker *pWorker = NULL;
  22. CWorker *pTail = NULL;
  23. for ( int i = 0; i < 10; i++ )
  24. {
  25. if (Global::g_szDirectory[i] != NULL )
  26. {
  27. if ( pWorker == NULL )
  28. {
  29. pWorker = pTail = (CWorker*)malloc(sizeof(CWorker));
  30. pWorker->m_pNext = NULL;
  31. _stprintf_s(pWorker->m_szDirectory, _T("%s"), Global::g_szDirectory[i]);
  32. pWorker->StartThread();
  33. free(Global::g_szDirectory[i]);
  34. }
  35. else
  36. {
  37. CWorker *p = (CWorker*)malloc(sizeof(CWorker));
  38. p->m_pNext = NULL;
  39. pTail->m_pNext = p;
  40. pTail = p;
  41. _stprintf_s(pTail->m_szDirectory, _T("%s"), Global::g_szDirectory[i]);
  42. pTail->StartThread();
  43. free(Global::g_szDirectory[i]);
  44. }
  45. }
  46. }
  47. // 取队列数据;
  48. int nIndex = 0;
  49. IMGCommon hImg;
  50. std::wstring dir;
  51. std::wstring name;
  52. std::wstring file;
  53. std::wstring filenew;
  54. std::wstring strit;
  55. #if 0
  56. std::vector<std::wstring>::iterator it;
  57. TCHAR szMsg[MAX_PATH];
  58. while (true)
  59. {
  60. it = CWorker::_files.begin();
  61. if ( it != CWorker::_files.end())
  62. {
  63. strit = it->c_str();
  64. nIndex = strit.find_last_of(_T('\\'));
  65. if ( nIndex != std::wstring::npos )
  66. {
  67. name = strit.substr(nIndex+1);
  68. // 去除ok,modifytime,thumbs.db文件;
  69. if ( _tcsicmp(name.c_str(), _T("ok")) == 0 || _tcsicmp(name.c_str(), _T("modifytime")) == 0 || _tcsicmp(name.c_str(), _T("thumbs.db")) == 0)
  70. {
  71. AutoThreadSection aSection(&CWorker::_critSection);
  72. CWorker::_files.erase(CWorker::_files.begin()); // 不能使用it,因为在push_back时,it会自动无效;
  73. continue;
  74. }
  75. dir = strit.substr(0, nIndex);
  76. // 是否是s,m文件;
  77. if ( name.at(0) == _T('s') || name.at(0) == _T('m') )
  78. {
  79. file = name;
  80. file.erase(0,1);
  81. filenew = dir;
  82. filenew.append(_T("\\"));
  83. filenew.append(file);
  84. if ( PathFileExists(filenew.c_str()) )
  85. {
  86. AutoThreadSection aSection(&CWorker::_critSection);
  87. CWorker::_files.erase(CWorker::_files.begin()); // 不能使用it,因为在push_back时,it会自动无效;
  88. continue;
  89. }
  90. }
  91. // 生成s图;
  92. filenew = dir;
  93. filenew.append(_T("\\s"));
  94. filenew.append(name);
  95. if (!PathFileExists(filenew.c_str()))
  96. hImg.SimpleImgThumbnail(strit.c_str(), SET_PIX(100,100), filenew.c_str(), 80);
  97. // 生成m图;
  98. filenew = dir;
  99. filenew.append(_T("\\m"));
  100. filenew.append(name);
  101. if (!PathFileExists(filenew.c_str()))
  102. hImg.SimpleImgThumbnail(strit.c_str(), SET_PIX(2000,2000), filenew.c_str(), 80);
  103. // 移除;
  104. AutoThreadSection aSection(&CWorker::_critSection);
  105. CWorker::_files.erase(CWorker::_files.begin()); // 不能使用it,因为在push_back时,it会自动无效;
  106. }
  107. }
  108. Sleep(300);
  109. }
  110. #else
  111. while (g_bwork)
  112. {
  113. if ( CWorker::m_phead )
  114. {
  115. strit = CWorker::m_phead->szfile;
  116. nIndex = strit.find_last_of(_T('\\'));
  117. if ( nIndex != std::wstring::npos )
  118. {
  119. name = strit.substr(nIndex+1);
  120. // 去除ok,modifytime,thumbs.db文件;
  121. if ( _tcsicmp(name.c_str(), _T("ok")) == 0 || _tcsicmp(name.c_str(), _T("modifytime")) == 0 || _tcsicmp(name.c_str(), _T("thumbs.db")) == 0)
  122. {
  123. AutoThreadSection aSection(&CWorker::_critSection);
  124. Watchfile *pnode = CWorker::m_phead;
  125. CWorker::m_phead = pnode->next;
  126. free(pnode);
  127. continue;
  128. }
  129. dir = strit.substr(0, nIndex);
  130. // 是否是s,m文件;
  131. if ( name.at(0) == _T('s') || name.at(0) == _T('m') )
  132. {
  133. file = name;
  134. file.erase(0,1);
  135. filenew = dir;
  136. filenew.append(_T("\\"));
  137. filenew.append(file);
  138. if ( PathFileExists(filenew.c_str()) )
  139. {
  140. AutoThreadSection aSection(&CWorker::_critSection);
  141. Watchfile *pnode = CWorker::m_phead;
  142. CWorker::m_phead = pnode->next;
  143. free(pnode);
  144. continue;
  145. }
  146. }
  147. // 生成s图;
  148. if ( Global::g_bThumbnails )
  149. {
  150. filenew = dir;
  151. filenew.append(_T("\\s"));
  152. filenew.append(name);
  153. if (!PathFileExists(filenew.c_str()) || Global::g_boverwrite)
  154. hImg.SimpleImgThumbnail(strit.c_str(), SET_PIX(100,100), filenew.c_str(), 80);
  155. }
  156. // 生成m图;
  157. if ( Global::g_bThumbnailm )
  158. {
  159. filenew = dir;
  160. filenew.append(_T("\\m"));
  161. filenew.append(name);
  162. if (!PathFileExists(filenew.c_str()) || Global::g_boverwrite)
  163. hImg.SimpleImgThumbnail(strit.c_str(), SET_PIX(2000,2000), filenew.c_str(), 80);
  164. }
  165. // 移除;
  166. AutoThreadSection aSection(&CWorker::_critSection);
  167. Watchfile *pnode = CWorker::m_phead;
  168. CWorker::m_phead = pnode->next;
  169. free(pnode);
  170. }
  171. }
  172. Sleep(Global::g_dwTimeout);
  173. }
  174. CWorker *pNode = NULL;
  175. while( pWorker )
  176. {
  177. pNode = pWorker;
  178. pWorker = pWorker->m_pNext;
  179. free(pNode);
  180. }
  181. #endif
  182. }
  183. // 工作结束回调函数;
  184. void CALLBACK WorkEndof()
  185. {
  186. g_bwork = false;
  187. }
  188. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  189. {
  190. int nRetCode = 0;
  191. HWND hwnd = GetForegroundWindow(); // 直接获得前景窗口的句柄
  192. SendMessage(hwnd , WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL,MAKEINTRESOURCE(IDI_MICON)));
  193. HMODULE hModule = ::GetModuleHandle(NULL);
  194. Global::GetIniInfo();
  195. // 初始化 MFC 并在失败时显示错误
  196. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  197. {
  198. // TODO: 更改错误代码以符合您的需要
  199. _tprintf(_T("错误: MFC 初始化失败\n"));
  200. nRetCode = 1;
  201. }
  202. else
  203. {
  204. #ifdef _DEBUG
  205. /*
  206. // 定义环境句柄;
  207. SQLHENV henv = SQL_NULL_HENV;
  208. // 定义数据库连接句柄;
  209. SQLHDBC hdbc = SQL_NULL_HDBC;
  210. // 定义语句句柄;
  211. SQLHSTMT hstmt = SQL_NULL_HSTMT;
  212. // 1.初始化环境句柄;
  213. RETCODE retCode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
  214. if ( retCode == SQL_SUCCESS )
  215. {
  216. retCode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
  217. if ( retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO )
  218. {
  219. OutputDebugString(_T("设置Odbc环境失败\n"));
  220. return 0;
  221. }
  222. }
  223. // 2.动态配置数据源 //不要把UID和PWD加入,否则失败;//在SQLConnect中传入;
  224. if ( !SQLConfigDataSource(NULL, ODBC_ADD_DSN, _T("SQL Server"),_T("DSN=mydb\0SERVER=127.0.0.1\0DATABASE=db\0\0")) )
  225. {
  226. OutputDebugString(_T("创建数据源失败\n"));
  227. }
  228. // 3.连接数据库源;
  229. retCode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
  230. if ( retCode == SQL_SUCCESS || retCode == SQL_SUCCESS_WITH_INFO )
  231. {
  232. retCode = SQLConnect(hdbc, _T("mydb"), SQL_NTS, _T("sa"), SQL_NTS, _T("ly1234"), SQL_NTS);
  233. if ( retCode != SQL_SUCCESS && retCode != SQL_SUCCESS_WITH_INFO )
  234. {
  235. OutputDebugString(_T("连接数据库失败\n"));
  236. return 0;
  237. }
  238. }
  239. // 4.创建语句句柄;
  240. retCode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
  241. if (retCode == SQL_SUCCESS || retCode == SQL_SUCCESS_WITH_INFO )
  242. {
  243. #if 1 // 方式一;
  244. TCHAR szSql[1024] = {0};
  245. _stprintf_s(szSql, _T("select * from NetShareInfo"));
  246. retCode = SQLExecDirect(hstmt, szSql, _tcslen(szSql));
  247. if (retCode == SQL_SUCCESS || retCode == SQL_SUCCESS_WITH_INFO )
  248. {
  249. }
  250. #else // 方式二;
  251. #endif
  252. // end.释放语句句柄;
  253. SQLCloseCursor(hstmt);
  254. SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
  255. }
  256. // 5.断开数据源;
  257. SQLDisconnect(hdbc);
  258. SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
  259. SQLFreeHandle(SQL_HANDLE_ENV,henv);
  260. */
  261. WorkStart();
  262. system("pause");
  263. WorkEndof();
  264. #else
  265. // TODO: 在此处为应用程序的行为编写代码。
  266. // 1.先判断服务是否存在;
  267. WinService::InitServiceName();
  268. if ( WinService::IsInstalled())
  269. {
  270. if ( argc > 1)
  271. {
  272. // 卸载服务
  273. if (_tcscmp((TCHAR *)argv[1], _T("/uninstall")) == 0)
  274. WinService::Uninstall();
  275. // 启动服务
  276. if (_tcscmp((TCHAR *)argv[1], _T("/startsvc")) == 0)
  277. WinService::StartSvc();
  278. // 停止服务
  279. if (_tcscmp((TCHAR *)argv[1], _T("/stopsvc")) == 0)
  280. WinService::StopSvc();
  281. return 0;
  282. }
  283. else
  284. {
  285. WinService::SetCallBack(WorkStart, WorkEndof);
  286. SERVICE_TABLE_ENTRY st[] =
  287. {
  288. { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
  289. { NULL, NULL }
  290. };
  291. if (!::StartServiceCtrlDispatcher(st)) // 在直接双击.exe时,这步会运行失败;
  292. {
  293. //LOG4C((LOG_NOTICE,"服务已安装,须系统运行控制"));
  294. //lyfzGlobal::WriteTextLog(_T("服务已安装,须系统运行控制"));
  295. WinService::LogEvent(_T("Register Service Main Function Error!"));
  296. return 0;
  297. }
  298. }
  299. }
  300. else
  301. {
  302. // 2.不存在,安装服务;
  303. printf("服务不存在,正在安装……\n");
  304. WinService::SetCallBack(WorkStart, WorkEndof);
  305. SERVICE_TABLE_ENTRY st[] =
  306. {
  307. { WinService::g_scVariant.scName, (LPSERVICE_MAIN_FUNCTION)WinService::ServiceMain },
  308. { NULL, NULL }
  309. };
  310. if ( WinService::Install() )
  311. printf("服务安装成功……\n");
  312. else
  313. printf("服务安装失败……\n");
  314. system("pause");
  315. }
  316. #endif
  317. }
  318. return nRetCode;
  319. }