Misc.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. #include "stdafx.h"
  2. #include "CP_Main.h"
  3. #include "Misc.h"
  4. #include "OptionsSheet.h"
  5. #include "shared/TextConvert.h"
  6. #include "AlphaBlend.h"
  7. #include "Tlhelp32.h"
  8. CString GetIPAddress()
  9. {
  10. WORD wVersionRequested;
  11. WSADATA wsaData;
  12. char name[255];
  13. CString IP;
  14. PHOSTENT hostinfo;
  15. wVersionRequested = MAKEWORD(2,0);
  16. if (WSAStartup(wVersionRequested, &wsaData)==0)
  17. {
  18. if(gethostname(name, sizeof(name))==0)
  19. {
  20. if((hostinfo=gethostbyname(name)) != NULL)
  21. {
  22. IP = inet_ntoa(*(struct in_addr*)* hostinfo->h_addr_list);
  23. }
  24. }
  25. WSACleanup();
  26. }
  27. IP.MakeUpper();
  28. return IP;
  29. }
  30. CString GetComputerName()
  31. {
  32. TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH+1] = _T("");
  33. DWORD Size=MAX_COMPUTERNAME_LENGTH+1;
  34. GetComputerName(ComputerName, &Size);
  35. CString cs(ComputerName);
  36. cs.MakeUpper();
  37. return cs;
  38. }
  39. void AppendToFile(const TCHAR* fn, const TCHAR* msg)
  40. {
  41. #ifdef _UNICODE
  42. FILE *file = _wfopen(fn, _T("a"));
  43. #else
  44. FILE *file = fopen(fn, _T("a"));
  45. #endif
  46. ASSERT( file );
  47. if(file != NULL)
  48. {
  49. #ifdef _UNICODE
  50. fwprintf(file, _T("%s"), msg);
  51. #else
  52. fprintf(file, _T("%s"),msg);
  53. #endif
  54. fclose(file);
  55. }
  56. }
  57. void log(const TCHAR* msg, bool bFromSendRecieve, CString csFile, long lLine)
  58. {
  59. ASSERT(AfxIsValidString(msg));
  60. SYSTEMTIME st;
  61. GetLocalTime(&st);
  62. CString csText;
  63. csText.Format(_T("[%d/%d/%d %02d:%02d:%02d.%03d - "), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
  64. CString csFileLine;
  65. csFile = GetFileName(csFile);
  66. csFileLine.Format(_T("%s %d] "), csFile, lLine);
  67. csText += csFileLine;
  68. csText += msg;
  69. csText += "\n";
  70. #ifndef _DEBUG
  71. if(CGetSetOptions::m_bOutputDebugString)
  72. #endif
  73. {
  74. OutputDebugString(csText);
  75. }
  76. #ifndef _DEBUG
  77. if(!bFromSendRecieve)
  78. {
  79. if(!g_Opt.m_bEnableDebugLogging)
  80. return;
  81. }
  82. #endif
  83. CString csExeFile = CGetSetOptions::GetPath(PATH_LOG_FILE);
  84. csExeFile += "Ditto.log";
  85. AppendToFile(csExeFile, csText);
  86. }
  87. void logsendrecieveinfo(CString cs, CString csFile, long lLine)
  88. {
  89. if(g_Opt.m_bLogSendReceiveErrors)
  90. log(cs, true, csFile, lLine);
  91. }
  92. CString GetErrorString( int err )
  93. {
  94. CString str;
  95. LPVOID lpMsgBuf;
  96. ::FormatMessage(
  97. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  98. NULL,
  99. err,
  100. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  101. (LPTSTR) &lpMsgBuf,
  102. 0,
  103. NULL
  104. );
  105. str = (LPCTSTR) lpMsgBuf;
  106. // Display the string.
  107. // ::MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
  108. ::LocalFree( lpMsgBuf );
  109. return str;
  110. }
  111. int g_funnyGetTickCountAdjustment = -1;
  112. double IdleSeconds()
  113. {
  114. LASTINPUTINFO info;
  115. info.cbSize = sizeof(info);
  116. GetLastInputInfo(&info);
  117. DWORD currentTick = GetTickCount();
  118. if(g_funnyGetTickCountAdjustment == -1)
  119. {
  120. if(currentTick < info.dwTime)
  121. {
  122. g_funnyGetTickCountAdjustment = 1;
  123. }
  124. else
  125. {
  126. g_funnyGetTickCountAdjustment = 0;
  127. }
  128. }
  129. if(g_funnyGetTickCountAdjustment == 1 || g_funnyGetTickCountAdjustment == 2)
  130. {
  131. //Output message the first time
  132. if(g_funnyGetTickCountAdjustment == 1)
  133. {
  134. Log(StrF(_T("Adjusting time of get tickcount by: %d, on startup we found GetTickCount to be less than last input"), CGetSetOptions::GetFunnyTickCountAdjustment()));
  135. g_funnyGetTickCountAdjustment = 2;
  136. }
  137. currentTick += CGetSetOptions::GetFunnyTickCountAdjustment();
  138. }
  139. double idleSeconds = (currentTick - info.dwTime)/1000.0;
  140. return idleSeconds;
  141. }
  142. CString StrF(const TCHAR * pszFormat, ...)
  143. {
  144. ASSERT( AfxIsValidString( pszFormat ) );
  145. CString str;
  146. va_list argList;
  147. va_start( argList, pszFormat );
  148. str.FormatV( pszFormat, argList );
  149. va_end( argList );
  150. return str;
  151. }
  152. BYTE GetEscapeChar( BYTE ch )
  153. {
  154. switch(ch)
  155. {
  156. case '\'': return '\''; // Single quotation mark (') = 39 or 0x27
  157. case '\"': return '\"'; // Double quotation mark (") = 34 or 0x22
  158. case '?': return '\?'; // Question mark (?) = 63 or 0x3f
  159. case '\\': return '\\'; // Backslash (\) = 92 or 0x5c
  160. case 'a': return '\a'; // Alert (BEL) = 7
  161. case 'b': return '\b'; // Backspace (BS) = 8
  162. case 'f': return '\f'; // Formfeed (FF) = 12 or 0x0c
  163. case 'n': return '\n'; // Newline (NL or LF) = 10 or 0x0a
  164. case 'r': return '\r'; // Carriage Return (CR) = 13 or 0x0d
  165. case 't': return '\t'; // Horizontal tab (HT) = 9
  166. case 'v': return '\v'; // Vertical tab (VT) = 11 or 0x0b
  167. case '0': return '\0'; // Null character (NUL) = 0
  168. }
  169. return 0; // invalid
  170. }
  171. CString RemoveEscapes( const TCHAR* str )
  172. {
  173. ASSERT( str );
  174. CString ret;
  175. TCHAR* pSrc = (TCHAR*) str;
  176. TCHAR* pDest = ret.GetBuffer((int)STRLEN(pSrc));
  177. TCHAR* pStart = pDest;
  178. while( *pSrc != '\0' )
  179. {
  180. if( *pSrc == '\\' )
  181. {
  182. pSrc++;
  183. *pDest = GetEscapeChar((BYTE)pSrc );
  184. }
  185. else
  186. *pDest = *pSrc;
  187. pSrc++;
  188. pDest++;
  189. }
  190. ret.ReleaseBuffer((int)(pDest - pStart));
  191. return ret;
  192. }
  193. CString GetWndText( HWND hWnd )
  194. {
  195. CString text;
  196. if( !IsWindow(hWnd) )
  197. return "! NOT A VALID WINDOW !";
  198. CWnd* pWnd = CWnd::FromHandle(hWnd);
  199. pWnd->GetWindowText(text);
  200. return text;
  201. }
  202. bool IsAppWnd( HWND hWnd )
  203. {
  204. DWORD dwMyPID = ::GetCurrentProcessId();
  205. DWORD dwTestPID;
  206. ::GetWindowThreadProcessId( hWnd, &dwTestPID );
  207. return dwMyPID == dwTestPID;
  208. }
  209. /*----------------------------------------------------------------------------*\
  210. Global Memory Helper Functions
  211. \*----------------------------------------------------------------------------*/
  212. // make sure the given HGLOBAL is valid.
  213. BOOL IsValid(HGLOBAL hGlobal)
  214. {
  215. void* pvData = ::GlobalLock(hGlobal);
  216. ::GlobalUnlock(hGlobal);
  217. return (pvData != NULL);
  218. }
  219. // asserts if hDest isn't big enough
  220. void CopyToGlobalHP(HGLOBAL hDest, LPVOID pBuf, SIZE_T ulBufLen)
  221. {
  222. ASSERT(hDest && pBuf && ulBufLen);
  223. LPVOID pvData = GlobalLock(hDest);
  224. ASSERT(pvData);
  225. SIZE_T size = GlobalSize(hDest);
  226. ASSERT(size >= ulBufLen); // assert if hDest isn't big enough
  227. memcpy(pvData, pBuf, ulBufLen);
  228. GlobalUnlock(hDest);
  229. }
  230. void CopyToGlobalHH(HGLOBAL hDest, HGLOBAL hSource, SIZE_T ulBufLen)
  231. {
  232. ASSERT(hDest && hSource && ulBufLen);
  233. LPVOID pvData = GlobalLock(hSource);
  234. ASSERT(pvData );
  235. SIZE_T size = GlobalSize(hSource);
  236. ASSERT(size >= ulBufLen); // assert if hSource isn't big enough
  237. CopyToGlobalHP(hDest, pvData, ulBufLen);
  238. GlobalUnlock(hSource);
  239. }
  240. HGLOBAL NewGlobalP(LPVOID pBuf, SIZE_T nLen)
  241. {
  242. ASSERT(pBuf && nLen);
  243. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  244. ASSERT(hDest );
  245. CopyToGlobalHP(hDest, pBuf, nLen);
  246. return hDest;
  247. }
  248. HGLOBAL NewGlobal(SIZE_T nLen)
  249. {
  250. ASSERT(nLen);
  251. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  252. return hDest;
  253. }
  254. HGLOBAL NewGlobalH(HGLOBAL hSource, SIZE_T nLen)
  255. {
  256. ASSERT(hSource && nLen);
  257. LPVOID pvData = GlobalLock(hSource);
  258. HGLOBAL hDest = NewGlobalP(pvData, nLen);
  259. GlobalUnlock(hSource);
  260. return hDest;
  261. }
  262. int CompareGlobalHP(HGLOBAL hLeft, LPVOID pBuf, SIZE_T ulBufLen)
  263. {
  264. ASSERT(hLeft && pBuf && ulBufLen);
  265. LPVOID pvData = GlobalLock(hLeft);
  266. ASSERT(pvData);
  267. ASSERT(ulBufLen <= GlobalSize(hLeft));
  268. int result = memcmp(pvData, pBuf, ulBufLen);
  269. GlobalUnlock(hLeft);
  270. return result;
  271. }
  272. int CompareGlobalHH( HGLOBAL hLeft, HGLOBAL hRight, SIZE_T ulBufLen)
  273. {
  274. ASSERT(hLeft && hRight && ulBufLen);
  275. ASSERT(ulBufLen <= GlobalSize(hRight));
  276. LPVOID pvData = GlobalLock(hRight);
  277. ASSERT(pvData);
  278. int result = CompareGlobalHP(hLeft, pvData, ulBufLen);
  279. GlobalUnlock(hLeft);
  280. return result;
  281. }
  282. //Do not change these these are stored in the database
  283. CLIPFORMAT GetFormatID(LPCTSTR cbName)
  284. {
  285. if(STRCMP(cbName, _T("CF_TEXT")) == 0)
  286. return CF_TEXT;
  287. else if(STRCMP(cbName, _T("CF_METAFILEPICT")) == 0)
  288. return CF_METAFILEPICT;
  289. else if(STRCMP(cbName, _T("CF_SYLK")) == 0)
  290. return CF_SYLK;
  291. else if(STRCMP(cbName, _T("CF_DIF")) == 0)
  292. return CF_DIF;
  293. else if(STRCMP(cbName, _T("CF_TIFF")) == 0)
  294. return CF_TIFF;
  295. else if(STRCMP(cbName, _T("CF_OEMTEXT")) == 0)
  296. return CF_OEMTEXT;
  297. else if(STRCMP(cbName, _T("CF_DIB")) == 0)
  298. return CF_DIB;
  299. else if(STRCMP(cbName, _T("CF_PALETTE")) == 0)
  300. return CF_PALETTE;
  301. else if(STRCMP(cbName, _T("CF_PENDATA")) == 0)
  302. return CF_PENDATA;
  303. else if(STRCMP(cbName, _T("CF_RIFF")) == 0)
  304. return CF_RIFF;
  305. else if(STRCMP(cbName, _T("CF_WAVE")) == 0)
  306. return CF_WAVE;
  307. else if(STRCMP(cbName, _T("CF_UNICODETEXT")) == 0)
  308. return CF_UNICODETEXT;
  309. else if(STRCMP(cbName, _T("CF_ENHMETAFILE")) == 0)
  310. return CF_ENHMETAFILE;
  311. else if(STRCMP(cbName, _T("CF_HDROP")) == 0)
  312. return CF_HDROP;
  313. else if(STRCMP(cbName, _T("CF_LOCALE")) == 0)
  314. return CF_LOCALE;
  315. else if(STRCMP(cbName, _T("CF_OWNERDISPLAY")) == 0)
  316. return CF_OWNERDISPLAY;
  317. else if(STRCMP(cbName, _T("CF_DSPTEXT")) == 0)
  318. return CF_DSPTEXT;
  319. else if(STRCMP(cbName, _T("CF_DSPBITMAP")) == 0)
  320. return CF_DSPBITMAP;
  321. else if(STRCMP(cbName, _T("CF_DSPMETAFILEPICT")) == 0)
  322. return CF_DSPMETAFILEPICT;
  323. else if(STRCMP(cbName, _T("CF_DSPENHMETAFILE")) == 0)
  324. return CF_DSPENHMETAFILE;
  325. return ::RegisterClipboardFormat(cbName);
  326. }
  327. //Do not change these these are stored in the database
  328. CString GetFormatName(CLIPFORMAT cbType)
  329. {
  330. switch(cbType)
  331. {
  332. case CF_TEXT:
  333. return _T("CF_TEXT");
  334. case CF_BITMAP:
  335. return _T("CF_BITMAP");
  336. case CF_METAFILEPICT:
  337. return _T("CF_METAFILEPICT");
  338. case CF_SYLK:
  339. return _T("CF_SYLK");
  340. case CF_DIF:
  341. return _T("CF_DIF");
  342. case CF_TIFF:
  343. return _T("CF_TIFF");
  344. case CF_OEMTEXT:
  345. return _T("CF_OEMTEXT");
  346. case CF_DIB:
  347. return _T("CF_DIB");
  348. case CF_PALETTE:
  349. return _T("CF_PALETTE");
  350. case CF_PENDATA:
  351. return _T("CF_PENDATA");
  352. case CF_RIFF:
  353. return _T("CF_RIFF");
  354. case CF_WAVE:
  355. return _T("CF_WAVE");
  356. case CF_UNICODETEXT:
  357. return _T("CF_UNICODETEXT");
  358. case CF_ENHMETAFILE:
  359. return _T("CF_ENHMETAFILE");
  360. case CF_HDROP:
  361. return _T("CF_HDROP");
  362. case CF_LOCALE:
  363. return _T("CF_LOCALE");
  364. case CF_OWNERDISPLAY:
  365. return _T("CF_OWNERDISPLAY");
  366. case CF_DSPTEXT:
  367. return _T("CF_DSPTEXT");
  368. case CF_DSPBITMAP:
  369. return _T("CF_DSPBITMAP");
  370. case CF_DSPMETAFILEPICT:
  371. return _T("CF_DSPMETAFILEPICT");
  372. case CF_DSPENHMETAFILE:
  373. return _T("CF_DSPENHMETAFILE");
  374. default:
  375. //Not a default type get the name from the clipboard
  376. if (cbType != 0)
  377. {
  378. TCHAR szFormat[256];
  379. GetClipboardFormatName(cbType, szFormat, 256);
  380. return szFormat;
  381. }
  382. break;
  383. }
  384. return "ERROR";
  385. }
  386. CString GetFilePath(CString csFileName)
  387. {
  388. long lSlash = csFileName.ReverseFind('\\');
  389. if(lSlash > -1)
  390. {
  391. csFileName = csFileName.Left(lSlash + 1);
  392. }
  393. return csFileName;
  394. }
  395. CString GetFileName(CString csFileName)
  396. {
  397. long lSlash = csFileName.ReverseFind('\\');
  398. if(lSlash > -1)
  399. {
  400. csFileName = csFileName.Right(csFileName.GetLength() - lSlash - 1);
  401. }
  402. return csFileName;
  403. }
  404. /****************************************************************************************************
  405. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  406. ***************************************************************************************************/
  407. typedef struct
  408. {
  409. long lFlags; // Flags
  410. LPRECT pVirtualRect; // Ptr to rect that receives the results, or the src of the monitor search method
  411. int iMonitor; // Ndx to the mointor to look at, -1 for all, -or- result of the monitor search method
  412. int nMonitorCount; // Total number of monitors found, -1 for monitor search method
  413. } MONITOR_ENUM_PARAM;
  414. #define MONITOR_SEARCH_METOHD 0x00000001
  415. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  416. {
  417. // Typecast param
  418. MONITOR_ENUM_PARAM* pParam = (MONITOR_ENUM_PARAM*)dwData;
  419. if(pParam)
  420. {
  421. // If a dest rect was passed
  422. if(pParam->pVirtualRect)
  423. {
  424. // If MONITOR_SEARCH_METOHD then we are being asked for the index of the monitor
  425. // that the rect falls inside of
  426. if(pParam->lFlags & MONITOR_SEARCH_METOHD)
  427. {
  428. if( (pParam->pVirtualRect->right < lprcMonitor->left) ||
  429. (pParam->pVirtualRect->left > lprcMonitor->right) ||
  430. (pParam->pVirtualRect->bottom < lprcMonitor->top) ||
  431. (pParam->pVirtualRect->top > lprcMonitor->bottom))
  432. {
  433. // Nothing
  434. }
  435. else
  436. {
  437. // This is the one
  438. pParam->iMonitor = pParam->nMonitorCount;
  439. // Stop the enumeration
  440. return FALSE;
  441. }
  442. }
  443. else
  444. {
  445. if(pParam->iMonitor == pParam->nMonitorCount)
  446. {
  447. *pParam->pVirtualRect = *lprcMonitor;
  448. }
  449. else
  450. if(pParam->iMonitor == -1)
  451. {
  452. pParam->pVirtualRect->left = min(pParam->pVirtualRect->left, lprcMonitor->left);
  453. pParam->pVirtualRect->top = min(pParam->pVirtualRect->top, lprcMonitor->top);
  454. pParam->pVirtualRect->right = max(pParam->pVirtualRect->right, lprcMonitor->right);
  455. pParam->pVirtualRect->bottom = max(pParam->pVirtualRect->bottom, lprcMonitor->bottom);
  456. }
  457. }
  458. }
  459. // Up the count if necessary
  460. pParam->nMonitorCount++;
  461. }
  462. return TRUE;
  463. }
  464. int GetScreenWidth(void)
  465. {
  466. OSVERSIONINFO OS_Version_Info;
  467. DWORD dwPlatform = 0;
  468. if(GetVersionEx(&OS_Version_Info) != 0)
  469. {
  470. dwPlatform = OS_Version_Info.dwPlatformId;
  471. }
  472. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  473. {
  474. int width, height;
  475. width = GetSystemMetrics(SM_CXSCREEN);
  476. height = GetSystemMetrics(SM_CYSCREEN);
  477. switch(width)
  478. {
  479. default:
  480. case 640:
  481. case 800:
  482. case 1024:
  483. return(width);
  484. case 1280:
  485. if(height == 480)
  486. {
  487. return(width / 2);
  488. }
  489. return(width);
  490. case 1600:
  491. if(height == 600)
  492. {
  493. return(width / 2);
  494. }
  495. return(width);
  496. case 2048:
  497. if(height == 768)
  498. {
  499. return(width / 2);
  500. }
  501. return(width);
  502. }
  503. }
  504. else
  505. {
  506. return(GetSystemMetrics(SM_CXSCREEN));
  507. }
  508. }
  509. int GetScreenHeight(void)
  510. {
  511. OSVERSIONINFO OS_Version_Info;
  512. DWORD dwPlatform = 0;
  513. if(GetVersionEx(&OS_Version_Info) != 0)
  514. {
  515. dwPlatform = OS_Version_Info.dwPlatformId;
  516. }
  517. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  518. {
  519. int width, height;
  520. width = GetSystemMetrics(SM_CXSCREEN);
  521. height = GetSystemMetrics(SM_CYSCREEN);
  522. switch(height)
  523. {
  524. default:
  525. case 480:
  526. case 600:
  527. case 768:
  528. return(height);
  529. case 960:
  530. if(width == 640)
  531. {
  532. return(height / 2);
  533. }
  534. return(height);
  535. case 1200:
  536. if(width == 800)
  537. {
  538. return(height / 2);
  539. }
  540. return(height);
  541. case 1536:
  542. if(width == 1024)
  543. {
  544. return(height / 2);
  545. }
  546. return(height);
  547. }
  548. }
  549. else
  550. {
  551. return(GetSystemMetrics(SM_CYSCREEN));
  552. }
  553. }
  554. int GetMonitorFromRect(LPRECT lpMonitorRect)
  555. {
  556. // Build up the param
  557. MONITOR_ENUM_PARAM EnumParam;
  558. ZeroMemory(&EnumParam, sizeof(EnumParam));
  559. EnumParam.lFlags = MONITOR_SEARCH_METOHD;
  560. EnumParam.pVirtualRect = lpMonitorRect;
  561. EnumParam.iMonitor = -1;
  562. // Enum Displays
  563. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  564. // Return the result
  565. return EnumParam.iMonitor;
  566. }
  567. void GetMonitorRect(int iMonitor, LPRECT lpDestRect)
  568. {
  569. // Build up the param
  570. MONITOR_ENUM_PARAM EnumParam;
  571. ZeroMemory(&EnumParam, sizeof(EnumParam));
  572. EnumParam.iMonitor = iMonitor;
  573. EnumParam.pVirtualRect = lpDestRect;
  574. // Zero out dest rect
  575. lpDestRect->bottom = lpDestRect->left = lpDestRect->right = lpDestRect->top = 0;
  576. // Enum Displays
  577. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  578. // If not successful, default to the screen dimentions
  579. if(lpDestRect->right == 0 || lpDestRect->bottom == 0)
  580. {
  581. lpDestRect->right = GetScreenWidth();
  582. lpDestRect->bottom = GetScreenHeight();
  583. }
  584. //adjust the rect for the taskbar
  585. APPBARDATA appBarData;
  586. appBarData.cbSize=sizeof(appBarData);
  587. if (SHAppBarMessage(ABM_GETTASKBARPOS, &appBarData))
  588. {
  589. switch(appBarData.uEdge)
  590. {
  591. case ABE_LEFT:
  592. lpDestRect->left += appBarData.rc.right - appBarData.rc.left;
  593. break;
  594. case ABE_RIGHT:
  595. lpDestRect->right -= appBarData.rc.right - appBarData.rc.left;
  596. break;
  597. case ABE_TOP:
  598. lpDestRect->top += appBarData.rc.bottom - appBarData.rc.top;
  599. break;
  600. case ABE_BOTTOM:
  601. lpDestRect->bottom -= appBarData.rc.bottom - appBarData.rc.top;
  602. break;
  603. }
  604. return;
  605. }
  606. }
  607. /*------------------------------------------------------------------*\
  608. ID based Globals
  609. \*------------------------------------------------------------------*/
  610. long NewGroupID(int parentID, CString text)
  611. {
  612. long lID=0;
  613. CTime time;
  614. time = CTime::GetCurrentTime();
  615. try
  616. {
  617. //sqlite doesn't like single quotes ' replace them with double ''
  618. if(text.IsEmpty())
  619. text = time.Format("NewGroup %y/%m/%d %H:%M:%S");
  620. text.Replace(_T("'"), _T("''"));
  621. CString cs;
  622. cs.Format(_T("insert into Main (lDate, mText, lDontAutoDelete, bIsGroup, lParentID) values(%d, '%s', %d, 1, %d);"),
  623. (int)time.GetTime(),
  624. text,
  625. (int)time.GetTime(),
  626. parentID);
  627. theApp.m_db.execDML(cs);
  628. lID = (long)theApp.m_db.lastRowId();
  629. }
  630. CATCH_SQLITE_EXCEPTION_AND_RETURN(0)
  631. return lID;
  632. }
  633. BOOL DeleteAllIDs()
  634. {
  635. try
  636. {
  637. theApp.m_db.execDML(_T("DELETE FROM Data;"));
  638. theApp.m_db.execDML(_T("DELETE FROM Main;"));
  639. }
  640. CATCH_SQLITE_EXCEPTION
  641. return TRUE;
  642. }
  643. BOOL DeleteFormats(int parentID, ARRAY& formatIDs)
  644. {
  645. if(formatIDs.GetSize() <= 0)
  646. return TRUE;
  647. try
  648. {
  649. //Delete the requested data formats
  650. INT_PTR count = formatIDs.GetSize();
  651. for(int i = 0; i < count; i++)
  652. {
  653. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lID = %d;"), formatIDs[i]);
  654. }
  655. CClip clip;
  656. if(clip.LoadFormats(parentID))
  657. {
  658. DWORD CRC = clip.GenerateCRC();
  659. //Update the main table with new size
  660. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, parentID);
  661. }
  662. }
  663. CATCH_SQLITE_EXCEPTION
  664. return TRUE;
  665. }
  666. CRect CenterRect(CRect startingRect)
  667. {
  668. CRect crMonitor;
  669. int nMonitor = GetMonitorFromRect(&startingRect);
  670. if(nMonitor < 0)
  671. {
  672. GetMonitorRect(0, crMonitor);
  673. }
  674. else
  675. {
  676. GetMonitorRect(nMonitor, crMonitor);
  677. }
  678. return CenterRectFromRect(startingRect, crMonitor);
  679. }
  680. CRect CenterRectFromRect(CRect startingRect, CRect outerRect)
  681. {
  682. CPoint center = outerRect.CenterPoint();
  683. CRect centerRect;
  684. centerRect.left = center.x - (startingRect.Width() / 2);
  685. centerRect.top = center.y - (startingRect.Height() / 2);
  686. centerRect.right = centerRect.left + startingRect.Width();
  687. centerRect.bottom = centerRect.top + startingRect.Height();
  688. return centerRect;
  689. }
  690. BOOL EnsureWindowVisible(CRect *pcrRect)
  691. {
  692. int nMonitor = GetMonitorFromRect(pcrRect);
  693. if(nMonitor < 0)
  694. {
  695. GetMonitorRect(0, pcrRect);
  696. pcrRect->right = pcrRect->left + 300;
  697. pcrRect->bottom = pcrRect->top + 300;
  698. return TRUE;
  699. }
  700. BOOL ret = FALSE;
  701. CRect crMonitor;
  702. GetMonitorRect(nMonitor, crMonitor);
  703. //Validate the left
  704. long lDiff = pcrRect->left - crMonitor.left;
  705. if(lDiff < 0)
  706. {
  707. pcrRect->left += abs(lDiff);
  708. pcrRect->right += abs(lDiff);
  709. ret = TRUE;
  710. }
  711. //Right side
  712. lDiff = pcrRect->right - crMonitor.right;
  713. if(lDiff > 0)
  714. {
  715. pcrRect->left -= abs(lDiff);
  716. pcrRect->right -= abs(lDiff);
  717. ret = TRUE;
  718. }
  719. //Top
  720. lDiff = pcrRect->top - crMonitor.top;
  721. if(lDiff < 0)
  722. {
  723. pcrRect->top += abs(lDiff);
  724. pcrRect->bottom += abs(lDiff);
  725. ret = TRUE;
  726. }
  727. //Bottom
  728. lDiff = pcrRect->bottom - crMonitor.bottom;
  729. if(lDiff > 0)
  730. {
  731. pcrRect->top -= abs(lDiff);
  732. pcrRect->bottom -= abs(lDiff);
  733. ret = TRUE;
  734. }
  735. return ret;
  736. }
  737. __int64 GetLastWriteTime(const CString &csFile)
  738. {
  739. __int64 nLastWrite = 0;
  740. CFileFind finder;
  741. BOOL bResult = finder.FindFile(csFile);
  742. if (bResult)
  743. {
  744. finder.FindNextFile();
  745. FILETIME ft;
  746. finder.GetLastWriteTime(&ft);
  747. memcpy(&nLastWrite, &ft, sizeof(ft));
  748. }
  749. return nLastWrite;
  750. }
  751. CString GetProcessName(HWND hWnd)
  752. {
  753. DWORD startTick = GetTickCount();
  754. DWORD Id;
  755. CString strProcessName ;
  756. GetWindowThreadProcessId(hWnd, &Id);
  757. PROCESSENTRY32 processEntry = { 0 };
  758. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  759. processEntry.dwSize = sizeof(PROCESSENTRY32);
  760. if (Process32First(hSnapShot, &processEntry))
  761. {
  762. do
  763. {
  764. if (processEntry.th32ProcessID == Id)
  765. {
  766. strProcessName = processEntry.szExeFile;
  767. break;
  768. }
  769. } while(Process32Next(hSnapShot, &processEntry));
  770. }
  771. CloseHandle(hSnapShot);
  772. DWORD endTick = GetTickCount();
  773. DWORD diff = endTick - startTick;
  774. if(diff > 5)
  775. {
  776. Log(StrF(_T("GetProcessName Time (ms): %d"), endTick-startTick));
  777. }
  778. return strProcessName;
  779. }
  780. BOOL IsVista()
  781. {
  782. OSVERSIONINFO osver;
  783. osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  784. if (::GetVersionEx( &osver ) &&
  785. osver.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  786. (osver.dwMajorVersion >= 6 ) )
  787. {
  788. return TRUE;
  789. }
  790. return FALSE;
  791. }
  792. bool IsRunningLimited()
  793. {
  794. LPCTSTR pszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  795. LPCTSTR pszValue = _T("EnableLUA");
  796. DWORD dwType = 0;
  797. DWORD dwValue = 0;
  798. DWORD dwValueSize = sizeof(DWORD);
  799. if(ERROR_SUCCESS != SHGetValue(HKEY_LOCAL_MACHINE, pszSubKey, pszValue, &dwType, &dwValue, &dwValueSize))
  800. {
  801. //failed to read the reg key, either it's not there or we don't have access to the registry
  802. //If we are vista then assume we don't have access and we are running as a limited app
  803. //otherwise we are xp and the reg key probably doesn't exist and we are not a limited running app
  804. if(IsVista())
  805. {
  806. OutputDebugString(_T("Ditto - Failed to read registry entry finding UAC, Running as limited application"));
  807. return true;
  808. }
  809. }
  810. if(dwValue == 1)
  811. {
  812. OutputDebugString(_T("Ditto - UAC ENABLED, Running as limited application"));
  813. return true;
  814. }
  815. OutputDebugString(_T("Ditto - Running as standard application"));
  816. return false;
  817. }
  818. void DeleteReceivedFiles(CString csDir)
  819. {
  820. if(csDir.Find(_T("\\ReceivedFiles\\")) == -1)
  821. return;
  822. FIX_CSTRING_PATH(csDir);
  823. CTime ctOld = CTime::GetCurrentTime();
  824. CTime ctFile;
  825. ctOld -= CTimeSpan(0, 0, 0, 1);
  826. CFileFind Find;
  827. CString csFindString;
  828. csFindString.Format(_T("%s*.*"), csDir);
  829. BOOL bFound = Find.FindFile(csFindString);
  830. while(bFound)
  831. {
  832. bFound = Find.FindNextFile();
  833. if(Find.IsDots())
  834. continue;
  835. if(Find.IsDirectory())
  836. {
  837. CString csDir(Find.GetFilePath());
  838. DeleteReceivedFiles(csDir);
  839. RemoveDirectory(csDir);
  840. }
  841. if(Find.GetLastAccessTime(ctFile))
  842. {
  843. //Delete the remote copied file if it has'nt been used for the last day
  844. if(ctFile < ctOld)
  845. {
  846. DeleteFile(Find.GetFilePath());
  847. }
  848. }
  849. else
  850. {
  851. DeleteFile(Find.GetFilePath());
  852. }
  853. }
  854. }
  855. __int64 FileSize(const TCHAR *fileName)
  856. {
  857. __stat64 buf;
  858. if (_wstat64(fileName, &buf) != 0)
  859. return -1; // error, could use errno to find out more
  860. return buf.st_size;
  861. }