DBServer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /****************************************************************/
  2. /* */
  3. /* DBServerApp.cpp */
  4. /* */
  5. /* Defines the class behaviors for the application. */
  6. /* */
  7. /* Programmed by LYFZ van der Meer */
  8. /* Copyright LYFZ Software Solutions 2002 */
  9. /* http://www.LYFZvandermeer.nl */
  10. /* */
  11. /* Last updated: 10 july 2002 */
  12. /* */
  13. /****************************************************************/
  14. #include "stdafx.h"
  15. #include "DBServer.h"
  16. #include "DBServerDlg.h"
  17. #include "theDBServer.h"
  18. #include "MyLock.h"
  19. #include "mysqldata.h"
  20. #include "DumpCollect.h"
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. BEGIN_MESSAGE_MAP(CDBServer, CWinApp)
  27. //{{AFX_MSG_MAP(CDBServer)
  28. ON_COMMAND(ID_APP_SHOW, OnAppShow)
  29. ON_COMMAND(ID_APP_EXIT, OnAppExit)
  30. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  31. ON_COMMAND(ID_HELP_INDEX, OnHelpIndex)
  32. //}}AFX_MSG_MAP
  33. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CDBServer construction
  37. CDBServer::CDBServer()
  38. {
  39. // TODO: add construction code here,
  40. // Place all significant initialization in InitInstance
  41. }
  42. /////////////////////////////////////////////////////////////////////////////
  43. // The one and only CDBServer object
  44. CDBServer theApp;
  45. CtheDBServer theServer;
  46. /////////////////////////////////////////////////////////////////////////////
  47. // CDBServer initialization
  48. BOOL CDBServer::InitInstance()
  49. {
  50. AfxInitRichEdit2();
  51. CWinApp::InitInstance();
  52. AfxEnableControlContainer();
  53. DumpCollect::InitMinDump();
  54. InitSocketLibrary(MAKEWORD(2,2));
  55. // 2.配置数据库连接信息;
  56. char server[50];
  57. DWORD leng=50;
  58. memset(server, 0, 50);
  59. ::GetComputerName(server, &leng);
  60. _stprintf_s(g_szHostName, MAX_PATH, _T("%s"), server);
  61. g_localname = server;
  62. TCHAR szDir[MAX_PATH];
  63. ::GetModuleFileName(NULL, szDir, MAX_PATH);
  64. g_mainpath = szDir;
  65. g_mainpath = g_mainpath.Left (g_mainpath.ReverseFind ('\\'));
  66. if( GetIniInfo() == -1)
  67. {
  68. WriteTextLog("未找到配置文件:ServiceInfo.ini");
  69. return FALSE;
  70. }
  71. // 1.根据运行的端口号来运行单实例;
  72. _stprintf_s(szDir, _T("lyfzServer%d"), g_dwCSPort);
  73. HANDLE hObject = CreateMutex(NULL, FALSE, szDir);
  74. if(GetLastError() == ERROR_ALREADY_EXISTS)
  75. {
  76. return false;
  77. }
  78. g_strdbpsw.Format("uid=%s;pwd=%s",g_szDBAccount,g_szDBPassWord);
  79. if ( g_dwDBServerPort != 0 )
  80. g_strdbServrename.Format("%s,%d",g_szDBSource,g_dwDBServerPort);
  81. else
  82. g_strdbServrename.Format("%s",g_szDBSource);
  83. WriteTextLog("启动");
  84. // 3.初始化加密狗信息,并获取相应的加密数据;
  85. MyLock lock("ytSoftKeyaccessxiao");
  86. if(ytSoftKey.IniEx())
  87. {
  88. AfxMessageBox(_T("未找到加密狗或加密狗版本不一致!"));
  89. return false;
  90. }
  91. #ifdef CONNCOUNT_VERSION // Jeff.座席版;
  92. {
  93. int value[8];
  94. double f[8];
  95. TCHAR s0[50]=_T(""),s1[50]=_T(""),s2[50]=_T(""),s3[50]=_T(""),s4[50]=_T(""),s5[50]=_T(""),s6[50]=_T(""),s7[50]=_T("");
  96. //运行自定义函数
  97. int ret=ytSoftKey.getdata(&value[0],&value[1],&value[2],&value[3],&value[4],&value[5],&value[6],&value[7],
  98. &f[0],&f[1],&f[2],&f[3],&f[4],&f[5],&f[6],&f[7],
  99. s0,s1,s2,s3,s4,s5,s6,s7);
  100. if(ret!=0 && ret!=-43)
  101. {
  102. g_conncount=-1; // Jeff:S0 存储着座席数量;
  103. }
  104. else
  105. {
  106. g_conncount=atoi(s0); // Jeff:S0 存储着座席数量;
  107. g_sdomain=s1;
  108. g_bReg=atoi(s2); // ???医院跟踪系统??
  109. }
  110. }
  111. #else
  112. {
  113. int value[8];
  114. double f[8];
  115. TCHAR s0[50]=_T(""),s1[50]=_T(""),s2[50]=_T(""),s3[50]=_T(""),s4[50]=_T(""),s5[50]=_T(""),s6[50]=_T(""),s7[50]=_T("");
  116. //运行自定义函数
  117. int ret=ytSoftKey.getdata(&value[0],&value[1],&value[2],&value[3],&value[4],&value[5],&value[6],&value[7],
  118. &f[0],&f[1],&f[2],&f[3],&f[4],&f[5],&f[6],&f[7],
  119. s0,s1,s2,s3,s4,s5,s6,s7);
  120. if(ret!=0 && ret!=-43)
  121. {
  122. }
  123. else
  124. {
  125. g_sdomain=s1;
  126. g_bReg=atoi(s2);
  127. }
  128. }
  129. #endif
  130. if ( g_sdomain.IsEmpty() )
  131. {
  132. AfxMessageBox(_T("加密锁域名为空,无法启动服务器!"));
  133. return FALSE;
  134. }
  135. CString savedir=g_mainpath+"\\数据备份";
  136. if(!CheckFolderFileExist (savedir))
  137. ::CreateDirectory (savedir, NULL);
  138. ::CoInitialize( NULL );
  139. BOOL bMasterDb=0;
  140. BOOL bUserDb=0;
  141. // 4.连接master数据库,判断数据库连接是否正常;
  142. CDatabase g_masterdb; // Jeff:master用于附加与分离数据库;
  143. try
  144. {
  145. TCHAR szConnectString[MAX_PATH] = _T("");
  146. if( g_dwDBServerPort != 0)
  147. {
  148. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  149. sprintf(szConnectString,DB_SW_CONN_WITH_PORT, g_szDBSource,g_dwDBServerPort, _T("master"), g_szDBName);
  150. else
  151. sprintf(szConnectString,DB_SS_CONN_WITH_PORT,g_szDBSource,g_dwDBServerPort, _T("master"), g_szDBAccount,g_szDBPassWord);
  152. }
  153. else
  154. {
  155. if ( _tcscmp(g_szDBAccount, _T("")) == 0 )
  156. sprintf(szConnectString,DB_SW_CONN_WITHOUT_PORT, g_szDBSource, _T("master"), g_szDBName);
  157. else
  158. sprintf(szConnectString,DB_SS_CONN_WITHOUT_PORT, g_szDBSource, _T("master"), g_szDBAccount, g_szDBPassWord);
  159. }
  160. g_masterdb.OpenEx(szConnectString,CDatabase::noOdbcDialog);
  161. bMasterDb=1;
  162. }
  163. catch(CDBException * e)
  164. {
  165. #ifdef _DEBUG
  166. e->ReportError();
  167. #endif
  168. e->Delete();
  169. }
  170. if(bMasterDb==0)
  171. {
  172. WriteTextLog("sql数据库未启动!");
  173. ShellExecute(NULL, _T("open"), g_mainpath+"\\AutoRun.exe", NULL, NULL, SW_HIDE);
  174. return false;
  175. }
  176. // 启动线程检测数据库完整性;
  177. if ( g_bCheckdb )
  178. {
  179. HANDLE hCheckdb = ::CreateThread(NULL, 0, CheckdbThread, NULL, 0, NULL);
  180. if ( hCheckdb == NULL )
  181. WriteTextLog(_T("创建检测数据库线程失败!"));
  182. else
  183. CloseHandle(hCheckdb);
  184. }
  185. CODBCPool *pdbpool = new CODBCPool;
  186. if ( pdbpool->InitializePool(g_szDBSource, g_dwDBServerPort, g_szDBAccount, g_szDBPassWord, g_szDBName, g_dwDBPoolDef, g_dwDBPoolMaxCount) )
  187. {
  188. g_dbpool.Add(pdbpool);
  189. bUserDb=1;
  190. BackupDatabase(savedir, 1, 0); // Jeff. savedir=\\数据备份
  191. CDatabase *pdb = NULL;
  192. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  193. CString strSQL;
  194. strSQL.Format(_T("update path set path1='%s'"), g_sdomain);
  195. if ( pdb )
  196. {
  197. try
  198. {
  199. pdb->ExecuteSQL(strSQL);
  200. pdb->ExecuteSQL(_T("IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[historydb]') AND type in (N'U')) CREATE TABLE [historydb]([year] VARCHAR(50))"));
  201. }
  202. catch (CDBException* e)
  203. {
  204. WriteTextLog(_T("%s-<%d>-%s,%s"), __FILE__, __LINE__, __FUNCTION__, e->m_strError);
  205. e->Delete();
  206. }
  207. }
  208. }
  209. else
  210. {
  211. delete pdbpool;
  212. }
  213. if(bUserDb==0 && bMasterDb)
  214. {
  215. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  216. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  217. WriteTextLog("数据库初始化中...");
  218. TCHAR szDBFile[MAX_PATH] = _T("");
  219. sprintf(szDBFile,"%s\\数据\\%s.mdf",g_mainpath,g_szDBName);
  220. if(::PathFileExists(szDBFile) == 0 )
  221. {
  222. WriteTextLog("数据库文件丢失!");
  223. return false;
  224. }
  225. BOOL bLog=0;
  226. TCHAR szDBLogFile[MAX_PATH] = _T("");
  227. sprintf(szDBLogFile,"%s\\数据\\%s_log.ldf",g_mainpath,g_szDBName);
  228. if(::PathFileExists(szDBLogFile) == 0 )
  229. {
  230. bLog=1;
  231. }
  232. CString sql;
  233. try
  234. {
  235. // g_masterdb.ExecuteSQL ("exec sp_detach_db db");
  236. }
  237. catch(CDBException * e)
  238. {
  239. #ifdef _DEBUG
  240. e->ReportError();
  241. #endif
  242. e->Delete();
  243. }
  244. if(bLog)
  245. sql.Format("exec sp_attach_db '%s',@filename1='%s\\数据\\%s.mdf',@filename2='%s\\数据\\%s_log.ldf'",g_szDBName,g_ModulePath,g_szDBName,g_ModulePath,g_szDBName);
  246. else
  247. sql.Format("exec sp_attach_db '%s',@filename1='%s\\数据\\%s.mdf'", g_szDBName,g_ModulePath,g_szDBName);
  248. try
  249. {
  250. g_masterdb.ExecuteSQL (sql); // 同master数据库去执行附加和分离;
  251. }
  252. catch(CDBException * e)
  253. {
  254. g_masterdb.Close();
  255. WriteTextLog(e->m_strError);
  256. WriteTextLog("数据库初始化失败!");
  257. #ifdef _DEBUG
  258. e->ReportError();
  259. #endif
  260. e->Delete();
  261. return false;
  262. }
  263. g_masterdb.Close();
  264. WriteTextLog("数据库初始化完成,请重新启动系统!");
  265. return false;
  266. }
  267. // 在数据库完成初始化后,判断本地目录下是否有MSCHRT20.OCX文件,无则从数据库下载;2014.09.06
  268. // 客户端从dwToUserID==211里传递MSCHRT20.OCX;
  269. CString strMSCHRTOCX = g_mainpath+"\\系统文件\\MSCHRT20.OCX";
  270. OFSTRUCT ofStruct;
  271. OpenFile(strMSCHRTOCX,&ofStruct,OF_EXIST);
  272. if( ERROR_FILE_NOT_FOUND == GetLastError())
  273. {
  274. CDatabase *pdb = NULL;
  275. ODBCConnGuard ConnGuard(pdb, -1, 3000);
  276. if ( pdb )
  277. {
  278. CRstUpdate rsSt;
  279. rsSt.m_pDatabase = pdb;
  280. rsSt.Open();
  281. if(!rsSt.IsEOF())
  282. {
  283. CFile fp;
  284. if(fp.Open(strMSCHRTOCX, CFile::modeCreate|CFile::modeWrite))
  285. {
  286. void *pData = GlobalLock(rsSt.m_lbOCX.m_hData);
  287. fp.Write(pData, rsSt.m_lbOCX.m_dwDataLength);
  288. fp.Close();
  289. GlobalUnlock(rsSt.m_lbOCX.m_hData);
  290. }
  291. }
  292. rsSt.Close();
  293. }
  294. }
  295. ULONG_PTR gdiplusToken;
  296. GdiplusStartupInput gdiplusStartupInput;
  297. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  298. CString path1="客户原片(管理软件)$";
  299. CString path2="修好的片(管理软件)$";
  300. CString path3="设计好的片(管理软件)$";
  301. CString path4="精修好的片(管理软件)$";
  302. CString path5="礼服图片(管理软件)$";
  303. CString path6="电话录音(管理软件)$";
  304. CString temp;
  305. if(CheckFolderFileExist (g_mainpath+"\\"+path1))
  306. {
  307. temp.Format ("net share %s=%s\\%s", path1, g_mainpath, path1);
  308. WinExec(temp,SW_HIDE);
  309. }
  310. if(CheckFolderFileExist (g_mainpath+"\\"+path2))
  311. {
  312. temp.Format ("net share %s=%s\\%s", path2, g_mainpath, path2);
  313. WinExec(temp,SW_HIDE);
  314. }
  315. if(CheckFolderFileExist (g_mainpath+"\\"+path3))
  316. {
  317. temp.Format ("net share %s=%s\\%s", path3, g_mainpath, path3);
  318. WinExec(temp,SW_HIDE);
  319. }
  320. if(CheckFolderFileExist (g_mainpath+"\\"+path4))
  321. {
  322. temp.Format ("net share %s=%s\\%s", path4, g_mainpath, path4);
  323. WinExec(temp,SW_HIDE);
  324. }
  325. if(CheckFolderFileExist (g_mainpath+"\\"+path5))
  326. {
  327. temp.Format ("net share %s=%s\\%s", path5, g_mainpath, path5);
  328. WinExec(temp,SW_HIDE);
  329. }
  330. if(CheckFolderFileExist (g_mainpath+"\\"+path6))
  331. {
  332. temp.Format ("net share %s=%s\\%s", path6, g_mainpath, path6);
  333. WinExec(temp,SW_HIDE);
  334. }
  335. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  336. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  337. #ifdef ENTERPRISE_VERSION
  338. autorunpath=g_mainpath+"\\DataSyncReceive.exe";
  339. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  340. autorunpath=g_mainpath+"\\DataSyncSend.exe";
  341. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  342. autorunpath=g_mainpath+"\\CPhotoFTPReceive.exe";
  343. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  344. autorunpath=g_mainpath+"\\CPhotoFTPSend.exe";
  345. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  346. #endif
  347. // Standard initialization
  348. // If you are not using these features and wish to reduce the size
  349. // of your final executable, you should remove from the following
  350. // the specific initialization routines you do not need.
  351. #ifdef _AFXDLL
  352. Enable3dControls(); // Call this when using MFC in a shared DLL
  353. #else
  354. Enable3dControlsStatic(); // Call this when linking to MFC statically
  355. #endif
  356. InitCommonControls();
  357. CDBServerDlg dlg;
  358. m_pMainWnd = &dlg;
  359. int nResponse = dlg.DoModal();
  360. if (nResponse == IDOK)
  361. {
  362. }
  363. else if (nResponse == IDCANCEL)
  364. {
  365. }
  366. GdiplusShutdown(gdiplusToken);
  367. g_Instance = AfxGetResourceHandle();
  368. return FALSE;
  369. }
  370. DWORD CDBServer::CheckdbThread(LPVOID lpParamter)
  371. {
  372. // 检查数据库的完整性;
  373. g_bIsCatalogDamage = IsCatalogDamage();
  374. return 0;
  375. }
  376. void CDBServer::OnAppShow()
  377. {
  378. if (m_pMainWnd)
  379. m_pMainWnd->ShowWindow(SW_SHOW);
  380. }
  381. void CDBServer::OnAppExit()
  382. {
  383. //御载lyfzBackupModle.dll
  384. m_pMainWnd->DestroyWindow();
  385. }
  386. void CDBServer::OnAppAbout()
  387. {
  388. }
  389. /********************************************************************/
  390. /* */
  391. /* Function name : OnHelpIndex */
  392. /* Description : Command to show help file. */
  393. /* */
  394. /********************************************************************/
  395. void CDBServer::OnHelpIndex()
  396. {
  397. // launch help
  398. // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L);
  399. }
  400. int CDBServer::ExitInstance()
  401. {
  402. // 释放Socket库;
  403. ReleaseSocketLibrary();
  404. return CWinApp::ExitInstance();
  405. }