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

模拟服务器

开发平台:

C/C++

  1. //-----------------------------------------//
  2. //                                         //
  3. //  File : KStdAfx.cpp              //
  4. // Author : Yang Xiaodong            //
  5. // Modified : 8/14/2002                //
  6. //                                         //
  7. //-----------------------------------------//
  8. #include "GlobalFun.h"
  9. #include "process.h"
  10. static const TCHAR szEnter1[] = _T("rn");
  11. static const UINT uiEnter1 = _tcslen( szEnter1 );
  12. static const TCHAR szEnter2[] = _T("nr");
  13. static const UINT uiEnter2 = _tcslen( szEnter2 );
  14. static const TCHAR szEnter3[] = _T("r");
  15. static const UINT uiEnter3 = _tcslen( szEnter3 );
  16. static const TCHAR szEnter4[] = _T("n");
  17. static const UINT uiEnter4 = _tcslen( szEnter4 );
  18. static const TCHAR szTabKey[] = { 0x09, 0 };
  19. static const UINT uiTabkey = _tcslen( szTabKey );
  20. static const UINT uiTab2SpaceNum = 4;
  21. static const TCHAR szSpaceKey[] = _T(" ");
  22. static const UINT uiSpaceKey = _tcslen( szSpaceKey );
  23. static const TCHAR szLBracket[] = _T("[");
  24. static const UINT uiLBracket = _tcslen( szLBracket );
  25. static const TCHAR szRBracket[] = _T("]");
  26. static const UINT uiRBracket = _tcslen( szRBracket );
  27. static const TCHAR szEqual[] = _T("=");
  28. static const UINT uiEqual = _tcslen( szEqual );
  29. static BOOL IsEnterKey( LPCTSTR lpChar,
  30. BOOL* pEnterType = NULL )
  31. {
  32. BOOL bRet = FALSE;
  33. if ( 0 == _tcsncmp( lpChar, szEnter1, uiEnter1 ) )
  34. {
  35. if ( NULL != pEnterType )
  36. {
  37. *pEnterType = TRUE;
  38. }
  39. bRet = TRUE;
  40. }
  41. else if ( 0 == _tcsncmp( lpChar, szEnter2, uiEnter2 ) )
  42. {
  43. if ( NULL != pEnterType )
  44. {
  45. *pEnterType = TRUE;
  46. }
  47. bRet = TRUE;
  48. }
  49. else if ( 0 == _tcsncmp( lpChar, szEnter3, uiEnter3 ) )
  50. {
  51. if ( NULL != pEnterType )
  52. {
  53. *pEnterType = FALSE;
  54. }
  55. bRet = TRUE;
  56. }
  57. else if ( 0 == _tcsncmp( lpChar, szEnter4, uiEnter4 ) )
  58. {
  59. if ( NULL != pEnterType )
  60. {
  61. *pEnterType = FALSE;
  62. }
  63. bRet = TRUE;
  64. }
  65. return bRet;
  66. }
  67. static long GetFirstLineLen( LPCTSTR lpszSource )
  68. {
  69. long liRet = 0;
  70. if ( NULL != lpszSource )
  71. {
  72. LPTSTR lpszAt = ( LPTSTR )lpszSource;
  73. BOOL bHasEnterKey = FALSE;
  74. while ( 0 != lpszAt[0] )
  75. {
  76. if ( (0 == _tcsncmp( lpszAt, szEnter1, uiEnter1 ) )
  77. || ( 0 == _tcsncmp( lpszAt, szEnter2, uiEnter2 ) )
  78. || ( 0 == _tcsncmp( lpszAt, szEnter3, uiEnter3 ) )
  79. || ( 0 == _tcsncmp( lpszAt, szEnter4, uiEnter4 ) ) )
  80. {
  81. bHasEnterKey = TRUE;
  82. liRet = lpszAt - lpszSource;
  83. break;
  84. }
  85. lpszAt++;
  86. }
  87. if ( !bHasEnterKey )
  88. {
  89. liRet = lpszAt - lpszSource;
  90. }
  91. }
  92. return liRet;
  93. }
  94. static BOOL GetStrInStrPos( LPCTSTR lpszSouce,
  95.    LPCTSTR lpszSearch,
  96.    long &liOffset )
  97. {
  98. BOOL bRet = FALSE;
  99. if ( ( NULL == lpszSouce )
  100. || ( NULL == lpszSearch ) )
  101. {
  102. bRet = FALSE;
  103. }
  104. else
  105. {
  106. long liSource = _tcslen( lpszSouce );
  107. long liSearch = _tcslen( lpszSearch );
  108. LPTSTR lpszAt = ( LPTSTR )( lpszSouce );
  109. for ( long i = 0; i < liSource; i++ )
  110. {
  111. if ( 0 == _tcsncmp( lpszAt, lpszSearch, liSearch ) )
  112. {
  113. bRet = TRUE;
  114. liOffset = i;
  115. break;
  116. }
  117. lpszAt++;
  118. }
  119. }
  120. return bRet;
  121. }
  122. //----------------------------------------------------------
  123. // < Function >
  124. // Name : GetPrivateProfileKeyPos
  125. // Return : int
  126. //   = -1 ---- Error;
  127. //   = 0 ---- Has no the section;
  128. //   = 1 ---- Finds out the key successfully;
  129. //   = 2 ---- Only finds out the section.
  130. // Platform : Unconcerned.
  131. //----------------------------------------------------------
  132. static int GetPrivateProfileKeyPos( LPCTSTR lpszBuf,
  133. LPCTSTR lpszAppName,
  134. LPCTSTR lpszKeyName,
  135. long &liOffset,
  136. long &liSize )
  137. {
  138. int iRet = -1;
  139. if ( ( NULL == lpszBuf )
  140. || ( NULL == lpszAppName )
  141. || ( NULL == lpszKeyName ) )
  142. {
  143. iRet = -1;
  144. }
  145. else
  146. {
  147. BOOL bSearchFlag = FALSE;
  148. LPTSTR lpszAt = ( LPTSTR )lpszBuf;
  149. long liAppName = _tcslen( lpszAppName );
  150. LPTSTR lpszModifiedAppName = new TCHAR[liAppName+3];
  151. if ( NULL != lpszModifiedAppName )
  152. {
  153. _tcscpy( lpszModifiedAppName, szLBracket );
  154. _tcscpy( lpszModifiedAppName + uiLBracket, lpszAppName );
  155. _tcscpy( lpszModifiedAppName + uiLBracket + liAppName, szRBracket );
  156. long liAppNameOffset;
  157. bSearchFlag = GetStrInStrPos( lpszBuf, lpszModifiedAppName, liAppNameOffset );
  158. if ( bSearchFlag )
  159. {
  160. lpszAt +=
  161. liAppNameOffset + liAppName + uiLBracket + uiRBracket;
  162. long liKeyNameOffset;
  163. bSearchFlag = GetStrInStrPos( lpszAt, lpszKeyName, liKeyNameOffset );
  164. if ( bSearchFlag )
  165. {
  166. liOffset =
  167. liAppNameOffset + liKeyNameOffset + liAppName + uiLBracket + uiRBracket;
  168. liSize = GetFirstLineLen( lpszAt + liKeyNameOffset );
  169. iRet = 1;
  170. }
  171. else
  172. {
  173. liOffset =
  174. liAppNameOffset + liAppName + uiLBracket + uiRBracket;
  175. liSize = 0;
  176. iRet = 2;
  177. }
  178. }
  179. else
  180. {
  181. iRet = 0;
  182. }
  183. delete []lpszModifiedAppName;
  184. }
  185. }
  186. return iRet;
  187. }
  188. static LPTSTR GetKeyValue( LPCTSTR lpszKeyLine, long &liKeyValueSize )
  189. {
  190. LPTSTR lpszRet = NULL;
  191. if ( NULL != lpszKeyLine )
  192. {
  193. long liKeySize = GetFirstLineLen( lpszKeyLine );
  194. LPTSTR lpszAt = ( LPTSTR )lpszKeyLine;
  195. BOOL bHasValue = FALSE;
  196. long i;
  197. for ( i = 0; i < liKeySize; i++ )
  198. {
  199. if ( 0 == _tcsncmp( lpszAt, szEqual, uiEqual ) )
  200. {
  201. bHasValue = TRUE;
  202. break;
  203. }
  204. lpszAt++;
  205. }
  206. if ( bHasValue )
  207. {
  208. for ( i += uiEqual, lpszAt += uiEqual; i < liKeySize; )
  209. {
  210. if ( 0 == _tcsncmp( lpszAt, szSpaceKey, uiSpaceKey ) )
  211. {
  212. lpszAt += uiSpaceKey;
  213. i += uiSpaceKey;
  214. continue;
  215. }
  216. else
  217. {
  218. if ( ( lpszAt - lpszKeyLine ) < liKeySize )
  219. {
  220. lpszRet = lpszAt;
  221. liKeyValueSize =
  222. liKeySize - ( lpszAt - lpszKeyLine );
  223. }
  224. break;
  225. }
  226. }
  227. }
  228. }
  229. return lpszRet;
  230. }
  231. //----------------------------------------------------------
  232. // < Function >
  233. // Name : KPIWritePrivateProfileString
  234. // Return : BOOL
  235. // Description : copies a string into the
  236. //   specified section of an
  237. //   initialization file.
  238. // Platform : Unconcerned.
  239. //----------------------------------------------------------
  240. BOOL KPIWritePrivateProfileString( LPCTSTR lpszAppName,  /* section name */
  241.   LPCTSTR lpszKeyName,  /* key name */
  242.   LPCTSTR lpszString,   /* string to add */
  243.   LPCTSTR lpszFileName  /* initialization file */ )
  244. {
  245. BOOL bRet = TRUE;
  246. if ( ( NULL == lpszAppName )
  247. || ( NULL == lpszKeyName )
  248. || ( NULL == lpszString )
  249. || ( NULL == lpszFileName ) )
  250. {
  251. bRet = FALSE;
  252. }
  253. else
  254. {
  255. try
  256. {
  257. FILE* pFile = fopen( lpszFileName, "rb" );
  258. long liFileSize = 0;
  259. char* lpszFileBuf = NULL;
  260. if ( NULL != pFile )
  261. {
  262. fseek( pFile, 0, SEEK_END );
  263. liFileSize = ftell( pFile );
  264. if ( liFileSize > 0 )
  265. {
  266. lpszFileBuf = new char[liFileSize+1];
  267. if ( NULL != lpszFileBuf )
  268. {
  269. fseek( pFile, 0, SEEK_SET );
  270. fread( lpszFileBuf, 1, liFileSize, pFile );
  271. lpszFileBuf[liFileSize] = 0;
  272. }
  273. else
  274. {
  275. fclose( pFile );
  276. bRet = FALSE;
  277. return bRet;
  278. }
  279. }
  280. fclose( pFile );
  281. }
  282. pFile = fopen( lpszFileName, "wb" );
  283. if ( NULL != pFile )
  284. {
  285. if ( 0 == liFileSize )
  286. {
  287. fwrite( szLBracket, 1, uiLBracket, pFile );
  288. fwrite( lpszAppName, 1, _tcslen( lpszAppName ), pFile );
  289. fwrite( szRBracket, 1, uiRBracket, pFile );
  290. fwrite( szEnter1, 1, uiEnter1, pFile );
  291. fwrite( lpszKeyName, 1, _tcslen( lpszKeyName ), pFile );
  292. fwrite( szEqual, 1, uiEqual, pFile );
  293. fwrite( lpszString, 1, _tcslen( lpszString ), pFile );
  294. fwrite( szEnter1, 1, uiEnter1, pFile );
  295. }
  296. else
  297. {
  298. long liOffset, liSize;
  299. int iResult =
  300. GetPrivateProfileKeyPos( lpszFileBuf,
  301. lpszAppName,
  302. lpszKeyName,
  303. liOffset,
  304. liSize );
  305. if ( 0 == iResult )
  306. {
  307. fwrite( lpszFileBuf, 1, liFileSize, pFile );
  308. if ( ( !IsEnterKey( &( lpszFileBuf[liFileSize-2] ) ) )
  309. && ( !IsEnterKey( &( lpszFileBuf[liFileSize-3] ) ) ) )
  310. {
  311. fwrite( szEnter1, 1, uiEnter1, pFile );
  312. }
  313. fwrite( szLBracket, 1, uiLBracket, pFile );
  314. fwrite( lpszAppName, 1, _tcslen( lpszAppName ), pFile );
  315. fwrite( szRBracket, 1, uiRBracket, pFile );
  316. fwrite( szEnter1, 1, uiEnter1, pFile );
  317. fwrite( lpszKeyName, 1, _tcslen( lpszKeyName ), pFile );
  318. fwrite( szEqual, 1, uiEqual, pFile );
  319. fwrite( lpszString, 1, _tcslen( lpszString ), pFile );
  320. fwrite( szEnter1, 1, uiEnter1, pFile );
  321. bRet = TRUE;
  322. }
  323. else if ( 1 == iResult )
  324. {
  325. fwrite( lpszFileBuf, 1, liOffset, pFile );
  326. long liKeyLen =
  327. GetFirstLineLen( &( lpszFileBuf[liOffset] ) );
  328. LPTSTR lpszKeyTemp = new TCHAR[liKeyLen+1];
  329. memcpy( lpszKeyTemp, &( lpszFileBuf[liOffset] ), liKeyLen * sizeof( TCHAR ) );
  330. lpszKeyTemp[liKeyLen] = 0;
  331. long liKeyValueSize;
  332. if ( NULL != GetKeyValue( lpszKeyTemp, liKeyValueSize ) )
  333. {
  334. fwrite( lpszKeyTemp, 1, liKeyLen - liKeyValueSize, pFile );
  335. fwrite( lpszString, 1, _tcslen( lpszString ), pFile );
  336. fwrite( &( lpszFileBuf[liOffset+liKeyLen] ), 1, liFileSize - liOffset - liKeyLen, pFile );
  337. }
  338. else
  339. {
  340. fwrite( &( lpszFileBuf[liOffset] ), 1, liFileSize - liOffset, pFile );
  341. }
  342. delete []lpszKeyTemp;
  343. bRet = TRUE;
  344. }
  345. else if ( 2 == iResult )
  346. {
  347. fwrite( lpszFileBuf, 1, liOffset, pFile );
  348. fwrite( szEnter1, 1, uiEnter1, pFile );
  349. fwrite( lpszKeyName, 1, _tcslen( lpszKeyName ), pFile );
  350. fwrite( szEqual, 1, uiEqual, pFile );
  351. fwrite( lpszString, 1, _tcslen( lpszString ), pFile );
  352. if ( !IsEnterKey( &( lpszFileBuf[liOffset] ) ) )
  353. {
  354. fwrite( szEnter1, 1, uiEnter1, pFile );
  355. }
  356. fwrite( &( lpszFileBuf[liOffset] ), 1, liFileSize - liOffset, pFile );
  357. bRet = TRUE;
  358. }
  359. else
  360. {
  361. bRet = FALSE;
  362. }
  363. }
  364. fclose( pFile );
  365. }
  366. else
  367. {
  368. bRet = FALSE;
  369. }
  370. if ( NULL != lpszFileBuf )
  371. {
  372. delete []lpszFileBuf;
  373. }
  374. }
  375. catch(...)
  376. {
  377. bRet = FALSE;
  378. }
  379. }
  380. return bRet;
  381. }
  382. //----------------------------------------------------------
  383. // < Function >
  384. // Name : KPIGetPrivateProfileString
  385. // Return : DWORD
  386. //   The return value is the number of
  387. //   characters copied to the buffer,
  388. //   not including the terminating null
  389. //   character.
  390. // Description : retrieves a string from
  391. //   the specified section in
  392. //   an initialization file.
  393. // Platform : Unconcerned.
  394. //----------------------------------------------------------
  395. DWORD KPIGetPrivateProfileString( LPCTSTR lpszAppName, /* section name(Input) */
  396.  LPCTSTR lpszKeyName, /* key name(Input) */
  397.  LPCTSTR lpszDefault, /* default string(Input) */
  398.  LPTSTR lpszReturnedString, /* destination buffer(Output) */
  399.  DWORD dwSize, /* size of destination buffer(Input) */
  400.  LPCTSTR lpszFileName /* initialization file name(Input) */ )
  401. {
  402. DWORD dwRet = 0;
  403. if ( ( NULL == lpszAppName )
  404. || ( NULL == lpszKeyName )
  405. || ( NULL == lpszDefault )
  406. || ( NULL == lpszFileName ) )
  407. {
  408. return dwRet;
  409. }
  410. try
  411. {
  412. FILE* pFile = fopen( lpszFileName, "rb" );
  413. if ( NULL != pFile )
  414. {
  415. fseek( pFile, 0, SEEK_END );
  416. long liFileSize = ftell( pFile );
  417. char* lpszFileBuf = NULL;
  418. if ( liFileSize > 0 )
  419. {
  420. lpszFileBuf = new char[liFileSize+1];
  421. if ( NULL != lpszFileBuf )
  422. {
  423. fseek( pFile, 0, SEEK_SET );
  424. fread( lpszFileBuf, 1, liFileSize, pFile );
  425. lpszFileBuf[liFileSize] = 0;
  426. long liOffset, liSize;
  427. int iResult =
  428. GetPrivateProfileKeyPos( lpszFileBuf,
  429. lpszAppName,
  430. lpszKeyName,
  431. liOffset,
  432. liSize );
  433. if ( 1 == iResult )
  434. {
  435. long liKeyLen =
  436. GetFirstLineLen( &( lpszFileBuf[liOffset] ) );
  437. LPTSTR lpszKeyTemp = new TCHAR[liKeyLen+1];
  438. memcpy( lpszKeyTemp, &( lpszFileBuf[liOffset] ), liKeyLen * sizeof( TCHAR ) );
  439. lpszKeyTemp[liKeyLen] = 0;
  440. long liKeyValueSize;
  441. LPTSTR lpszKeyValue =
  442. GetKeyValue( lpszKeyTemp, liKeyValueSize );
  443. if ( NULL != lpszKeyValue )
  444. {
  445. dwRet = liKeyValueSize;
  446. DWORD dwCopyLen =
  447. dwSize <= liKeyValueSize ? dwSize : liKeyValueSize;
  448. if ( NULL != lpszReturnedString )
  449. {
  450. memcpy( lpszReturnedString, lpszKeyValue, dwCopyLen );
  451. if ( dwSize > dwCopyLen )
  452. {
  453. lpszReturnedString[dwCopyLen] = 0;
  454. }
  455. }
  456. }
  457. else
  458. {
  459. dwRet = _tcslen( lpszDefault );
  460. DWORD dwCopyLen =
  461. dwSize <= dwRet ? dwSize : dwRet;
  462. if ( NULL != lpszReturnedString )
  463. {
  464. memcpy( lpszReturnedString, lpszDefault, dwCopyLen );
  465. if ( dwSize > dwCopyLen )
  466. {
  467. lpszReturnedString[dwCopyLen] = 0;
  468. }
  469. }
  470. }
  471. delete []lpszKeyTemp;
  472. }
  473. else
  474. {
  475. dwRet = _tcslen( lpszDefault );
  476. DWORD dwCopyLen =
  477. dwSize <= dwRet ? dwSize : dwRet;
  478. if ( NULL != lpszReturnedString )
  479. {
  480. memcpy( lpszReturnedString, lpszDefault, dwCopyLen );
  481. if ( dwSize > dwCopyLen )
  482. {
  483. lpszReturnedString[dwCopyLen] = 0;
  484. }
  485. }
  486. }
  487. delete []lpszFileBuf;
  488. }
  489. }
  490. fclose( pFile );
  491. }
  492. }
  493. catch(...)
  494. {
  495. }
  496. return dwRet;
  497. }
  498. //----------------------------------------------------------
  499. // < Function >
  500. // Name : KPIWaitForSingleObject
  501. // Return : DWORD
  502. //   = 0 ---- failed;
  503. //   = 1 ---- The state of the specified
  504. //      object is signaled;
  505. //   = 2 ---- time out;
  506. //   = ... ---- Not defined.
  507. // Platform : Windows 9X/NT/2000/Later
  508. //----------------------------------------------------------
  509. DWORD KPIWaitForSingleObject( HANDLE hHandle, DWORD dwMilliseconds )
  510. {
  511. DWORD dwRet = 0;
  512. DWORD dwResult = WaitForSingleObject( hHandle, dwMilliseconds );
  513. switch ( dwResult )
  514. {
  515. case WAIT_FAILED:
  516. dwRet = 0;
  517. break;
  518. case WAIT_OBJECT_0:
  519. dwRet = 1;
  520. break;
  521. case WAIT_TIMEOUT:
  522. dwRet = 2;
  523. break;
  524. default:
  525. dwRet = 0;
  526. break;
  527. }
  528. return dwRet;
  529. }
  530. //----------------------------------------------------------
  531. // < Function >
  532. // Name : KPICloseHandle
  533. // Return : BOOL
  534. // Platform : Windows 9X/NT/2000/Later
  535. //----------------------------------------------------------
  536. BOOL KPICloseHandle( HANDLE hObject )
  537. {
  538. BOOL bRet = FALSE;
  539. bRet = CloseHandle( hObject );
  540. return bRet;
  541. }
  542. //----------------------------------------------------------
  543. // < Function >
  544. // Name : KPITerminateThread
  545. // Return : BOOL
  546. // Platform : Windows 9X/NT/2000/Later
  547. //----------------------------------------------------------
  548. BOOL KPITerminateThread( HANDLE hThread, DWORD dwExitCode )
  549. {
  550. BOOL bRet = FALSE;
  551. bRet = TerminateThread( hThread, dwExitCode );
  552. return bRet;
  553. }
  554. //----------------------------------------------------------
  555. // < Function >
  556. // Name : KPICreateThread
  557. // Return : HANDLE
  558. //   Returns 0 if function is failed;
  559. //   Returns the handle of new thread if
  560. //   function is successful.
  561. // Description : Creates a thread to execute within
  562. //   the virtual address space of the
  563. //   calling process.
  564. // Platform : Windows 9X/NT/2000/Later
  565. //----------------------------------------------------------
  566. HANDLE KPICreateThread( LPTHREAD_START_ROUTINE lpStartAddress, /* thread function */
  567.    LPVOID lpParameter, /* thread argument */
  568.    LPDWORD lpThreadId /* thread identifier */ )
  569. {
  570. typedef unsigned ( __stdcall *start_address )( void * );
  571. HANDLE hRet = NULL;
  572. hRet = 
  573. (HANDLE)_beginthreadex( NULL, 0, (start_address)lpStartAddress, lpParameter, 0, (unsigned*)lpThreadId);
  574. return hRet;
  575. }
  576. SOCKET KPICreateUDPSocket( int iPort )
  577. {
  578. SOCKET s = INVALID_SOCKET;
  579. // The follow code is used to initialize
  580. // the environment for Windows OS platform
  581. // and should be removed or changed when
  582. // replanted to other platforms.
  583. //------>BEGIN
  584. WORD wVersionRequired = MAKEWORD(1,1);
  585. WSADATA WSAdata;
  586. if ( 0 != WSAStartup( wVersionRequired, &WSAdata ) )
  587. {
  588. s = INVALID_SOCKET;
  589. return s;
  590. }
  591. //<------END
  592. s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  593. if ( INVALID_SOCKET == s )
  594. {
  595. return s;
  596. }
  597. SOCKADDR_IN local;
  598. local.sin_family = AF_INET;
  599. local.sin_port = htons( ( short )( iPort ) );
  600. local.sin_addr.s_addr = htonl( INADDR_ANY );
  601. if ( bind( s, ( SOCKADDR* )( &local ), sizeof( local ) )
  602. == SOCKET_ERROR )
  603. {
  604. closesocket( s );
  605. s = INVALID_SOCKET;
  606. return s;
  607. }
  608. WSACleanup(); // Needed on Windows OS platforms.
  609. return s;
  610. }
  611. HANDLE KPICreateEvent( LPSECURITY_ATTRIBUTES lpEventAttributes, /* SD */
  612.   BOOL bManualReset, /* reset type */
  613.   BOOL bInitialState, /* initial state */
  614.   LPCTSTR lpszName /* object name */ )
  615. {
  616. HANDLE hRet = NULL;
  617. hRet = CreateEvent( lpEventAttributes, bManualReset, bInitialState, lpszName );
  618. return hRet;
  619. }
  620. BOOL KPISetEvent( HANDLE hEvent/* handle to event */ )
  621. {
  622. BOOL bRet = FALSE;
  623. bRet = SetEvent( hEvent );
  624. return bRet;
  625. }
  626. BOOL KPIResetEvent( HANDLE hEvent/* handle to event */ )
  627. {
  628. BOOL bRet = FALSE;
  629. bRet = ResetEvent( hEvent );
  630. return bRet;
  631. }
  632. void KPIGetExePath( LPSTR lpExePath, DWORD dwSize )
  633. {
  634. if ( lpExePath )
  635. {
  636. memset( lpExePath, 0, dwSize );
  637. GetModuleFileName( NULL, lpExePath, dwSize );
  638. DWORD dwEndAt;
  639. dwEndAt = strlen( lpExePath ) - 1;
  640. while ( dwEndAt >= 1 )
  641. {
  642. if ( lpExePath[dwEndAt-1] == '\' )
  643. {
  644. lpExePath[dwEndAt] = 0;
  645. break;
  646. }
  647. dwEndAt--;
  648. }
  649. }
  650. }
  651. HANDLE KPICreateMutex( LPSECURITY_ATTRIBUTES lpMutexAttributes, /* SD */
  652.   BOOL bInitialOwner, /* initial owner */
  653.   LPCTSTR lpName /* object name */ )
  654. {
  655. return CreateMutex( lpMutexAttributes, bInitialOwner, lpName );
  656. }
  657. BOOL KPIReleaseMutex( HANDLE hMutex )
  658. {
  659. return ReleaseMutex( hMutex );
  660. }
  661. unsigned long KPIHash( char* pStr, unsigned long ulModel, unsigned long ulBaseNum )
  662. {
  663. assert( ( NULL != pStr ) && ( 0 < ulModel ) );
  664.     unsigned long i = 0;
  665.     unsigned long j = 1;
  666.     while ( *pStr )
  667. {
  668. i += ( ( unsigned char )*pStr ) * j++;
  669. pStr++;
  670. }
  671.     return ( i % ulModel + ulBaseNum );
  672. }
  673. void KPIPrintToFile( const TCHAR* lpszFilePath, const TCHAR* lpszFmt, ... )
  674. {
  675. if ( ( NULL == lpszFilePath )
  676. || ( NULL == lpszFmt ) )
  677. {
  678. return;
  679. }
  680. FILE* pF = fopen( lpszFilePath, "a+b" );
  681. if ( NULL != pF )
  682. {
  683. fseek( pF, 0, SEEK_END );
  684. va_list args;
  685. va_start( args, lpszFmt );
  686. vfprintf( pF, lpszFmt, args );
  687. va_end ( args );
  688. fclose( pF );
  689. }
  690. }
  691. void KPIPrintToFile( FILE* pFile, const TCHAR* lpszFmt, ... )
  692. {
  693. if ( ( NULL == pFile )
  694. || ( NULL == lpszFmt ) )
  695. {
  696. return;
  697. }
  698. va_list args;
  699. va_start( args, lpszFmt );
  700. vfprintf( pFile, lpszFmt, args );
  701. va_end ( args );
  702. }