#include "StdAfx.h" #include #include #include #include #include "common_funcs.h" //#include "debug.h" #include #pragma comment(lib, "winmm.lib") #pragma warning(push) #pragma warning(disable:4996) Common_Funcs::Common_Funcs(void) { } Common_Funcs::~Common_Funcs(void) { } void GetModulePath(string &path) { TCHAR szPathBuffer[MAX_PATH] = {0}; ::GetModuleFileName(NULL, szPathBuffer, MAX_PATH); string prc = szPathBuffer; std::size_t found = prc.find_last_of("/"); path = prc.substr(0,found+1); } UINT64 GetC_M_N(UINT64 M,UINT64 N) { UINT64 d = 1; UINT64 c = 1; for(UINT64 i = N;i > (N - M);i --) { c = c * i; } for(UINT64 i = 1;i <= M;i++) { c = c / i; } //ASSERT(0);//need test return c; } int GetRandomNumber(int Begin,int End) { srand(GetTickCount()); return rand() % (End - Begin + 1) + Begin; } DWORD WaitForObjectAcurateTime(HANDLE event,DWORD dwMilliseconds) { double elapsed; DWORD passedTime = 0; LARGE_INTEGER startCount; LARGE_INTEGER endCount; LARGE_INTEGER freq; QueryPerformanceFrequency(&freq); while(passedTime < dwMilliseconds) { passedTime = 0; QueryPerformanceCounter(&startCount); if(WaitForSingleObject(event,dwMilliseconds) == WAIT_OBJECT_0) { return WAIT_OBJECT_0; } QueryPerformanceCounter(&endCount); elapsed = (double)(endCount.QuadPart - startCount.QuadPart) / freq.QuadPart; passedTime = (DWORD)(elapsed * 1000); if(dwMilliseconds > passedTime) { dwMilliseconds -= passedTime; } else { break; } } return WAIT_TIMEOUT; } BOOL StrToInt(char * str,int *result) { int value = 0; int sign = 1; int radix; if(*str == '-') { sign = -1; str++; } // 16=xVF5DJWWVD8JGx;rU_0x if(*str == '0' && (*(str+1) == 'x' || *(str+1) == 'X')) { radix = 16; str += 2; } else if(*str == '0') // 0K=xVFJWWV7{N*0 { radix = 8; str++; } else { radix = 10; } while(*str) { if(radix == 16) { if(*str >= '0' && *str <= '9') { value = value * radix + *str - '0'; } else { if((*str | 0x20) >= 'a' && (*str | 0x20) <= 'f') { value = value * radix + (*str | 0x20) - 'a' + 10; } else { return FALSE; } } } else { value = value * radix + *str - '0'; } str++; } *result = sign*value; return TRUE; } int sundaySearch(const unsigned char *src,int len_s,const unsigned char *keystr,int len_key) { int i,j,pos=0; int next[256]={0};//nextJ}Wi#,T$4&@zOuJ<;/ for(j=0;j<256;++j)//3uJ<;/nextJ}?E next[j]=len_key+1; for(j=0;jM04UUnextLxW* { pos+=next[src[pos+len_key]]; break; } } if(j==len_key) return pos; } return -1;//N^WS4.Tr75;X-1 } DWORD GetMyTickCount() { return timeGetTime(); } HBITMAP OutPutToBitMap16(BYTE* pBits,LONG width,LONG height) { BITMAPINFO bmpInfo; HBITMAP hBackBitmap;// //HDC hBackDC; LPVOID pBitBits; BYTE *pBitMapBits; ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biBitCount=16; bmpInfo.bmiHeader.biCompression = BI_RGB; bmpInfo.bmiHeader.biWidth=width; bmpInfo.bmiHeader.biHeight=height; bmpInfo.bmiHeader.biPlanes=1; bmpInfo.bmiHeader.biSizeImage=abs(bmpInfo.bmiHeader.biHeight)*bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biBitCount/8; HDC hdc=GetDC(GetDesktopWindow()); //hBackDC=CreateCompatibleDC(hdc); hBackBitmap= CreateDIBSection(hdc,&bmpInfo,DIB_RGB_COLORS,&(pBitBits),NULL,0); if(hBackBitmap==NULL) { MessageBox(NULL,"Unable to Create BackBuffer Bitamp","ERROR",MB_OK); return FALSE; } ReleaseDC(GetDesktopWindow(),hdc); //copy it pBitMapBits = (BYTE *)pBitBits; for(int i=0;i 0;i--) { if(csCurrentPath[i] == '/') { csCurrentPath[i] = 0; break; } } strcpy(szDir,csCurrentPath); } void TrimTheA(PBYTE pBuff,DWORD width,DWORD height) { LONG pointindex = 0; LONG pointindex32 = 0; DWORD RealWidth = width*4; for(DWORD i=0;i 0;i--) { if(csCurrentPath[i] == '/') { csCurrentPath[i] = 0; break; } } strcat(csCurrentPath,"/"); strcat(csCurrentPath, szFilename); do{ if(szFilename == NULL) { break; } hdc=GetDC(NULL); ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); GetDIBits(hdc,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS); if(bmpInfo.bmiHeader.biSizeImage<=0) bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*(bmpInfo.bmiHeader.biBitCount+7)/8; if((pBuf=malloc(bmpInfo.bmiHeader.biSizeImage))==NULL) { MessageBox(NULL,"Unable to Allocate Bitmap Memory","Error",MB_OK|MB_ICONERROR); bret = FALSE; break; } bmpInfo.bmiHeader.biCompression=BI_RGB; GetDIBits(hdc,hBitmap,0,bmpInfo.bmiHeader.biHeight,pBuf,&bmpInfo,DIB_RGB_COLORS); fp = CreateFile(csCurrentPath,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0); if(fp==INVALID_HANDLE_VALUE) { MessageBox(NULL,"Unable to Create Bitmap File","Error",MB_OK|MB_ICONERROR); bret = FALSE; break; } bmpFileHeader.bfReserved1=0; bmpFileHeader.bfReserved2=0; bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage; bmpFileHeader.bfType='MB'; bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); WriteFile(fp,&bmpFileHeader,sizeof(BITMAPFILEHEADER),&returnedLenth,0); //fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp); WriteFile(fp,&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),&returnedLenth,0); //fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp); WriteFile(fp,pBuf,bmpInfo.bmiHeader.biSizeImage,&returnedLenth,0); //fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp); }while(false); if(hdc) ReleaseDC(NULL,hdc); if(pBuf) free(pBuf); if(fp && fp != INVALID_HANDLE_VALUE) CloseHandle(fp); return bret; } BOOL GetWcharFromChar(WCHAR *pWcharBuff,DWORD nWcharBuffLen,const char *pChar) { if(!pWcharBuff || nWcharBuffLen == 0 ) { return FALSE; } std::wstring str; int uLen = MultiByteToWideChar(CP_UTF8,0,pChar,-1,NULL,0); if(uLen > 0 && uLen < (INT)nWcharBuffLen) { str.resize(uLen); MultiByteToWideChar(CP_UTF8,0,pChar,-1, (LPWSTR )&str[0],uLen); wcsncpy(pWcharBuff,str.data(),nWcharBuffLen); return TRUE; } return FALSE; } BOOL ConvWcharToChar(LPSTR pCharBuff,DWORD nCharBuffLen,WCHAR *pUniStr) { if(!pCharBuff || nCharBuffLen == 0 ) { return FALSE; } int uLen = WideCharToMultiByte(CP_UTF8,0,pUniStr,-1,NULL,0,NULL,NULL); if(uLen > 0 && uLen < (INT)nCharBuffLen) { WideCharToMultiByte(CP_UTF8,0,pUniStr,-1, (LPSTR)pCharBuff,uLen,NULL,NULL); return TRUE; } return FALSE; } DWORD WINAPI TheSystemShutdown(LPVOID pPara) { HANDLE hToken; TOKEN_PRIVILEGES tkp; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return 0; // Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if (GetLastError() != ERROR_SUCCESS) return 0; // Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) return 0; return 1; } DWORD WINAPI TheSystemReboot(LPVOID pPara) { HANDLE hToken; TOKEN_PRIVILEGES tkp; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return 0; // Get the LUID for the shutdown privilege. LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); tkp.PrivilegeCount = 1; // one privilege to set tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; // Get the shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if (GetLastError() != ERROR_SUCCESS) return 0; // Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_REBOOT | EWX_FORCE, 0)) return 0; return 1; } char GetXorKeyWithString(char *pCharBuff,size_t len) { size_t i = 0; char xkey = 0; for(i =0;i < len;i++) { xkey ^= pCharBuff[i]; } return xkey; } void EncryptWithXorKey(char key,char *pCharBuff,UINT len) { UINT i = 0; for(i =0;i < len;i++) { pCharBuff[i] ^= key; } } void DoTheCrapyWithPassword(char *pPassword,char *pCharBuff,UINT len) { char key = GetXorKeyWithString(pPassword,strlen(pPassword)); EncryptWithXorKey(key,pCharBuff,len); } INT GetRectWidth(RECT &rect) { return (rect.right - rect.left + 1); } INT GetRectHeight(RECT &rect) { return (rect.bottom - rect.top + 1); } void NormalizeRect(RECT &rect) { LONG temp; if (rect.right < rect.left) { temp = rect.left; rect.left = rect.right; rect.right = temp; } if (rect.bottom < rect.top) { temp = rect.bottom; rect.bottom = rect.top; rect.top = temp; } } POINT GetRectTopLeft(RECT &rect) { POINT pt; pt.x = rect.left; pt.y = rect.top; return pt; } POINT GetRectBottomRight(RECT &rect) { POINT pt; pt.x = rect.right; pt.y = rect.bottom; return pt; } #pragma warning(pop) Common_Funcs g_Common_Funcs;