DBServer.cpp 11 KB

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