SysGlobalF.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //---------------------------------------------------------------------------
  2. #pragma hdrstop
  3. //---------------------------------------------------------------------------
  4. #include <vcl.h>
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <memory.h>
  10. #include <Systobj.h>
  11. #include <winsock.h>
  12. #include <time.h>
  13. #include <inifiles.hpp>
  14. #include <math.h>
  15. //---------------------------------------------------------------------------
  16. #include "SysGlobalF.h"
  17. //---------------------------------------------------------------------------
  18. #pragma package(smart_init)
  19. //---------------------------------------------------------------------------
  20. String g_sAppDir = ""; // Application Directory
  21. String g_sAppName = ""; // Program name
  22. String g_sCfgDir = ""; // Program Config Directory
  23. String g_sLogDir = ""; // Program Log Directory
  24. String g_sTempDir = ""; // Program Temp Directory
  25. String g_sImgDir = ""; // Program Image Directory
  26. String g_sFormDir = ""; // Program Form Directory
  27. String g_sFtpDir = ""; // Application Directory
  28. //---------------------------------------------------------------------------
  29. /*
  30. * 응용프로그램이 중복으로 실행되는 것을 막기위해 뮤텍스를 생성한다.
  31. * arguments
  32. *
  33. * return
  34. * void
  35. */
  36. bool SYS_ApplicationSingleInstance(AnsiString AProgName, AnsiString APidFile)
  37. {
  38. bool isRunning = false;
  39. HANDLE hMutex = 0;
  40. AnsiString sMutexName;
  41. sMutexName = "Global\\" + AProgName;
  42. hMutex = CreateMutex(NULL, TRUE, sMutexName.c_str());
  43. if (hMutex && GetLastError() == ERROR_ALREADY_EXISTS)
  44. {
  45. char buffer[1000];
  46. ReleaseMutex(hMutex);
  47. hMutex=0;
  48. isRunning = true;
  49. ////////////////////////////////////////////////////////////////////////////
  50. AnsiString fn;
  51. char tmp[500];
  52. if (FileExists(APidFile))
  53. {
  54. TFileStream *fp = new TFileStream(APidFile, fmOpenRead);
  55. HWND hwnd;
  56. AnsiString nfo;
  57. memset(buffer, 0, 1000);
  58. fp->Read(buffer, 100);
  59. delete fp;
  60. hwnd = (HWND)(String(buffer).ToIntDef(0));
  61. if (hwnd)
  62. {
  63. SendMessage(hwnd, WM_USER, WM_WINDOW_RESTORE, 0);
  64. }
  65. }
  66. }
  67. if (hMutex)
  68. {
  69. ReleaseMutex(hMutex);
  70. hMutex=0;
  71. }
  72. return isRunning;
  73. }
  74. //---------------------------------------------------------------------------
  75. /*
  76. * 응용프로그램의 중복실행을 막기위해 응용프로그램이 실행되면
  77. * 프로세스(윈도우핸들) 정보를 파일에 저장한다.
  78. * arguments
  79. *
  80. * return
  81. * void
  82. */
  83. void SYS_WritePidFile(long AHandle)
  84. {
  85. char tmp[500];
  86. String sPidFile;
  87. sPidFile = g_sCfgDir + ChangeFileExt(ExtractFileName(Application->ExeName), ".pid");
  88. try
  89. {
  90. TFileStream *fp = new TFileStream(sPidFile, fmCreate);
  91. memset(tmp, 0x00, sizeof(tmp));
  92. sprintf(tmp, "%d", (long)AHandle);
  93. fp->Write(tmp, strlen(tmp));
  94. delete fp;
  95. }
  96. catch(Exception &e)
  97. {
  98. }
  99. }
  100. //---------------------------------------------------------------------------
  101. /*
  102. * 환경설정 정보를 저장하는 함수.
  103. * arguments
  104. * String : RegisterKey 또는 파일이름
  105. * return
  106. * bool : 실패하면 false
  107. */
  108. bool SYS_WriteConfigInfo(String sTitle, String sItem, String sValue, String sCfgFile/*=""*/)
  109. {
  110. String ConfigFile;
  111. TIniFile *pIniFile = NULL;
  112. ConfigFile = sCfgFile;
  113. try
  114. {
  115. pIniFile = new TIniFile(ConfigFile);
  116. if (pIniFile == NULL)
  117. {
  118. return false;
  119. }
  120. pIniFile->WriteString(sTitle, sItem, sValue);
  121. }
  122. catch(...)
  123. {
  124. }
  125. if (pIniFile)
  126. {
  127. pIniFile->Free();
  128. pIniFile = NULL;
  129. }
  130. return true;
  131. }
  132. //---------------------------------------------------------------------------
  133. /*
  134. * 환경설정 정보를 읽어오는 함수.
  135. * arguments
  136. * String : RegisterKey 또는 파일이름
  137. * return
  138. * bool : 실패하면 false
  139. */
  140. bool ReadConfigInfo(String sTitle, String sItem, String &sValue, String sCfgFile/*=""*/)
  141. {
  142. bool bRes;
  143. String ConfigFile;
  144. TIniFile *pIniFile = NULL;
  145. bRes = false;
  146. ConfigFile = sCfgFile;
  147. try
  148. {
  149. pIniFile = new TIniFile(ConfigFile);
  150. if (pIniFile == NULL)
  151. {
  152. return bRes;
  153. }
  154. sValue = pIniFile->ReadString(sTitle, sItem, "");
  155. if (sValue != "")
  156. {
  157. bRes = true;
  158. }
  159. }
  160. catch(...)
  161. {
  162. }
  163. if (pIniFile)
  164. {
  165. pIniFile->Free();
  166. pIniFile = NULL;
  167. }
  168. return bRes;
  169. }
  170. //---------------------------------------------------------------------------
  171. String SYS_GetSysError()
  172. {
  173. AnsiString sErrMsg = "";
  174. LPVOID lpMsgBuf = NULL;
  175. try
  176. {
  177. try
  178. {
  179. FormatMessage(
  180. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  181. NULL,
  182. GetLastError(),
  183. //MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  184. MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
  185. (LPTSTR) &lpMsgBuf,
  186. 0,
  187. NULL
  188. );
  189. char *pData = (char*)lpMsgBuf;
  190. if (strlen(pData) > 2)
  191. {
  192. pData[strlen(pData)-2] = 0x00;
  193. pData[strlen(pData)-1] = 0x00;
  194. }
  195. sErrMsg = AnsiString(pData);
  196. }
  197. __finally
  198. {
  199. if (lpMsgBuf) LocalFree(lpMsgBuf);
  200. lpMsgBuf = NULL;
  201. }
  202. }
  203. catch(Exception &e)
  204. {
  205. }
  206. return sErrMsg;
  207. }
  208. //---------------------------------------------------------------------------