DBServer.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. // 1.只运行单实例;
  51. HANDLE hObject = CreateMutex(NULL,FALSE,_T("CDBServerXiao"));
  52. if(GetLastError() == ERROR_ALREADY_EXISTS)
  53. {
  54. return false;
  55. }
  56. // 2.配置数据库连接信息;
  57. char server[50];
  58. DWORD leng=50;
  59. memset(server, 0, 50);
  60. ::GetComputerName(server, &leng);
  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. WriteLogin("未找到配置文件:ServiceInfo.ini");
  69. return FALSE;
  70. }
  71. g_strdbpsw.Format("uid=%s;pwd=%s",g_szDBAccount,g_szDBPassWord);
  72. if ( g_dwDBServerPort != 0 )
  73. g_strdbServrename.Format("%s,%d",g_szDBSource,g_dwDBServerPort);
  74. else
  75. g_strdbServrename.Format("%s",g_szDBSource);
  76. WriteLogin("启动");
  77. // 3.初始化加密狗信息,并获取相应的加密数据;
  78. MyLock lock("ytSoftKeyaccessxiao");
  79. if(ytSoftKey.Ini())
  80. {
  81. WriteLogin("未找到加密狗"); //AfxMessageBox("未找到加密锁,程序退出");
  82. return false;
  83. }
  84. #ifdef CONNCOUNT_VERSION // Jeff.座席版;
  85. {
  86. long value[8];
  87. double f[8];
  88. 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("");
  89. //运行自定义函数
  90. int ret=ytSoftKey.getdata(&value[0],&value[1],&value[2],&value[3],&value[4],&value[5],&value[6],&value[7],
  91. &f[0],&f[1],&f[2],&f[3],&f[4],&f[5],&f[6],&f[7],
  92. s0,s1,s2,s3,s4,s5,s6,s7);
  93. if(ret!=0 && ret!=-43)
  94. {
  95. g_conncount=-1; // Jeff:S0 存储着座席数量;
  96. }
  97. else
  98. {
  99. g_conncount=atoi(s0); // Jeff:S0 存储着座席数量;
  100. g_sdomain=s1;
  101. g_bReg=atoi(s2); // ???医院跟踪系统??
  102. }
  103. }
  104. #else
  105. {
  106. long value[8];
  107. double f[8];
  108. 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("");
  109. //运行自定义函数
  110. int ret=ytSoftKey.getdata(&value[0],&value[1],&value[2],&value[3],&value[4],&value[5],&value[6],&value[7],
  111. &f[0],&f[1],&f[2],&f[3],&f[4],&f[5],&f[6],&f[7],
  112. s0,s1,s2,s3,s4,s5,s6,s7);
  113. if(ret!=0 && ret!=-43)
  114. {
  115. }
  116. else
  117. {
  118. g_sdomain=s1;
  119. g_bReg=atoi(s2);
  120. }
  121. }
  122. #endif
  123. CString savedir=g_mainpath+"\\数据备份";
  124. if(!CheckFolderFileExist (savedir))
  125. ::CreateDirectory (savedir, NULL);
  126. ::CoInitialize( NULL );
  127. BOOL bMasterDb=0;
  128. BOOL bUserDb=0;
  129. // 4.连接master数据库,判断数据库连接是否正常;
  130. CDatabase g_masterdb; // Jeff:master用于附加与分离数据库;
  131. try
  132. {
  133. TCHAR szConnectString[MAX_PATH] = _T("");
  134. if( g_dwDBServerPort != 0)
  135. sprintf(szConnectString,
  136. "driver={SQL Server};Server=%s,%d;database=master;uid=%s;pwd=%s",
  137. g_szDBSource,g_dwDBServerPort,g_szDBAccount,g_szDBPassWord);
  138. else
  139. sprintf(szConnectString,
  140. "driver={SQL Server};Server=%s;database=master;uid=%s;pwd=%s",
  141. g_szDBSource,g_szDBAccount,g_szDBPassWord);
  142. g_masterdb.OpenEx(szConnectString,CDatabase::noOdbcDialog);
  143. bMasterDb=1;
  144. }
  145. catch(CDBException * e)
  146. {
  147. e->Delete();
  148. }
  149. if(bMasterDb==0)
  150. {
  151. WriteLogin("sql数据库未启动!");
  152. ShellExecute(NULL, _T("open"), g_mainpath+"\\AutoRun.exe", NULL, NULL, SW_HIDE);
  153. return false;
  154. }
  155. try
  156. {
  157. // 5.连接db数据库;
  158. if( g_dwDBServerPort != 0)
  159. sprintf(g_szConnectString,"driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s",
  160. g_szDBSource,g_dwDBServerPort,g_szDBName,g_szDBAccount,g_szDBPassWord);
  161. else
  162. sprintf(g_szConnectString,"driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s",
  163. g_szDBSource,g_szDBName,g_szDBAccount,g_szDBPassWord);
  164. g_db.OpenEx(g_szConnectString,CDatabase::noOdbcDialog);
  165. bUserDb=1;
  166. g_curdb=&g_db;
  167. // 6.
  168. BakData(savedir, 1, 0); // Jeff. savedir=\\数据备份
  169. g_masterdb.Close();
  170. CString sql;
  171. sql="create table [historydb]([year] varchar(50))";
  172. g_db.ExecuteSQL (sql);
  173. }
  174. catch(CDBException * e)
  175. {
  176. e->Delete();
  177. }
  178. if(bUserDb==0 && bMasterDb)
  179. {
  180. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  181. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  182. WriteLogin("数据库初始化中...");
  183. TCHAR szDBFile[MAX_PATH] = _T("");
  184. sprintf(szDBFile,"%s\\数据\\%s.mdf",g_mainpath,g_szDBName);
  185. if(::PathFileExists(szDBFile) == 0 )
  186. {
  187. WriteLogin("数据库文件丢失!");
  188. return false;
  189. }
  190. BOOL bLog=0;
  191. TCHAR szDBLogFile[MAX_PATH] = _T("");
  192. sprintf(szDBLogFile,"%s\\数据\\%s_log.ldf",g_mainpath,g_szDBName);
  193. if(::PathFileExists(szDBLogFile) == 0 )
  194. {
  195. bLog=1;
  196. }
  197. CString sql;
  198. try
  199. {
  200. // g_masterdb.ExecuteSQL ("exec sp_detach_db db");
  201. }
  202. catch(CDBException * e)
  203. {
  204. // WriteLogin(e->m_strError+"xxx");
  205. e->Delete();
  206. }
  207. if(bLog)
  208. 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);
  209. else
  210. sql.Format("exec sp_attach_db '%s',@filename1='%s\\数据\\%s.mdf'", g_szDBName,g_ModulePath,g_szDBName);
  211. try
  212. {
  213. g_masterdb.ExecuteSQL (sql); // 同master数据库去执行附加和分离;
  214. }
  215. catch(CDBException * e)
  216. {
  217. g_masterdb.Close();
  218. WriteLogin(e->m_strError);
  219. WriteLogin("数据库初始化失败!");
  220. e->Delete();
  221. return false;
  222. }
  223. g_masterdb.Close();
  224. WriteLogin("数据库初始化完成,请重新启动系统!");
  225. return false;
  226. }
  227. ULONG_PTR gdiplusToken;
  228. GdiplusStartupInput gdiplusStartupInput;
  229. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  230. CString path1="客户原片(管理软件)$";
  231. CString path2="修好的片(管理软件)$";
  232. CString path3="设计好的片(管理软件)$";
  233. CString path4="精修好的片(管理软件)$";
  234. CString path5="礼服图片(管理软件)$";
  235. CString path6="电话录音(管理软件)$";
  236. CString temp;
  237. if(CheckFolderFileExist (g_mainpath+"\\"+path1))
  238. {
  239. temp.Format ("net share %s=%s\\%s", path1, g_mainpath, path1);
  240. WinExec(temp,SW_HIDE);
  241. }
  242. if(CheckFolderFileExist (g_mainpath+"\\"+path2))
  243. {
  244. temp.Format ("net share %s=%s\\%s", path2, g_mainpath, path2);
  245. WinExec(temp,SW_HIDE);
  246. }
  247. if(CheckFolderFileExist (g_mainpath+"\\"+path3))
  248. {
  249. temp.Format ("net share %s=%s\\%s", path3, g_mainpath, path3);
  250. WinExec(temp,SW_HIDE);
  251. }
  252. if(CheckFolderFileExist (g_mainpath+"\\"+path4))
  253. {
  254. temp.Format ("net share %s=%s\\%s", path4, g_mainpath, path4);
  255. WinExec(temp,SW_HIDE);
  256. }
  257. if(CheckFolderFileExist (g_mainpath+"\\"+path5))
  258. {
  259. temp.Format ("net share %s=%s\\%s", path5, g_mainpath, path5);
  260. WinExec(temp,SW_HIDE);
  261. }
  262. if(CheckFolderFileExist (g_mainpath+"\\"+path6))
  263. {
  264. temp.Format ("net share %s=%s\\%s", path6, g_mainpath, path6);
  265. WinExec(temp,SW_HIDE);
  266. }
  267. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  268. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  269. #ifdef ENTERPRISE_VERSION
  270. autorunpath=g_mainpath+"\\DataSyncReceive.exe";
  271. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  272. autorunpath=g_mainpath+"\\DataSyncSend.exe";
  273. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  274. autorunpath=g_mainpath+"\\CPhotoFTPReceive.exe";
  275. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  276. autorunpath=g_mainpath+"\\CPhotoFTPSend.exe";
  277. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  278. #endif
  279. // Standard initialization
  280. // If you are not using these features and wish to reduce the size
  281. // of your final executable, you should remove from the following
  282. // the specific initialization routines you do not need.
  283. #ifdef _AFXDLL
  284. Enable3dControls(); // Call this when using MFC in a shared DLL
  285. #else
  286. Enable3dControlsStatic(); // Call this when linking to MFC statically
  287. #endif
  288. InitCommonControls();
  289. CDBServerDlg dlg;
  290. m_pMainWnd = &dlg;
  291. int nResponse = dlg.DoModal();
  292. if (nResponse == IDOK)
  293. {
  294. }
  295. else if (nResponse == IDCANCEL)
  296. {
  297. }
  298. // Transport_UnInit();
  299. GdiplusShutdown(gdiplusToken);
  300. return FALSE;
  301. }
  302. void CDBServer::OnAppShow()
  303. {
  304. if (m_pMainWnd)
  305. m_pMainWnd->ShowWindow(SW_SHOW);
  306. }
  307. void CDBServer::OnAppExit()
  308. {
  309. m_pMainWnd->DestroyWindow();
  310. }
  311. void CDBServer::OnAppAbout()
  312. {
  313. }
  314. /********************************************************************/
  315. /* */
  316. /* Function name : OnHelpIndex */
  317. /* Description : Command to show help file. */
  318. /* */
  319. /********************************************************************/
  320. void CDBServer::OnHelpIndex()
  321. {
  322. // launch help
  323. // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L);
  324. }
  325. //extern CString g_bakinfo;