Misc.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. (long)time.GetTime(),
  624. text,
  625. (long)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. BOOL EnsureWindowVisible(CRect *pcrRect)
  667. {
  668. int nMonitor = GetMonitorFromRect(pcrRect);
  669. if(nMonitor < 0)
  670. {
  671. GetMonitorRect(0, pcrRect);
  672. pcrRect->right = pcrRect->left + 300;
  673. pcrRect->bottom = pcrRect->top + 300;
  674. return TRUE;
  675. }
  676. CRect crMonitor;
  677. GetMonitorRect(nMonitor, crMonitor);
  678. //Validate the left
  679. long lDiff = pcrRect->left - crMonitor.left;
  680. if(lDiff < 0)
  681. {
  682. pcrRect->left += abs(lDiff);
  683. pcrRect->right += abs(lDiff);
  684. }
  685. //Right side
  686. lDiff = pcrRect->right - crMonitor.right;
  687. if(lDiff > 0)
  688. {
  689. pcrRect->left -= abs(lDiff);
  690. pcrRect->right -= abs(lDiff);
  691. }
  692. //Top
  693. lDiff = pcrRect->top - crMonitor.top;
  694. if(lDiff < 0)
  695. {
  696. pcrRect->top += abs(lDiff);
  697. pcrRect->bottom += abs(lDiff);
  698. }
  699. //Bottom
  700. lDiff = pcrRect->bottom - crMonitor.bottom;
  701. if(lDiff > 0)
  702. {
  703. pcrRect->top -= abs(lDiff);
  704. pcrRect->bottom -= abs(lDiff);
  705. }
  706. return TRUE;
  707. }
  708. __int64 GetLastWriteTime(const CString &csFile)
  709. {
  710. __int64 nLastWrite = 0;
  711. CFileFind finder;
  712. BOOL bResult = finder.FindFile(csFile);
  713. if (bResult)
  714. {
  715. finder.FindNextFile();
  716. FILETIME ft;
  717. finder.GetLastWriteTime(&ft);
  718. memcpy(&nLastWrite, &ft, sizeof(ft));
  719. }
  720. return nLastWrite;
  721. }
  722. CString GetProcessName(HWND hWnd)
  723. {
  724. DWORD Id;
  725. GetWindowThreadProcessId(hWnd, &Id);
  726. PROCESSENTRY32 processEntry = { 0 };
  727. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  728. processEntry.dwSize = sizeof(PROCESSENTRY32);
  729. if (Process32First(hSnapShot, &processEntry))
  730. {
  731. do
  732. {
  733. if (processEntry.th32ProcessID == Id)
  734. {
  735. return processEntry.szExeFile;
  736. }
  737. } while(Process32Next(hSnapShot, &processEntry));
  738. }
  739. CloseHandle(hSnapShot);
  740. return "";
  741. }
  742. BOOL IsVista()
  743. {
  744. OSVERSIONINFO osver;
  745. osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  746. if (::GetVersionEx( &osver ) &&
  747. osver.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  748. (osver.dwMajorVersion >= 6 ) )
  749. {
  750. return TRUE;
  751. }
  752. return FALSE;
  753. }
  754. bool IsRunningLimited()
  755. {
  756. LPCTSTR pszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  757. LPCTSTR pszValue = _T("EnableLUA");
  758. DWORD dwType = 0;
  759. DWORD dwValue = 0;
  760. DWORD dwValueSize = sizeof(DWORD);
  761. if(ERROR_SUCCESS != SHGetValue(HKEY_LOCAL_MACHINE, pszSubKey, pszValue, &dwType, &dwValue, &dwValueSize))
  762. {
  763. //failed to read the reg key, either it's not there or we don't have access to the registry
  764. //If we are vista then assume we don't have access and we are running as a limited app
  765. //otherwise we are xp and the reg key probably doesn't exist and we are not a limited running app
  766. if(IsVista())
  767. {
  768. OutputDebugString(_T("Ditto - Failed to read registry entry finding UAC, Running as limited application"));
  769. return true;
  770. }
  771. }
  772. if(dwValue == 1)
  773. {
  774. OutputDebugString(_T("Ditto - UAC ENABLED, Running as limited application"));
  775. return true;
  776. }
  777. OutputDebugString(_T("Ditto - Running as standard application"));
  778. return false;
  779. }
  780. void DeleteReceivedFiles(CString csDir)
  781. {
  782. if(csDir.Find(_T("\\ReceivedFiles\\")) == -1)
  783. return;
  784. FIX_CSTRING_PATH(csDir);
  785. CTime ctOld = CTime::GetCurrentTime();
  786. CTime ctFile;
  787. ctOld -= CTimeSpan(0, 0, 0, 1);
  788. CFileFind Find;
  789. CString csFindString;
  790. csFindString.Format(_T("%s*.*"), csDir);
  791. BOOL bFound = Find.FindFile(csFindString);
  792. while(bFound)
  793. {
  794. bFound = Find.FindNextFile();
  795. if(Find.IsDots())
  796. continue;
  797. if(Find.IsDirectory())
  798. {
  799. CString csDir(Find.GetFilePath());
  800. DeleteReceivedFiles(csDir);
  801. RemoveDirectory(csDir);
  802. }
  803. if(Find.GetLastAccessTime(ctFile))
  804. {
  805. //Delete the remote copied file if it has'nt been used for the last day
  806. if(ctFile < ctOld)
  807. {
  808. DeleteFile(Find.GetFilePath());
  809. }
  810. }
  811. else
  812. {
  813. DeleteFile(Find.GetFilePath());
  814. }
  815. }
  816. }