PublicFun.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:11k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FileName    :   PublicFun.cpp
  4. //  Version     :   1.0
  5. //  Creater     :   Cheng Bitao
  6. //  Date        :   2002-4-8 16:42:22
  7. //  Comment     :   
  8. //
  9. //////////////////////////////////////////////////////////////////////////////////////
  10. #include "stdafx.h"
  11. #include "PublicFun.h"
  12. #include "Direct.h"
  13. #include "winsvc.h"
  14. #include "KString.h"
  15. void AddPathChar(char szPath[])
  16. {
  17.     int nPathLen = strlen(szPath);
  18.     if (szPath[nPathLen - 1] != '\')
  19.         memcpy(&szPath[nPathLen], "\", 2);
  20. }
  21. void AddPathChar(char szPath[], char chPathChar)
  22. {
  23.     int nPathLen = 0;
  24.   
  25.     //ASSERT(*szPath);
  26. if (szPath[0] == '')
  27. return;
  28.     nPathLen = strlen(szPath);
  29.     if (szPath[nPathLen - 1] != chPathChar)
  30.     {
  31.         szPath[nPathLen] = chPathChar;
  32.         szPath[nPathLen + 1] = '';
  33.     }
  34. }
  35. void MkDirEx(const char cszPathDir[])
  36. {
  37.     char szDir[MAX_PATH]    = {0};
  38.     char szTemp[MAX_PATH]   = {0};
  39.     char *pszPos            = NULL;
  40.     char *pszTemp           = NULL;
  41.     int nLen                = 0;
  42.     
  43.     strcpy(szTemp, cszPathDir);
  44.     pszPos = strchr(szTemp, '\');
  45.     pszTemp = szTemp;
  46. while(pszPos != NULL)
  47. {
  48. if (*szDir == '')
  49.             strncpy(szDir, pszTemp, pszPos - pszTemp);
  50.         else
  51.             strncat(szDir, pszTemp, pszPos - pszTemp);
  52.         pszTemp = pszPos + 1;        
  53.         nLen = strlen(szDir);
  54. if (szDir[nLen - 1] != ':')//such as "c:"
  55. _mkdir(szDir);
  56. strcat(szDir, "\");
  57. pszPos = strchr(pszTemp, '\');
  58. }
  59.     if (*szDir == '')
  60.         strcpy(szDir, pszTemp);
  61.     else
  62.         strcat(szDir, pszTemp);
  63. _mkdir(szDir);
  64. }
  65.  
  66. // Check file exists
  67. int FileExists(LPCTSTR lpszFileName)
  68. {
  69.     int nResult                     = false;
  70.     HANDLE hFind                    = INVALID_HANDLE_VALUE;
  71.     WIN32_FIND_DATA *pFindFileData  = NULL;
  72.     pFindFileData = new WIN32_FIND_DATA;
  73.     KAV_PROCESS_ERROR(pFindFileData);
  74.     hFind = ::FindFirstFile(lpszFileName, pFindFileData);
  75.     if (INVALID_HANDLE_VALUE != hFind)
  76.     {
  77.         if (!((pFindFileData->dwFileAttributes) & FILE_ATTRIBUTE_DIRECTORY))
  78.             nResult = true;
  79.      
  80.         ::FindClose(hFind);
  81.         hFind = INVALID_HANDLE_VALUE;
  82.     }
  83. Exit0:
  84.     KAV_DELETE(pFindFileData);
  85.     return nResult;
  86. }
  87. int CopyDir(const char cszSource[], const char cszDest[], int nFailedIfExist)
  88. {
  89.     WIN32_FIND_DATA FindFileData;
  90.     HANDLE Handle;
  91.     char szSource[MAX_PATH];
  92.     char szSourceFile[MAX_PATH];
  93.     char szDest[MAX_PATH];
  94.     strcpy(szSource, cszSource);
  95.     AddPathChar(szSource, '\');
  96.     strcat(szSource, "*.*");
  97.     MkDirEx(cszDest);
  98.     Handle = ::FindFirstFile(szSource, &FindFileData);
  99.     if (Handle != INVALID_HANDLE_VALUE)
  100.     {
  101.         do
  102.         {   
  103.             strcpy(szDest, cszDest);
  104.             AddPathChar(szDest);
  105.             strcat(szDest, FindFileData.cFileName);
  106.             strcpy(szSourceFile, cszSource);
  107.             AddPathChar(szSourceFile);
  108.             strcat(szSourceFile, FindFileData.cFileName);
  109.             if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  110.             {
  111.                 if ((strcmp(FindFileData.cFileName, ".") != 0) && (strcmp(FindFileData.cFileName, "..") != 0))
  112.                     CopyDir(szSourceFile, szDest, nFailedIfExist);
  113.                 continue;
  114.             }                   
  115.             ::CopyFile(szSourceFile, szDest, nFailedIfExist);            
  116.         }
  117.         while(::FindNextFile(Handle, &FindFileData));
  118.         ::FindClose(Handle);
  119.     }
  120.     return true;
  121. }
  122. // Return  1 : Exist process and exit successfully
  123. //         0 : Exist process but exit failed
  124. //        -1 : Not exist process
  125. int StopAService(const char cszServiceName[])
  126. {
  127.     int nResult                 = -1;
  128.     SC_HANDLE SCMgrHandle       = NULL;
  129.     SC_HANDLE SCServiceHandle   = NULL;
  130.     SERVICE_STATUS ssStatus;
  131.     DWORD dwErrorCode           = 0;
  132. DWORD dwOldCheckPoint; 
  133.     DWORD dwStartTickCount;
  134.     DWORD dwWaitTime;
  135.     SCMgrHandle = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
  136.     KAV_PROCESS_ERROR(SCMgrHandle);
  137.     SCServiceHandle = OpenService(SCMgrHandle, cszServiceName, SC_MANAGER_ALL_ACCESS);
  138.     if (!SCServiceHandle)
  139.     {
  140.         dwErrorCode = GetLastError();
  141.         if ((dwErrorCode == ERROR_SERVICE_DOES_NOT_EXIST) || (dwErrorCode == ERROR_INVALID_NAME))
  142.             nResult = -1;
  143.         goto Exit0;
  144.     }
  145.     if (!ControlService(SCServiceHandle, SERVICE_CONTROL_STOP, &ssStatus))
  146.     {
  147. nResult = 0;
  148. goto Exit0;
  149. }
  150. // Check the status until the service is no longer start pending. 
  151.     if (!QueryServiceStatus( 
  152. SCServiceHandle,   // handle to service 
  153. &ssStatus) 
  154. )  // address of status information structure
  155.     {
  156. nResult = 0;
  157. goto Exit0;
  158. }
  159.  
  160.     // Save the tick count and initial checkpoint.
  161.     dwStartTickCount = GetTickCount();
  162.     dwOldCheckPoint = ssStatus.dwCheckPoint;
  163.     while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) 
  164.     { 
  165.         // Do not wait longer than the wait hint. A good interval is 
  166.         // one tenth the wait hint, but no less than 1 second and no 
  167.         // more than 10 seconds. 
  168.  
  169.         dwWaitTime = ssStatus.dwWaitHint / 10;
  170.         if(dwWaitTime < 1000)
  171.             dwWaitTime = 1000;
  172.         else if (dwWaitTime > 10000)
  173.             dwWaitTime = 10000;
  174.         Sleep(dwWaitTime);
  175.         // Check the status again. 
  176.  
  177.         if (!QueryServiceStatus( 
  178.             SCServiceHandle,   // handle to service 
  179.             &ssStatus)
  180. )  // address of structure
  181.             break; 
  182.  
  183.         if (ssStatus.dwCheckPoint > dwOldCheckPoint)
  184.         {
  185.             // The service is making progress.
  186.             dwStartTickCount = GetTickCount();
  187.             dwOldCheckPoint = ssStatus.dwCheckPoint;
  188.         }
  189.         else
  190.         {
  191.             if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
  192.             {
  193.                 // No progress made within the wait hint
  194.                 break;
  195.             }
  196.         }
  197.     } 
  198.     if (ssStatus.dwCurrentState == SERVICE_STOPPED) 
  199. nResult = 1;
  200. else 
  201. nResult = 0;
  202. Exit0:
  203.     if (SCServiceHandle)
  204.     {
  205.         CloseServiceHandle(SCServiceHandle);
  206.         SCServiceHandle = NULL;
  207.     }
  208.     if (SCMgrHandle)
  209.     {
  210.         CloseServiceHandle(SCMgrHandle);
  211.         SCMgrHandle = NULL;
  212.     }
  213.     return nResult;
  214. }
  215. int StartAService(const char cszServiceName[])
  216. {
  217.     int nResult                 = false;
  218. int nRetCode = 0;
  219.     SC_HANDLE SCMgrHandle       = NULL;
  220.     SC_HANDLE SCServiceHandle   = NULL;
  221.     SERVICE_STATUS ssStatus;
  222. DWORD dwOldCheckPoint; 
  223.     DWORD dwStartTickCount;
  224.     DWORD dwWaitTime;
  225.     
  226.     SCMgrHandle = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
  227.     KAV_PROCESS_ERROR(SCMgrHandle);
  228.     SCServiceHandle = OpenService(SCMgrHandle, cszServiceName, SC_MANAGER_ALL_ACCESS);
  229.     KAV_PROCESS_ERROR(SCServiceHandle);
  230.     nRetCode = StartService(SCServiceHandle, 0, NULL);    
  231. KAV_PROCESS_ERROR(nRetCode);
  232. // Check the status until the service is no longer start pending. 
  233.     if (!QueryServiceStatus( 
  234. SCServiceHandle,   // handle to service 
  235.         &ssStatus)
  236. )  // address of status information structure
  237.         goto Exit0;
  238.  
  239.     // Save the tick count and initial checkpoint.
  240.     dwStartTickCount = GetTickCount();
  241.     dwOldCheckPoint = ssStatus.dwCheckPoint;
  242.     while (ssStatus.dwCurrentState == SERVICE_START_PENDING) 
  243.     { 
  244.         // Do not wait longer than the wait hint. A good interval is 
  245.         // one tenth the wait hint, but no less than 1 second and no 
  246.         // more than 10 seconds. 
  247.  
  248.         dwWaitTime = ssStatus.dwWaitHint / 10;
  249.         if(dwWaitTime < 1000)
  250.             dwWaitTime = 1000;
  251.         else if (dwWaitTime > 10000)
  252.             dwWaitTime = 10000;
  253.         Sleep(dwWaitTime);
  254.         // Check the status again. 
  255.  
  256.         if (!QueryServiceStatus( 
  257.             SCServiceHandle,   // handle to service 
  258.             &ssStatus) 
  259. )  // address of structure
  260.             break; 
  261.  
  262.         if (ssStatus.dwCheckPoint > dwOldCheckPoint)
  263.         {
  264.             // The service is making progress.
  265.             dwStartTickCount = GetTickCount();
  266.             dwOldCheckPoint = ssStatus.dwCheckPoint;
  267.         }
  268.         else
  269.         {
  270.             if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
  271.             {
  272.                 // No progress made within the wait hint
  273.                 break;
  274.             }
  275.         }
  276.     } 
  277.     if (ssStatus.dwCurrentState == SERVICE_RUNNING) 
  278. nResult = true;
  279. Exit0:
  280.     if (SCServiceHandle)
  281.     {
  282.         CloseServiceHandle(SCServiceHandle);
  283.         SCServiceHandle = NULL;
  284.     }
  285.     if (SCMgrHandle)
  286.     {
  287.         CloseServiceHandle(SCMgrHandle);
  288.         SCMgrHandle = NULL;
  289.     }
  290.     return nResult;
  291. }
  292. //Commented by LiuHaifeng
  293. //When compile KavRunPlus, we must comment following
  294. int GetExitCommandLineParameter()
  295. {
  296. int nResult  = false;
  297.     int nRetCode = false;
  298.     CWinApp *pWinApp    = NULL;
  299.     char *pszParam      = NULL;
  300.     char *pszParamSet   = NULL;
  301.     char *pszPos        = NULL;
  302.     
  303.     pWinApp = ::AfxGetApp();
  304.     if (pWinApp == NULL)
  305.         goto Exit0;
  306.     
  307.     pszParamSet = pWinApp->m_lpCmdLine;
  308.     
  309.     while (pszParamSet)
  310.     {
  311.         pszParam = pszParamSet;
  312.         
  313.         pszPos = strchr(pszParamSet, ' ');
  314.         if (pszPos)
  315.         {
  316.             *pszPos = '';
  317.             pszParamSet = pszPos + 1; 
  318.         }
  319.         else 
  320.         {
  321.             pszParamSet = NULL;
  322.         }
  323.         
  324.         if ((*pszParam == '-') || (*pszParam == '/'))
  325.         {
  326.             pszParam++;
  327.             
  328.             nRetCode = stricmp(pszParam, "QUIT");
  329.             if (!nRetCode)
  330.             {
  331.                 nResult = TRUE;
  332.                 
  333.                 break;
  334.             }
  335.         }
  336.     }
  337.     
  338. Exit0:
  339.     
  340.     return nResult;
  341. }
  342. void StrAddStr(char szStr1[], const char cszStr2[])
  343. {
  344. char szTemp1[2048] = {0};
  345. char szTemp2[2048] = {0};
  346. strcpy(szTemp1, szStr1);
  347. strcpy(szTemp2, cszStr2);
  348. //Commented by LiuHaifeng
  349.     //When compile KavRunPlus, we must comment following
  350.     StrUpper(szTemp1);
  351. StrUpper(szTemp2);
  352. if (strstr(szTemp1, szTemp2) == NULL)
  353. strcat(szStr1, cszStr2);
  354. }
  355. int GetRegKeyValueByKey(HKEY hKey, const char cszRegKey[], const char cszKeyName[], char szValue[], int nSize)
  356. {
  357. HKEY Key;
  358. int nRetCode = -1;
  359. DWORD dwCount = nSize;
  360. LONG lRetCode = 0;
  361. lRetCode = RegOpenKeyEx(hKey, cszRegKey, 0, KEY_READ, &Key);
  362. if (ERROR_SUCCESS == lRetCode)
  363. {
  364. nRetCode = 0;
  365. lRetCode = RegQueryValueEx(Key, cszKeyName, NULL, NULL, (LPBYTE)szValue, &dwCount);
  366. if (ERROR_SUCCESS == lRetCode)
  367. nRetCode = 1;
  368. RegCloseKey(Key);
  369. }
  370. return nRetCode;
  371. }
  372. int GetRegKeyValue(const char cszRegKey[], const char cszKeyName[], char szValue[], int nSize)
  373. {
  374. int nRetCode = 0;
  375. nRetCode = GetRegKeyValueByKey(HKEY_CURRENT_USER, cszRegKey, cszKeyName, szValue, nSize);
  376. if (0 >= nRetCode)
  377. nRetCode = GetRegKeyValueByKey(HKEY_LOCAL_MACHINE, cszRegKey, cszKeyName, szValue, nSize);
  378. return nRetCode;
  379. }