DBServer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. WriteTextLog("未找到配置文件: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. WriteTextLog("启动");
  77. CString savedir=g_mainpath+"\\数据备份";
  78. if(!CheckFolderFileExist (savedir))
  79. ::CreateDirectory (savedir, NULL);
  80. ::CoInitialize( NULL );
  81. BOOL bMasterDb=0;
  82. BOOL bUserDb=0;
  83. // 4.连接master数据库,判断数据库连接是否正常;
  84. CDatabase g_masterdb; // Jeff:master用于附加与分离数据库;
  85. try
  86. {
  87. #if 0
  88. CString strCon;
  89. strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=master;%s"),g_strdbServrename,g_strdbpsw);//Jeff.使用可配置数据库信息方式;
  90. g_masterdb.OpenEx(strCon,CDatabase::noOdbcDialog);
  91. #else
  92. TCHAR szConnectString[MAX_PATH] = _T("");
  93. if( g_dwDBServerPort != 0)
  94. sprintf(szConnectString,
  95. "driver={SQL Server};Server=%s,%d;database=master;uid=%s;pwd=%s",
  96. g_szDBSource,g_dwDBServerPort,g_szDBAccount,g_szDBPassWord);
  97. else
  98. sprintf(szConnectString,
  99. "driver={SQL Server};Server=%s;database=master;uid=%s;pwd=%s",
  100. g_szDBSource,g_szDBAccount,g_szDBPassWord);
  101. g_masterdb.OpenEx(szConnectString,CDatabase::noOdbcDialog);
  102. #endif
  103. bMasterDb=1;
  104. }
  105. catch(CDBException * e)
  106. {
  107. e->Delete();
  108. }
  109. if(bMasterDb==0)
  110. {
  111. WriteTextLog("sql数据库未启动!");
  112. ShellExecute(NULL, _T("open"), g_mainpath+"\\AutoRun.exe", NULL, NULL, SW_HIDE);
  113. return false;
  114. }
  115. try
  116. {
  117. // 5.连接db数据库;
  118. #if 0
  119. CString strCon;
  120. strCon.Format(_T("driver={SQL Server};Server=%s;DATABASE=db;%s"),g_strdbServrename,g_strdbpsw);
  121. g_db.OpenEx(strCon,CDatabase::noOdbcDialog);
  122. #else
  123. if( g_dwDBServerPort != 0)
  124. sprintf(g_szConnectString,"driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s",
  125. g_szDBSource,g_dwDBServerPort,g_szDBName,g_szDBAccount,g_szDBPassWord);
  126. else
  127. sprintf(g_szConnectString,"driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s",
  128. g_szDBSource,g_szDBName,g_szDBAccount,g_szDBPassWord);
  129. g_db.OpenEx(g_szConnectString,CDatabase::noOdbcDialog);
  130. #endif
  131. bUserDb=1;
  132. g_curdb=&g_db;
  133. // 6.
  134. BakData(savedir, 1, 0); // Jeff. savedir=\\数据备份
  135. g_masterdb.Close();
  136. CString sql;
  137. sql="create table [historydb]([year] varchar(50))";
  138. g_db.ExecuteSQL (sql);
  139. }
  140. catch(CDBException * e)
  141. {
  142. /* AfxMessageBox(e->m_strError);
  143. e->Delete();
  144. WriteLogin("sql数据库打开失败!");
  145. return false;*/
  146. e->Delete();
  147. }
  148. if(bUserDb==0 && bMasterDb)
  149. {
  150. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  151. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  152. WriteTextLog("数据库初始化中...");
  153. #if 0 // Jeff.deal
  154. if(::PathFileExists(g_mainpath+"\\数据\\db.mdf")==0 )
  155. #else
  156. TCHAR szDBFile[MAX_PATH] = _T("");
  157. sprintf(szDBFile,"%s\\数据\\%s.mdf",g_mainpath,g_szDBName);
  158. if(::PathFileExists(szDBFile) == 0 )
  159. #endif
  160. {
  161. WriteTextLog("数据库文件丢失!");
  162. return false;
  163. }
  164. BOOL bLog=0;
  165. #if 0
  166. if( ::PathFileExists(g_mainpath+"\\数据\\db_log.LDF"))
  167. #else
  168. TCHAR szDBLogFile[MAX_PATH] = _T("");
  169. sprintf(szDBLogFile,"%s\\数据\\%s_log.ldf",g_mainpath,g_szDBName);
  170. if(::PathFileExists(szDBLogFile) == 0 )
  171. #endif
  172. {
  173. bLog=1;
  174. }
  175. CString sql;
  176. try
  177. {
  178. // g_masterdb.ExecuteSQL ("exec sp_detach_db db");
  179. }
  180. catch(CDBException * e)
  181. {
  182. // WriteLogin(e->m_strError+"xxx");
  183. e->Delete();
  184. }
  185. #if 0 // Jeff.deal
  186. if(bLog) sql.Format("exec sp_attach_db 'db',@filename1='%s',@filename2='%s'", g_mainpath+"\\数据\\db.mdf", g_mainpath+"\\数据\\db_log.LDF");
  187. else sql.Format("exec sp_attach_db 'db',@filename1='%s'", g_mainpath+"\\数据\\db.mdf");
  188. #else
  189. if(bLog)
  190. 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);
  191. else
  192. sql.Format("exec sp_attach_db '%s',@filename1='%s\\数据\\%s.mdf'", g_szDBName,g_ModulePath,g_szDBName);
  193. #endif
  194. try
  195. {
  196. g_masterdb.ExecuteSQL (sql); // 同master数据库去执行附加和分离;
  197. }
  198. catch(CDBException * e)
  199. {
  200. g_masterdb.Close();
  201. WriteTextLog(e->m_strError);
  202. WriteTextLog("数据库初始化失败!");
  203. e->Delete();
  204. return false;
  205. }
  206. g_masterdb.Close();
  207. WriteTextLog("数据库初始化完成,请重新启动系统!");
  208. return false;
  209. }
  210. /* int nResult = Transport_Init();
  211. if( TRANSPORT_OK != nResult)
  212. {
  213. WriteLogin("网络初始化失败!");
  214. return 0;
  215. }*/
  216. #if 1
  217. // Jeff.在数据库完成初始化后,判断本地目录下是否有MSCHRT20.OCX文件,无则从数据库下载;2014.09.06
  218. // 客户端从dwToUserID==211里传递MSCHRT20.OCX;
  219. CString strMSCHRTOCX = g_mainpath+"\\系统文件\\MSCHRT20.OCX";
  220. OFSTRUCT ofStruct;
  221. OpenFile(strMSCHRTOCX,&ofStruct,OF_EXIST);
  222. if( ERROR_FILE_NOT_FOUND == GetLastError())
  223. {
  224. CRstUpdate rsSt;
  225. rsSt.m_pDatabase = &g_db;
  226. rsSt.Open();
  227. if(!rsSt.IsEOF())
  228. {
  229. CFile fp;
  230. if(fp.Open(strMSCHRTOCX, CFile::modeCreate|CFile::modeWrite))
  231. {
  232. void *pData = GlobalLock(rsSt.m_lbOCX.m_hData);
  233. fp.Write(pData, rsSt.m_lbOCX.m_dwDataLength);
  234. fp.Close();
  235. GlobalUnlock(rsSt.m_lbOCX.m_hData);
  236. }
  237. }
  238. rsSt.Close();
  239. }
  240. #endif
  241. ULONG_PTR gdiplusToken;
  242. GdiplusStartupInput gdiplusStartupInput;
  243. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  244. CString path1="客户原片(管理软件)$";
  245. CString path2="修好的片(管理软件)$";
  246. CString path3="设计好的片(管理软件)$";
  247. CString path4="精修好的片(管理软件)$";
  248. CString path5="礼服图片(管理软件)$";
  249. CString path6="电话录音(管理软件)$";
  250. CString temp;
  251. if(CheckFolderFileExist (g_mainpath+"\\"+path1))
  252. {
  253. temp.Format ("net share %s=%s\\%s", path1, g_mainpath, path1);
  254. WinExec(temp,SW_HIDE);
  255. }
  256. if(CheckFolderFileExist (g_mainpath+"\\"+path2))
  257. {
  258. temp.Format ("net share %s=%s\\%s", path2, g_mainpath, path2);
  259. WinExec(temp,SW_HIDE);
  260. }
  261. if(CheckFolderFileExist (g_mainpath+"\\"+path3))
  262. {
  263. temp.Format ("net share %s=%s\\%s", path3, g_mainpath, path3);
  264. WinExec(temp,SW_HIDE);
  265. }
  266. if(CheckFolderFileExist (g_mainpath+"\\"+path4))
  267. {
  268. temp.Format ("net share %s=%s\\%s", path4, g_mainpath, path4);
  269. WinExec(temp,SW_HIDE);
  270. }
  271. if(CheckFolderFileExist (g_mainpath+"\\"+path5))
  272. {
  273. temp.Format ("net share %s=%s\\%s", path5, g_mainpath, path5);
  274. WinExec(temp,SW_HIDE);
  275. }
  276. if(CheckFolderFileExist (g_mainpath+"\\"+path6))
  277. {
  278. temp.Format ("net share %s=%s\\%s", path6, g_mainpath, path6);
  279. WinExec(temp,SW_HIDE);
  280. }
  281. CString autorunpath=g_mainpath+"\\AutoRun.exe";
  282. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  283. #ifdef ENTERPRISE_VERSION
  284. autorunpath=g_mainpath+"\\DataSyncReceive.exe";
  285. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  286. autorunpath=g_mainpath+"\\DataSyncSend.exe";
  287. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  288. autorunpath=g_mainpath+"\\CPhotoFTPReceive.exe";
  289. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  290. autorunpath=g_mainpath+"\\CPhotoFTPSend.exe";
  291. ShellExecute(NULL, _T("open"), autorunpath, NULL, NULL, SW_HIDE);
  292. #endif
  293. // Standard initialization
  294. // If you are not using these features and wish to reduce the size
  295. // of your final executable, you should remove from the following
  296. // the specific initialization routines you do not need.
  297. #ifdef _AFXDLL
  298. Enable3dControls(); // Call this when using MFC in a shared DLL
  299. #else
  300. Enable3dControlsStatic(); // Call this when linking to MFC statically
  301. #endif
  302. InitCommonControls();
  303. CDBServerDlg dlg;
  304. m_pMainWnd = &dlg;
  305. int nResponse = dlg.DoModal();
  306. if (nResponse == IDOK)
  307. {
  308. }
  309. else if (nResponse == IDCANCEL)
  310. {
  311. }
  312. // Transport_UnInit();
  313. GdiplusShutdown(gdiplusToken);
  314. return FALSE;
  315. }
  316. void CDBServer::OnAppShow()
  317. {
  318. if (m_pMainWnd)
  319. m_pMainWnd->ShowWindow(SW_SHOW);
  320. }
  321. void CDBServer::OnAppExit()
  322. {
  323. m_pMainWnd->DestroyWindow();
  324. }
  325. void CDBServer::OnAppAbout()
  326. {
  327. }
  328. /********************************************************************/
  329. /* */
  330. /* Function name : OnHelpIndex */
  331. /* Description : Command to show help file. */
  332. /* */
  333. /********************************************************************/
  334. void CDBServer::OnHelpIndex()
  335. {
  336. // launch help
  337. // ::WinHelp(AfxGetMainWnd()->m_hWnd, AfxGetApp()->m_pszHelpFilePath, HELP_CONTENTS, 0L);
  338. }