DBServer.cpp 11 KB

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