UPhoneBox.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // UPhoneBox.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "UPhoneBox.h"
  5. #include "UPhoneBoxDlg.h"
  6. #include "DBConfig.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. //*****全局变量*******//
  13. CString g_headerpath;
  14. CString g_server2;
  15. CString g_ip;
  16. BOOL g_bSendOK = 0;
  17. SENDHEAD g_sendhead;
  18. COLORREF g_gridcol1 = RGB(204, 204, 204);
  19. COLORREF g_gridcol2 = RGB(190, 190, 190);
  20. int clr3DHilight = RGB(220, 220, 220);
  21. int clr3DShadow = RGB(70, 70, 70);
  22. int clr3DFace = RGB(128, 128, 128);
  23. CString g_mainpath; // 程序所在目录;
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CUPhoneBoxApp
  26. BEGIN_MESSAGE_MAP(CUPhoneBoxApp, CWinApp)
  27. //{{AFX_MSG_MAP(CUPhoneBoxApp)
  28. // NOTE - the ClassWizard will add and remove mapping macros here.
  29. // DO NOT EDIT what you see in these blocks of generated code!
  30. //}}AFX_MSG
  31. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CUPhoneBoxApp construction
  35. BOOL isInnerIP(DWORD a_ip)
  36. {
  37. BOOL bValid = 0;
  38. if ((a_ip >> 24 == 0xa) || (a_ip >> 16 == 0xc0a8) || (a_ip >> 22 == 0x2b0))
  39. {
  40. bValid = 1;
  41. }
  42. return bValid;
  43. }
  44. void MyGetIPByName(CString &name)
  45. {
  46. CStringArray g_serverarray;
  47. CString strIP = name;
  48. name.Empty();
  49. DWORD dwServerIP = 0;
  50. HOSTENT *host = gethostbyname(strIP);
  51. struct in_addr addr;
  52. if (host != NULL)
  53. {
  54. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  55. {
  56. memset(&addr, 0, sizeof(addr));
  57. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  58. dwServerIP = ntohl(addr.S_un.S_addr);
  59. BYTE* pIP = (BYTE*)&dwServerIP;
  60. name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  61. g_serverarray.Add(name);
  62. }
  63. }
  64. while (g_serverarray.GetSize() > 1)
  65. {
  66. CString name;
  67. BOOL bFind = 0;
  68. for (int i = 0; i < g_serverarray.GetSize(); i++)
  69. {
  70. DWORD dwServerIP = inet_addr(g_serverarray.ElementAt(i));
  71. dwServerIP = htonl(dwServerIP);//加了才正确
  72. if (isInnerIP(dwServerIP) == 0)
  73. {
  74. // AfxMessageBox("外部IP");
  75. g_serverarray.RemoveAt(i);
  76. bFind = 1;
  77. break;
  78. }
  79. // else
  80. // AfxMessageBox("内部IP");
  81. }
  82. if (bFind == 0)
  83. {
  84. break;
  85. }
  86. }
  87. name = "";
  88. if (g_serverarray.GetSize())name = g_serverarray.ElementAt(0);
  89. }
  90. CUPhoneBoxApp::CUPhoneBoxApp()
  91. {
  92. // TODO: add construction code here,
  93. // Place all significant initialization in InitInstance
  94. }
  95. /////////////////////////////////////////////////////////////////////////////
  96. // The one and only CUPhoneBoxApp object
  97. CUPhoneBoxApp theApp;
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CUPhoneBoxApp initialization
  100. BOOL CUPhoneBoxApp::InitInstance()
  101. {
  102. HANDLE hObject = CreateMutex(NULL, FALSE, _T("CUPhoneBoxAppXiao"));
  103. if (GetLastError() == ERROR_ALREADY_EXISTS)
  104. {
  105. return false;
  106. }
  107. AfxEnableControlContainer();
  108. // Standard initialization
  109. // If you are not using these features and wish to reduce the size
  110. // of your final executable, you should remove from the following
  111. // the specific initialization routines you do not need.
  112. char path[MAX_PATH];
  113. ::GetModuleFileName(NULL, path, MAX_PATH);
  114. g_mainpath = path;
  115. g_mainpath = g_mainpath.Left(g_mainpath.ReverseFind('\\'));
  116. CString configpath = g_mainpath + "\\dbconfig.dat";
  117. int nResult = Transport_Init();
  118. if (TRANSPORT_OK != nResult)
  119. {
  120. WriteLogin("初始化网络环境失败!");
  121. return false;
  122. }
  123. GetSysIniInfo();
  124. CFile fp;
  125. ll:
  126. if (fp.Open(configpath, CFile::modeRead))
  127. {
  128. DBSAVE db;
  129. memset(&db, 0, sizeof(db));
  130. fp.Read(&db, sizeof(db));
  131. fp.Close();
  132. char server[50];
  133. char user[50];
  134. char psw[50];
  135. memset(server, 0, sizeof(char) * 50);
  136. memset(user, 0, sizeof(char) * 50);
  137. memset(psw, 0, sizeof(char) * 50);
  138. for (int i = 0; i < 50; i++)
  139. {
  140. if (db.server[i])
  141. server[i] = 500 - db.server[i];
  142. if (db.user[i])
  143. user[i] = 500 - db.user[i];
  144. if (db.psw[i])
  145. psw[i] = 500 - db.psw[i];
  146. }
  147. g_server2 = server;
  148. g_headerpath = "\\\\" + g_server2 + "\\客户原片(管理软件)$";
  149. g_headerpath += "\\客人头像\\";
  150. g_ip = g_server2;
  151. MyGetIPByName(g_ip);
  152. }
  153. else
  154. {
  155. AfxMessageBox("请先设置数据库服务器!", MB_ICONINFORMATION);
  156. DBConfig dlg;
  157. dlg.DoModal();
  158. goto ll;
  159. return false;
  160. }
  161. WriteLogin("启动");
  162. //ShellExecute(NULL, _T("open"), g_mainpath + "\\AutoRunUPhoneBox.exe", NULL, NULL, SW_SHOWNORMAL);
  163. #ifdef _AFXDLL
  164. Enable3dControls(); // Call this when using MFC in a shared DLL
  165. #else
  166. Enable3dControlsStatic(); // Call this when linking to MFC statically
  167. #endif
  168. SetDialogBkColor(RGB(204, 204, 204), RGB(0, 0, 0));
  169. SetRegistryKey(_T("LYFZ-UPhoneBox"));
  170. ULONG_PTR gdiplusToken;
  171. GdiplusStartupInput gdiplusStartupInput;
  172. GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  173. CUPhoneBoxDlg dlg;
  174. m_pMainWnd = &dlg;
  175. int nResponse = dlg.DoModal();
  176. if (nResponse == IDOK)
  177. {
  178. // TODO: Place code here to handle when the dialog is
  179. // dismissed with OK
  180. }
  181. else if (nResponse == IDCANCEL)
  182. {
  183. // TODO: Place code here to handle when the dialog is
  184. // dismissed with Cancel
  185. }
  186. GdiplusShutdown(gdiplusToken);
  187. // Since the dialog has been closed, return FALSE so that we exit the
  188. // application, rather than start the application's message pump.
  189. return FALSE;
  190. }
  191. void WriteLogin(CString str)
  192. {
  193. CStdioFile fp;
  194. CString path = g_mainpath + "\\来电精灵日志.txt";
  195. if (::PathFileExists(path))
  196. {
  197. fp.Open(path, CFile::modeWrite);
  198. int length = fp.GetLength();
  199. if (length > 5 * 1024 * 1024)
  200. {
  201. fp.Close();
  202. ::DeleteFile(path);
  203. return;
  204. }
  205. fp.SeekToEnd();
  206. }
  207. else
  208. fp.Open(path, CFile::modeCreate | CFile::modeWrite);
  209. fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d %H:%M:%S") + " " + str + "\r\n");
  210. fp.Close();
  211. }
  212. int GetLengthEx(CString str)
  213. {
  214. int leng = 0;
  215. TBYTE ucHigh, ucLow;
  216. for (int i = 0; i < str.GetLength(); i++)
  217. {
  218. if ((TBYTE)str[i] < 0x80)
  219. {
  220. leng++;
  221. continue;
  222. }
  223. ucHigh = (TBYTE)str[i];
  224. if (i + 1 == str.GetLength())
  225. {
  226. leng++;
  227. break;
  228. }
  229. ucLow = (TBYTE)str[i + 1];
  230. if (ucHigh < 0xa1 || ucLow < 0xa1)
  231. {
  232. leng++;
  233. continue;
  234. }
  235. leng++;
  236. i++;
  237. }
  238. return leng;
  239. }