Common_Funcs.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. #include "StdAfx.h"
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <wchar.h>
  5. #include <shlwapi.h>
  6. #include "common_funcs.h"
  7. //#include "debug.h"
  8. #include <mmsystem.h>
  9. #pragma comment(lib, "winmm.lib")
  10. #pragma warning(push)
  11. #pragma warning(disable:4996)
  12. Common_Funcs::Common_Funcs(void)
  13. {
  14. }
  15. Common_Funcs::~Common_Funcs(void)
  16. {
  17. }
  18. void GetModulePath(string &path)
  19. {
  20. TCHAR szPathBuffer[MAX_PATH] = {0};
  21. ::GetModuleFileName(NULL, szPathBuffer, MAX_PATH);
  22. string prc = szPathBuffer;
  23. std::size_t found = prc.find_last_of("/");
  24. path = prc.substr(0,found+1);
  25. }
  26. UINT64 GetC_M_N(UINT64 M,UINT64 N)
  27. {
  28. UINT64 d = 1;
  29. UINT64 c = 1;
  30. for(UINT64 i = N;i > (N - M);i --)
  31. {
  32. c = c * i;
  33. }
  34. for(UINT64 i = 1;i <= M;i++)
  35. {
  36. c = c / i;
  37. }
  38. //ASSERT(0);//need test
  39. return c;
  40. }
  41. int GetRandomNumber(int Begin,int End)
  42. {
  43. srand(GetTickCount());
  44. return rand() % (End - Begin + 1) + Begin;
  45. }
  46. DWORD WaitForObjectAcurateTime(HANDLE event,DWORD dwMilliseconds)
  47. {
  48. double elapsed;
  49. DWORD passedTime = 0;
  50. LARGE_INTEGER startCount;
  51. LARGE_INTEGER endCount;
  52. LARGE_INTEGER freq;
  53. QueryPerformanceFrequency(&freq);
  54. while(passedTime < dwMilliseconds)
  55. {
  56. passedTime = 0;
  57. QueryPerformanceCounter(&startCount);
  58. if(WaitForSingleObject(event,dwMilliseconds) == WAIT_OBJECT_0)
  59. {
  60. return WAIT_OBJECT_0;
  61. }
  62. QueryPerformanceCounter(&endCount);
  63. elapsed = (double)(endCount.QuadPart - startCount.QuadPart) / freq.QuadPart;
  64. passedTime = (DWORD)(elapsed * 1000);
  65. if(dwMilliseconds > passedTime)
  66. {
  67. dwMilliseconds -= passedTime;
  68. }
  69. else
  70. {
  71. break;
  72. }
  73. }
  74. return WAIT_TIMEOUT;
  75. }
  76. BOOL StrToInt(char * str,int *result)
  77. {
  78. int value = 0;
  79. int sign = 1;
  80. int radix;
  81. if(*str == '-')
  82. {
  83. sign = -1;
  84. str++;
  85. }
  86. // 16=xVF5DJWWVD8JGx;rU_0x
  87. if(*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X'))
  88. {
  89. radix = 16;
  90. str += 2;
  91. }
  92. else if(*str == '0') // 0K=xVFJWWV7{N*0
  93. {
  94. radix = 8;
  95. str++;
  96. }
  97. else
  98. {
  99. radix = 10;
  100. }
  101. while(*str)
  102. {
  103. if(radix == 16)
  104. {
  105. if(*str >= '0' && *str <= '9')
  106. {
  107. value = value * radix + *str - '0';
  108. }
  109. else
  110. {
  111. if((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f')
  112. {
  113. value = value * radix + (*str | 0x20) - 'a' + 10;
  114. }
  115. else
  116. {
  117. return FALSE;
  118. }
  119. }
  120. }
  121. else
  122. {
  123. value = value * radix + *str - '0';
  124. }
  125. str++;
  126. }
  127. *result = sign*value;
  128. return TRUE;
  129. }
  130. int sundaySearch(const unsigned char *src,int len_s,const unsigned char *keystr,int len_key)
  131. {
  132. int i,j,pos=0;
  133. int next[256]={0};//nextJ}Wi#,T$4&@zOuJ<;/
  134. for(j=0;j<256;++j)//3uJ<;/nextJ}?E
  135. next[j]=len_key+1;
  136. for(j=0;j<len_key;++j)//IhVCnextJ}?E
  137. next[keystr[j]]=len_key-j;
  138. while( pos<(len_s-len_key+1) )//1i@zT-4.
  139. {
  140. i=pos;
  141. for(j=0;j<len_key;++j,++i)//1H=O
  142. {
  143. if(src[i]!=keystr[j])//R;5)2;F%Ed#,T-4.>M04UUnextLxW*
  144. {
  145. pos+=next[src[pos+len_key]];
  146. break;
  147. }
  148. }
  149. if(j==len_key)
  150. return pos;
  151. }
  152. return -1;//N^WS4.Tr75;X-1
  153. }
  154. DWORD GetMyTickCount()
  155. {
  156. return timeGetTime();
  157. }
  158. HBITMAP OutPutToBitMap16(BYTE* pBits,LONG width,LONG height)
  159. {
  160. BITMAPINFO bmpInfo;
  161. HBITMAP hBackBitmap;//
  162. //HDC hBackDC;
  163. LPVOID pBitBits;
  164. BYTE *pBitMapBits;
  165. ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
  166. bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  167. bmpInfo.bmiHeader.biBitCount=16;
  168. bmpInfo.bmiHeader.biCompression = BI_RGB;
  169. bmpInfo.bmiHeader.biWidth=width;
  170. bmpInfo.bmiHeader.biHeight=height;
  171. bmpInfo.bmiHeader.biPlanes=1;
  172. bmpInfo.bmiHeader.biSizeImage=abs(bmpInfo.bmiHeader.biHeight)*bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biBitCount/8;
  173. HDC hdc=GetDC(GetDesktopWindow());
  174. //hBackDC=CreateCompatibleDC(hdc);
  175. hBackBitmap= CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,&(pBitBits),NULL,0);
  176. if(hBackBitmap==NULL)
  177. {
  178. MessageBox(NULL,"Unable to Create BackBuffer Bitamp","ERROR",MB_OK); return FALSE;
  179. }
  180. ReleaseDC(GetDesktopWindow(),hdc);
  181. //copy it
  182. pBitMapBits = (BYTE *)pBitBits;
  183. for(int i=0;i<height;i++)
  184. {
  185. memcpy((BYTE*)pBitBits+(i)*width*BITSPERPIXEL/16,
  186. (BYTE*)pBits+(i)*width*BITSPERPIXEL/16,
  187. width*BITSPERPIXEL/16);
  188. }
  189. //do the bitblt
  190. return hBackBitmap;
  191. }
  192. HBITMAP OutPutToBitMap(BYTE* pBits,LONG width,LONG height)
  193. {
  194. BITMAPINFO bmpInfo;
  195. HBITMAP hBackBitmap;//
  196. //HDC hBackDC;
  197. LPVOID pBitBits;
  198. BYTE *pBitMapBits;
  199. ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
  200. bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  201. bmpInfo.bmiHeader.biBitCount=BITSPERPIXEL;
  202. bmpInfo.bmiHeader.biCompression = BI_RGB;
  203. bmpInfo.bmiHeader.biWidth=width;
  204. bmpInfo.bmiHeader.biHeight=height;
  205. bmpInfo.bmiHeader.biPlanes=1;
  206. bmpInfo.bmiHeader.biSizeImage=abs(bmpInfo.bmiHeader.biHeight)*bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biBitCount/8;
  207. HDC hdc=GetDC(GetDesktopWindow());
  208. //hBackDC=CreateCompatibleDC(hdc);
  209. hBackBitmap= CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,&(pBitBits),NULL,0);
  210. if(hBackBitmap==NULL)
  211. {
  212. MessageBox(NULL,"Unable to Create BackBuffer Bitamp","ERROR",MB_OK); return FALSE;
  213. }
  214. ReleaseDC(GetDesktopWindow(),hdc);
  215. //copy it
  216. pBitMapBits = (BYTE *)pBitBits;
  217. for(int i=0;i<height;i++)
  218. {
  219. memcpy((BYTE*)pBitBits+(i)*width*BITSPERPIXEL/8,
  220. (BYTE*)pBits+(i)*width*BITSPERPIXEL/8,
  221. width*BITSPERPIXEL/8);
  222. }
  223. //do the bitblt
  224. return hBackBitmap;
  225. }
  226. BOOL SaveImageDataToBMP( TCHAR *lpFileName, DWORD width, DWORD height, DWORD bits, void* pData )
  227. {
  228. if( bits != 32 )//bits != 24 &&
  229. return FALSE;
  230. BITMAPFILEHEADER bfh;
  231. ZeroMemory( &bfh, sizeof(BITMAPFILEHEADER) );
  232. bfh.bfType = ((WORD)('M'<<8)|'B');
  233. bfh.bfSize = 0;
  234. bfh.bfReserved2 = 0;
  235. bfh.bfReserved1 = 0;
  236. bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  237. BITMAPINFO BitmapInfo;
  238. BitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  239. BitmapInfo.bmiHeader.biPlanes = 1;
  240. BitmapInfo.bmiHeader.biBitCount = (WORD)bits;
  241. BitmapInfo.bmiHeader.biCompression = BI_RGB;
  242. BitmapInfo.bmiHeader.biSizeImage = 0;
  243. BitmapInfo.bmiHeader.biXPelsPerMeter = 0;
  244. BitmapInfo.bmiHeader.biYPelsPerMeter = 0;
  245. BitmapInfo.bmiHeader.biClrUsed = 0;
  246. BitmapInfo.bmiHeader.biClrImportant = 0;
  247. BitmapInfo.bmiHeader.biWidth = width;
  248. BitmapInfo.bmiHeader.biHeight = height;
  249. int dwPitch = width * bits / 8;
  250. while( dwPitch % 4 != 0 )
  251. dwPitch++;
  252. FILE* pFile = _tfopen( lpFileName, _T("wb") );
  253. fwrite( &bfh, sizeof(BITMAPFILEHEADER), 1, pFile );
  254. fwrite( &BitmapInfo, sizeof(BITMAPINFOHEADER), 1, pFile );
  255. fwrite( pData, sizeof(char), height * dwPitch, pFile );
  256. fclose( pFile );
  257. return TRUE;
  258. }
  259. BOOL SaveBitmap32To24(TCHAR *szFilename,BYTE* pBits,LONG width,LONG height)
  260. {
  261. BITMAPINFO bmpInfo;
  262. HBITMAP hBackBitmap;//
  263. LPVOID pBitBits;
  264. BYTE *pBitMapBits;
  265. ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
  266. bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  267. bmpInfo.bmiHeader.biBitCount=32;
  268. bmpInfo.bmiHeader.biCompression = BI_RGB;
  269. bmpInfo.bmiHeader.biWidth=width;
  270. bmpInfo.bmiHeader.biHeight=height;
  271. bmpInfo.bmiHeader.biPlanes=1;
  272. bmpInfo.bmiHeader.biSizeImage=0;//abs(bmpInfo.bmiHeader.biHeight)*bmpInfo.bmiHeader.biWidth*4;
  273. HDC hdc=GetDC(GetDesktopWindow());
  274. hBackBitmap= CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,&(pBitBits),NULL,0);
  275. if(hBackBitmap==NULL)
  276. {
  277. MessageBox(NULL,"Unable to Create BackBuffer Bitamp","ERROR",MB_OK); return FALSE;
  278. }
  279. ReleaseDC(GetDesktopWindow(),hdc);
  280. //copy it
  281. pBitMapBits = (BYTE *)pBitBits;
  282. LONG pointindex = 0;
  283. for(int i=0;i<height;i++)
  284. {
  285. for(int j = 0;j < width;j++)
  286. {
  287. pointindex = (i * width + j);
  288. //cpy(RGB,RGBA)
  289. memcpy(&pBitMapBits[pointindex*(BITMAPBITSPERPIXEL/8)],&pBits[pointindex*4],BITMAPBITSPERPIXEL/8);
  290. }
  291. }
  292. BOOL ret = SaveBitmap(szFilename,hBackBitmap);
  293. if(hBackBitmap)
  294. {
  295. DeleteObject(hBackBitmap);
  296. }
  297. return ret;
  298. }
  299. VOID GetMainDirctory(TCHAR *szDir)
  300. {
  301. TCHAR csCurrentPath[MAX_PATH] = {0};
  302. ::GetModuleFileName(NULL,csCurrentPath,MAX_PATH);
  303. //cut off last \
  304. //
  305. for(size_t i = strlen(csCurrentPath) - 1;i > 0;i--)
  306. {
  307. if(csCurrentPath[i] == '/')
  308. {
  309. csCurrentPath[i] = 0;
  310. break;
  311. }
  312. }
  313. strcpy(szDir,csCurrentPath);
  314. }
  315. void TrimTheA(PBYTE pBuff,DWORD width,DWORD height)
  316. {
  317. LONG pointindex = 0;
  318. LONG pointindex32 = 0;
  319. DWORD RealWidth = width*4;
  320. for(DWORD i=0;i<height;i++)
  321. {
  322. for(DWORD j = 0;j < width;j++)
  323. {
  324. pointindex = (i * RealWidth + j*(32/8));
  325. //erase last one
  326. pBuff[pointindex + 3] = 0;
  327. }
  328. }
  329. }
  330. PBYTE LoadBitMapFromFile(const TCHAR *szFilename,DWORD &width,DWORD &height)
  331. {
  332. BITMAPFILEHEADER bmpFileHeader;
  333. BITMAPINFOHEADER bmpInfo;
  334. PBYTE pBuff = NULL;
  335. PBYTE p32BitmapBuff = NULL;
  336. DWORD buffSize = 0;
  337. DWORD returnedLenth = 0;
  338. HANDLE fp = CreateFile(szFilename,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  339. if(fp==INVALID_HANDLE_VALUE)
  340. {
  341. MessageBox(NULL,"Unable to Open Bitmap File","Error",MB_OK|MB_ICONERROR);
  342. return NULL;
  343. }
  344. ReadFile(fp,&bmpFileHeader,sizeof(BITMAPFILEHEADER),&returnedLenth,0);
  345. if(returnedLenth != sizeof(BITMAPFILEHEADER))
  346. {
  347. MessageBox(NULL,"Wrong Bitmap File Format ","Error",MB_OK|MB_ICONERROR);
  348. CloseHandle(fp);
  349. return NULL;
  350. }
  351. ReadFile(fp,&bmpInfo,sizeof(BITMAPINFOHEADER),&returnedLenth,0);
  352. if(returnedLenth != sizeof(BITMAPINFOHEADER) || bmpFileHeader.bfType != 'MB')
  353. {
  354. MessageBox(NULL,"Wrong Bitmap File Format ","Error",MB_OK|MB_ICONERROR);
  355. CloseHandle(fp);
  356. return NULL;
  357. }
  358. pBuff = new BYTE[bmpInfo.biSizeImage];
  359. width = bmpInfo.biWidth;
  360. height = bmpInfo.biHeight;
  361. DWORD RealWidth = bmpInfo.biSizeImage / height;
  362. ReadFile(fp,pBuff,bmpInfo.biSizeImage,&returnedLenth,0);
  363. if(returnedLenth != bmpInfo.biSizeImage)
  364. {
  365. MessageBox(NULL,"Wrong Bitmap File Format ","Error",MB_OK|MB_ICONERROR);
  366. CloseHandle(fp);
  367. delete []pBuff;
  368. return NULL;
  369. }
  370. LONG pointindex = 0;
  371. LONG pointindex32 = 0;
  372. p32BitmapBuff = new BYTE[width*height*sizeof(COLORREF)];
  373. memset(p32BitmapBuff,0,width*height*sizeof(COLORREF));
  374. for(DWORD i=0;i<height;i++)
  375. {
  376. for(DWORD j = 0;j < width;j++)
  377. {
  378. pointindex = (i * RealWidth + j*(BITMAPBITSPERPIXEL/8));
  379. pointindex32 = (i *width + j);
  380. //cpy(RGB,RGBA)
  381. memcpy(&p32BitmapBuff[pointindex32*(BITSPERPIXEL/8)],&pBuff[pointindex],BITMAPBITSPERPIXEL/8);
  382. }
  383. }
  384. CloseHandle(fp);
  385. delete []pBuff;
  386. return p32BitmapBuff;
  387. }
  388. BOOL SaveBitmap(TCHAR *szFilename,HBITMAP hBitmap)
  389. {
  390. HDC hdc=NULL;
  391. HANDLE fp=NULL;
  392. LPVOID pBuf=NULL;
  393. BITMAPINFO bmpInfo;
  394. BITMAPFILEHEADER bmpFileHeader;
  395. DWORD returnedLenth = 0;
  396. BOOL bret = TRUE;
  397. TCHAR csCurrentPath[MAX_PATH] = {0};
  398. ::GetModuleFileName(NULL,csCurrentPath,MAX_PATH);
  399. //cut off last \
  400. //
  401. for(int i = (int)strlen(csCurrentPath) - 1;i > 0;i--)
  402. {
  403. if(csCurrentPath[i] == '/')
  404. {
  405. csCurrentPath[i] = 0;
  406. break;
  407. }
  408. }
  409. strcat(csCurrentPath,"/");
  410. strcat(csCurrentPath, szFilename);
  411. do{
  412. if(szFilename == NULL)
  413. {
  414. break;
  415. }
  416. hdc=GetDC(NULL);
  417. ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
  418. bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  419. GetDIBits(hdc,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS);
  420. if(bmpInfo.bmiHeader.biSizeImage<=0)
  421. bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8;
  422. if((pBuf=malloc(bmpInfo.bmiHeader.biSizeImage))==NULL)
  423. {
  424. MessageBox(NULL,"Unable to Allocate Bitmap Memory","Error",MB_OK|MB_ICONERROR);
  425. bret = FALSE;
  426. break;
  427. }
  428. bmpInfo.bmiHeader.biCompression=BI_RGB;
  429. GetDIBits(hdc,hBitmap,0,bmpInfo.bmiHeader.biHeight,pBuf,&bmpInfo,DIB_RGB_COLORS);
  430. fp = CreateFile(csCurrentPath,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
  431. if(fp==INVALID_HANDLE_VALUE)
  432. {
  433. MessageBox(NULL,"Unable to Create Bitmap File","Error",MB_OK|MB_ICONERROR);
  434. bret = FALSE;
  435. break;
  436. }
  437. bmpFileHeader.bfReserved1=0;
  438. bmpFileHeader.bfReserved2=0;
  439. bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
  440. bmpFileHeader.bfType='MB';
  441. bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
  442. WriteFile(fp,&bmpFileHeader,sizeof(BITMAPFILEHEADER),&returnedLenth,0);
  443. //fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
  444. WriteFile(fp,&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),&returnedLenth,0);
  445. //fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
  446. WriteFile(fp,pBuf,bmpInfo.bmiHeader.biSizeImage,&returnedLenth,0);
  447. //fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp);
  448. }while(false);
  449. if(hdc)
  450. ReleaseDC(NULL,hdc);
  451. if(pBuf)
  452. free(pBuf);
  453. if(fp && fp != INVALID_HANDLE_VALUE)
  454. CloseHandle(fp);
  455. return bret;
  456. }
  457. BOOL GetWcharFromChar(WCHAR *pWcharBuff,DWORD nWcharBuffLen,const char *pChar)
  458. {
  459. if(!pWcharBuff || nWcharBuffLen == 0 )
  460. {
  461. return FALSE;
  462. }
  463. std::wstring str;
  464. int uLen = MultiByteToWideChar(CP_UTF8,0,pChar,-1,NULL,0);
  465. if(uLen > 0 && uLen < (INT)nWcharBuffLen)
  466. {
  467. str.resize(uLen);
  468. MultiByteToWideChar(CP_UTF8,0,pChar,-1, (LPWSTR )&str[0],uLen);
  469. wcsncpy(pWcharBuff,str.data(),nWcharBuffLen);
  470. return TRUE;
  471. }
  472. return FALSE;
  473. }
  474. BOOL ConvWcharToChar(LPSTR pCharBuff,DWORD nCharBuffLen,WCHAR *pUniStr)
  475. {
  476. if(!pCharBuff || nCharBuffLen == 0 )
  477. {
  478. return FALSE;
  479. }
  480. int uLen = WideCharToMultiByte(CP_UTF8,0,pUniStr,-1,NULL,0,NULL,NULL);
  481. if(uLen > 0 && uLen < (INT)nCharBuffLen)
  482. {
  483. WideCharToMultiByte(CP_UTF8,0,pUniStr,-1, (LPSTR)pCharBuff,uLen,NULL,NULL);
  484. return TRUE;
  485. }
  486. return FALSE;
  487. }
  488. DWORD WINAPI TheSystemShutdown(LPVOID pPara)
  489. {
  490. HANDLE hToken;
  491. TOKEN_PRIVILEGES tkp;
  492. // Get a token for this process.
  493. if (!OpenProcessToken(GetCurrentProcess(),
  494. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  495. return 0;
  496. // Get the LUID for the shutdown privilege.
  497. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  498. &tkp.Privileges[0].Luid);
  499. tkp.PrivilegeCount = 1; // one privilege to set
  500. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  501. // Get the shutdown privilege for this process.
  502. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  503. (PTOKEN_PRIVILEGES)NULL, 0);
  504. if (GetLastError() != ERROR_SUCCESS)
  505. return 0;
  506. // Shut down the system and force all applications to close.
  507. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE,
  508. 0))
  509. return 0;
  510. return 1;
  511. }
  512. DWORD WINAPI TheSystemReboot(LPVOID pPara)
  513. {
  514. HANDLE hToken;
  515. TOKEN_PRIVILEGES tkp;
  516. // Get a token for this process.
  517. if (!OpenProcessToken(GetCurrentProcess(),
  518. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
  519. return 0;
  520. // Get the LUID for the shutdown privilege.
  521. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
  522. &tkp.Privileges[0].Luid);
  523. tkp.PrivilegeCount = 1; // one privilege to set
  524. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  525. // Get the shutdown privilege for this process.
  526. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
  527. (PTOKEN_PRIVILEGES)NULL, 0);
  528. if (GetLastError() != ERROR_SUCCESS)
  529. return 0;
  530. // Shut down the system and force all applications to close.
  531. if (!ExitWindowsEx(EWX_REBOOT | EWX_FORCE,
  532. 0))
  533. return 0;
  534. return 1;
  535. }
  536. char GetXorKeyWithString(char *pCharBuff,size_t len)
  537. {
  538. size_t i = 0;
  539. char xkey = 0;
  540. for(i =0;i < len;i++)
  541. {
  542. xkey ^= pCharBuff[i];
  543. }
  544. return xkey;
  545. }
  546. void EncryptWithXorKey(char key,char *pCharBuff,UINT len)
  547. {
  548. UINT i = 0;
  549. for(i =0;i < len;i++)
  550. {
  551. pCharBuff[i] ^= key;
  552. }
  553. }
  554. void DoTheCrapyWithPassword(char *pPassword,char *pCharBuff,UINT len)
  555. {
  556. char key = GetXorKeyWithString(pPassword,strlen(pPassword));
  557. EncryptWithXorKey(key,pCharBuff,len);
  558. }
  559. INT GetRectWidth(RECT &rect)
  560. {
  561. return (rect.right - rect.left + 1);
  562. }
  563. INT GetRectHeight(RECT &rect)
  564. {
  565. return (rect.bottom - rect.top + 1);
  566. }
  567. void NormalizeRect(RECT &rect)
  568. {
  569. LONG temp;
  570. if (rect.right < rect.left)
  571. {
  572. temp = rect.left;
  573. rect.left = rect.right;
  574. rect.right = temp;
  575. }
  576. if (rect.bottom < rect.top)
  577. {
  578. temp = rect.bottom;
  579. rect.bottom = rect.top;
  580. rect.top = temp;
  581. }
  582. }
  583. POINT GetRectTopLeft(RECT &rect)
  584. {
  585. POINT pt;
  586. pt.x = rect.left;
  587. pt.y = rect.top;
  588. return pt;
  589. }
  590. POINT GetRectBottomRight(RECT &rect)
  591. {
  592. POINT pt;
  593. pt.x = rect.right;
  594. pt.y = rect.bottom;
  595. return pt;
  596. }
  597. #pragma warning(pop)
  598. Common_Funcs g_Common_Funcs;