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