Misc.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. DWORD 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)
  130. {
  131. Log(StrF(_T("Adjusting time of get tickcount by: %d, on startup we found GetTickCount to be less than last input"), CGetSetOptions::GetFunnyTickCountAdjustment()));
  132. currentTick += CGetSetOptions::GetFunnyTickCountAdjustment();
  133. }
  134. DWORD idleSeconds = (currentTick - info.dwTime)/1000;
  135. return idleSeconds;
  136. }
  137. CString StrF(const TCHAR * pszFormat, ...)
  138. {
  139. ASSERT( AfxIsValidString( pszFormat ) );
  140. CString str;
  141. va_list argList;
  142. va_start( argList, pszFormat );
  143. str.FormatV( pszFormat, argList );
  144. va_end( argList );
  145. return str;
  146. }
  147. BYTE GetEscapeChar( BYTE ch )
  148. {
  149. switch(ch)
  150. {
  151. case '\'': return '\''; // Single quotation mark (') = 39 or 0x27
  152. case '\"': return '\"'; // Double quotation mark (") = 34 or 0x22
  153. case '?': return '\?'; // Question mark (?) = 63 or 0x3f
  154. case '\\': return '\\'; // Backslash (\) = 92 or 0x5c
  155. case 'a': return '\a'; // Alert (BEL) = 7
  156. case 'b': return '\b'; // Backspace (BS) = 8
  157. case 'f': return '\f'; // Formfeed (FF) = 12 or 0x0c
  158. case 'n': return '\n'; // Newline (NL or LF) = 10 or 0x0a
  159. case 'r': return '\r'; // Carriage Return (CR) = 13 or 0x0d
  160. case 't': return '\t'; // Horizontal tab (HT) = 9
  161. case 'v': return '\v'; // Vertical tab (VT) = 11 or 0x0b
  162. case '0': return '\0'; // Null character (NUL) = 0
  163. }
  164. return 0; // invalid
  165. }
  166. CString RemoveEscapes( const TCHAR* str )
  167. {
  168. ASSERT( str );
  169. CString ret;
  170. TCHAR* pSrc = (TCHAR*) str;
  171. TCHAR* pDest = ret.GetBuffer((int)STRLEN(pSrc));
  172. TCHAR* pStart = pDest;
  173. while( *pSrc != '\0' )
  174. {
  175. if( *pSrc == '\\' )
  176. {
  177. pSrc++;
  178. *pDest = GetEscapeChar((BYTE)pSrc );
  179. }
  180. else
  181. *pDest = *pSrc;
  182. pSrc++;
  183. pDest++;
  184. }
  185. ret.ReleaseBuffer((int)(pDest - pStart));
  186. return ret;
  187. }
  188. CString GetWndText( HWND hWnd )
  189. {
  190. CString text;
  191. if( !IsWindow(hWnd) )
  192. return "! NOT A VALID WINDOW !";
  193. CWnd* pWnd = CWnd::FromHandle(hWnd);
  194. pWnd->GetWindowText(text);
  195. return text;
  196. }
  197. bool IsAppWnd( HWND hWnd )
  198. {
  199. DWORD dwMyPID = ::GetCurrentProcessId();
  200. DWORD dwTestPID;
  201. ::GetWindowThreadProcessId( hWnd, &dwTestPID );
  202. return dwMyPID == dwTestPID;
  203. }
  204. /*----------------------------------------------------------------------------*\
  205. Global Memory Helper Functions
  206. \*----------------------------------------------------------------------------*/
  207. // make sure the given HGLOBAL is valid.
  208. BOOL IsValid(HGLOBAL hGlobal)
  209. {
  210. void* pvData = ::GlobalLock(hGlobal);
  211. ::GlobalUnlock(hGlobal);
  212. return (pvData != NULL);
  213. }
  214. // asserts if hDest isn't big enough
  215. void CopyToGlobalHP(HGLOBAL hDest, LPVOID pBuf, SIZE_T ulBufLen)
  216. {
  217. ASSERT(hDest && pBuf && ulBufLen);
  218. LPVOID pvData = GlobalLock(hDest);
  219. ASSERT(pvData);
  220. SIZE_T size = GlobalSize(hDest);
  221. ASSERT(size >= ulBufLen); // assert if hDest isn't big enough
  222. memcpy(pvData, pBuf, ulBufLen);
  223. GlobalUnlock(hDest);
  224. }
  225. void CopyToGlobalHH(HGLOBAL hDest, HGLOBAL hSource, SIZE_T ulBufLen)
  226. {
  227. ASSERT(hDest && hSource && ulBufLen);
  228. LPVOID pvData = GlobalLock(hSource);
  229. ASSERT(pvData );
  230. SIZE_T size = GlobalSize(hSource);
  231. ASSERT(size >= ulBufLen); // assert if hSource isn't big enough
  232. CopyToGlobalHP(hDest, pvData, ulBufLen);
  233. GlobalUnlock(hSource);
  234. }
  235. HGLOBAL NewGlobalP(LPVOID pBuf, SIZE_T nLen)
  236. {
  237. ASSERT(pBuf && nLen);
  238. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  239. ASSERT(hDest );
  240. CopyToGlobalHP(hDest, pBuf, nLen);
  241. return hDest;
  242. }
  243. HGLOBAL NewGlobal(SIZE_T nLen)
  244. {
  245. ASSERT(nLen);
  246. HGLOBAL hDest = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, nLen);
  247. return hDest;
  248. }
  249. HGLOBAL NewGlobalH(HGLOBAL hSource, SIZE_T nLen)
  250. {
  251. ASSERT(hSource && nLen);
  252. LPVOID pvData = GlobalLock(hSource);
  253. HGLOBAL hDest = NewGlobalP(pvData, nLen);
  254. GlobalUnlock(hSource);
  255. return hDest;
  256. }
  257. int CompareGlobalHP(HGLOBAL hLeft, LPVOID pBuf, SIZE_T ulBufLen)
  258. {
  259. ASSERT(hLeft && pBuf && ulBufLen);
  260. LPVOID pvData = GlobalLock(hLeft);
  261. ASSERT(pvData);
  262. ASSERT(ulBufLen <= GlobalSize(hLeft));
  263. int result = memcmp(pvData, pBuf, ulBufLen);
  264. GlobalUnlock(hLeft);
  265. return result;
  266. }
  267. int CompareGlobalHH( HGLOBAL hLeft, HGLOBAL hRight, SIZE_T ulBufLen)
  268. {
  269. ASSERT(hLeft && hRight && ulBufLen);
  270. ASSERT(ulBufLen <= GlobalSize(hRight));
  271. LPVOID pvData = GlobalLock(hRight);
  272. ASSERT(pvData);
  273. int result = CompareGlobalHP(hLeft, pvData, ulBufLen);
  274. GlobalUnlock(hLeft);
  275. return result;
  276. }
  277. //Do not change these these are stored in the database
  278. CLIPFORMAT GetFormatID(LPCTSTR cbName)
  279. {
  280. if(STRCMP(cbName, _T("CF_TEXT")) == 0)
  281. return CF_TEXT;
  282. else if(STRCMP(cbName, _T("CF_METAFILEPICT")) == 0)
  283. return CF_METAFILEPICT;
  284. else if(STRCMP(cbName, _T("CF_SYLK")) == 0)
  285. return CF_SYLK;
  286. else if(STRCMP(cbName, _T("CF_DIF")) == 0)
  287. return CF_DIF;
  288. else if(STRCMP(cbName, _T("CF_TIFF")) == 0)
  289. return CF_TIFF;
  290. else if(STRCMP(cbName, _T("CF_OEMTEXT")) == 0)
  291. return CF_OEMTEXT;
  292. else if(STRCMP(cbName, _T("CF_DIB")) == 0)
  293. return CF_DIB;
  294. else if(STRCMP(cbName, _T("CF_PALETTE")) == 0)
  295. return CF_PALETTE;
  296. else if(STRCMP(cbName, _T("CF_PENDATA")) == 0)
  297. return CF_PENDATA;
  298. else if(STRCMP(cbName, _T("CF_RIFF")) == 0)
  299. return CF_RIFF;
  300. else if(STRCMP(cbName, _T("CF_WAVE")) == 0)
  301. return CF_WAVE;
  302. else if(STRCMP(cbName, _T("CF_UNICODETEXT")) == 0)
  303. return CF_UNICODETEXT;
  304. else if(STRCMP(cbName, _T("CF_ENHMETAFILE")) == 0)
  305. return CF_ENHMETAFILE;
  306. else if(STRCMP(cbName, _T("CF_HDROP")) == 0)
  307. return CF_HDROP;
  308. else if(STRCMP(cbName, _T("CF_LOCALE")) == 0)
  309. return CF_LOCALE;
  310. else if(STRCMP(cbName, _T("CF_OWNERDISPLAY")) == 0)
  311. return CF_OWNERDISPLAY;
  312. else if(STRCMP(cbName, _T("CF_DSPTEXT")) == 0)
  313. return CF_DSPTEXT;
  314. else if(STRCMP(cbName, _T("CF_DSPBITMAP")) == 0)
  315. return CF_DSPBITMAP;
  316. else if(STRCMP(cbName, _T("CF_DSPMETAFILEPICT")) == 0)
  317. return CF_DSPMETAFILEPICT;
  318. else if(STRCMP(cbName, _T("CF_DSPENHMETAFILE")) == 0)
  319. return CF_DSPENHMETAFILE;
  320. return ::RegisterClipboardFormat(cbName);
  321. }
  322. //Do not change these these are stored in the database
  323. CString GetFormatName(CLIPFORMAT cbType)
  324. {
  325. switch(cbType)
  326. {
  327. case CF_TEXT:
  328. return _T("CF_TEXT");
  329. case CF_BITMAP:
  330. return _T("CF_BITMAP");
  331. case CF_METAFILEPICT:
  332. return _T("CF_METAFILEPICT");
  333. case CF_SYLK:
  334. return _T("CF_SYLK");
  335. case CF_DIF:
  336. return _T("CF_DIF");
  337. case CF_TIFF:
  338. return _T("CF_TIFF");
  339. case CF_OEMTEXT:
  340. return _T("CF_OEMTEXT");
  341. case CF_DIB:
  342. return _T("CF_DIB");
  343. case CF_PALETTE:
  344. return _T("CF_PALETTE");
  345. case CF_PENDATA:
  346. return _T("CF_PENDATA");
  347. case CF_RIFF:
  348. return _T("CF_RIFF");
  349. case CF_WAVE:
  350. return _T("CF_WAVE");
  351. case CF_UNICODETEXT:
  352. return _T("CF_UNICODETEXT");
  353. case CF_ENHMETAFILE:
  354. return _T("CF_ENHMETAFILE");
  355. case CF_HDROP:
  356. return _T("CF_HDROP");
  357. case CF_LOCALE:
  358. return _T("CF_LOCALE");
  359. case CF_OWNERDISPLAY:
  360. return _T("CF_OWNERDISPLAY");
  361. case CF_DSPTEXT:
  362. return _T("CF_DSPTEXT");
  363. case CF_DSPBITMAP:
  364. return _T("CF_DSPBITMAP");
  365. case CF_DSPMETAFILEPICT:
  366. return _T("CF_DSPMETAFILEPICT");
  367. case CF_DSPENHMETAFILE:
  368. return _T("CF_DSPENHMETAFILE");
  369. default:
  370. //Not a default type get the name from the clipboard
  371. if (cbType != 0)
  372. {
  373. TCHAR szFormat[256];
  374. GetClipboardFormatName(cbType, szFormat, 256);
  375. return szFormat;
  376. }
  377. break;
  378. }
  379. return "ERROR";
  380. }
  381. CString GetFilePath(CString csFileName)
  382. {
  383. long lSlash = csFileName.ReverseFind('\\');
  384. if(lSlash > -1)
  385. {
  386. csFileName = csFileName.Left(lSlash + 1);
  387. }
  388. return csFileName;
  389. }
  390. CString GetFileName(CString csFileName)
  391. {
  392. long lSlash = csFileName.ReverseFind('\\');
  393. if(lSlash > -1)
  394. {
  395. csFileName = csFileName.Right(csFileName.GetLength() - lSlash - 1);
  396. }
  397. return csFileName;
  398. }
  399. /****************************************************************************************************
  400. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  401. ***************************************************************************************************/
  402. typedef struct
  403. {
  404. long lFlags; // Flags
  405. LPRECT pVirtualRect; // Ptr to rect that receives the results, or the src of the monitor search method
  406. int iMonitor; // Ndx to the mointor to look at, -1 for all, -or- result of the monitor search method
  407. int nMonitorCount; // Total number of monitors found, -1 for monitor search method
  408. } MONITOR_ENUM_PARAM;
  409. #define MONITOR_SEARCH_METOHD 0x00000001
  410. BOOL CALLBACK MyMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
  411. {
  412. // Typecast param
  413. MONITOR_ENUM_PARAM* pParam = (MONITOR_ENUM_PARAM*)dwData;
  414. if(pParam)
  415. {
  416. // If a dest rect was passed
  417. if(pParam->pVirtualRect)
  418. {
  419. // If MONITOR_SEARCH_METOHD then we are being asked for the index of the monitor
  420. // that the rect falls inside of
  421. if(pParam->lFlags & MONITOR_SEARCH_METOHD)
  422. {
  423. if( (pParam->pVirtualRect->right < lprcMonitor->left) ||
  424. (pParam->pVirtualRect->left > lprcMonitor->right) ||
  425. (pParam->pVirtualRect->bottom < lprcMonitor->top) ||
  426. (pParam->pVirtualRect->top > lprcMonitor->bottom))
  427. {
  428. // Nothing
  429. }
  430. else
  431. {
  432. // This is the one
  433. pParam->iMonitor = pParam->nMonitorCount;
  434. // Stop the enumeration
  435. return FALSE;
  436. }
  437. }
  438. else
  439. {
  440. if(pParam->iMonitor == pParam->nMonitorCount)
  441. {
  442. *pParam->pVirtualRect = *lprcMonitor;
  443. }
  444. else
  445. if(pParam->iMonitor == -1)
  446. {
  447. pParam->pVirtualRect->left = min(pParam->pVirtualRect->left, lprcMonitor->left);
  448. pParam->pVirtualRect->top = min(pParam->pVirtualRect->top, lprcMonitor->top);
  449. pParam->pVirtualRect->right = max(pParam->pVirtualRect->right, lprcMonitor->right);
  450. pParam->pVirtualRect->bottom = max(pParam->pVirtualRect->bottom, lprcMonitor->bottom);
  451. }
  452. }
  453. }
  454. // Up the count if necessary
  455. pParam->nMonitorCount++;
  456. }
  457. return TRUE;
  458. }
  459. int GetScreenWidth(void)
  460. {
  461. OSVERSIONINFO OS_Version_Info;
  462. DWORD dwPlatform = 0;
  463. if(GetVersionEx(&OS_Version_Info) != 0)
  464. {
  465. dwPlatform = OS_Version_Info.dwPlatformId;
  466. }
  467. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  468. {
  469. int width, height;
  470. width = GetSystemMetrics(SM_CXSCREEN);
  471. height = GetSystemMetrics(SM_CYSCREEN);
  472. switch(width)
  473. {
  474. default:
  475. case 640:
  476. case 800:
  477. case 1024:
  478. return(width);
  479. case 1280:
  480. if(height == 480)
  481. {
  482. return(width / 2);
  483. }
  484. return(width);
  485. case 1600:
  486. if(height == 600)
  487. {
  488. return(width / 2);
  489. }
  490. return(width);
  491. case 2048:
  492. if(height == 768)
  493. {
  494. return(width / 2);
  495. }
  496. return(width);
  497. }
  498. }
  499. else
  500. {
  501. return(GetSystemMetrics(SM_CXSCREEN));
  502. }
  503. }
  504. int GetScreenHeight(void)
  505. {
  506. OSVERSIONINFO OS_Version_Info;
  507. DWORD dwPlatform = 0;
  508. if(GetVersionEx(&OS_Version_Info) != 0)
  509. {
  510. dwPlatform = OS_Version_Info.dwPlatformId;
  511. }
  512. if(dwPlatform == VER_PLATFORM_WIN32_NT)
  513. {
  514. int width, height;
  515. width = GetSystemMetrics(SM_CXSCREEN);
  516. height = GetSystemMetrics(SM_CYSCREEN);
  517. switch(height)
  518. {
  519. default:
  520. case 480:
  521. case 600:
  522. case 768:
  523. return(height);
  524. case 960:
  525. if(width == 640)
  526. {
  527. return(height / 2);
  528. }
  529. return(height);
  530. case 1200:
  531. if(width == 800)
  532. {
  533. return(height / 2);
  534. }
  535. return(height);
  536. case 1536:
  537. if(width == 1024)
  538. {
  539. return(height / 2);
  540. }
  541. return(height);
  542. }
  543. }
  544. else
  545. {
  546. return(GetSystemMetrics(SM_CYSCREEN));
  547. }
  548. }
  549. int GetMonitorFromRect(LPRECT lpMonitorRect)
  550. {
  551. // Build up the param
  552. MONITOR_ENUM_PARAM EnumParam;
  553. ZeroMemory(&EnumParam, sizeof(EnumParam));
  554. EnumParam.lFlags = MONITOR_SEARCH_METOHD;
  555. EnumParam.pVirtualRect = lpMonitorRect;
  556. EnumParam.iMonitor = -1;
  557. // Enum Displays
  558. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  559. // Return the result
  560. return EnumParam.iMonitor;
  561. }
  562. void GetMonitorRect(int iMonitor, LPRECT lpDestRect)
  563. {
  564. // Build up the param
  565. MONITOR_ENUM_PARAM EnumParam;
  566. ZeroMemory(&EnumParam, sizeof(EnumParam));
  567. EnumParam.iMonitor = iMonitor;
  568. EnumParam.pVirtualRect = lpDestRect;
  569. // Zero out dest rect
  570. lpDestRect->bottom = lpDestRect->left = lpDestRect->right = lpDestRect->top = 0;
  571. // Enum Displays
  572. EnumDisplayMonitors(NULL, NULL, MyMonitorEnumProc, (long)&EnumParam);
  573. // If not successful, default to the screen dimentions
  574. if(lpDestRect->right == 0 || lpDestRect->bottom == 0)
  575. {
  576. lpDestRect->right = GetScreenWidth();
  577. lpDestRect->bottom = GetScreenHeight();
  578. }
  579. }
  580. /*------------------------------------------------------------------*\
  581. ID based Globals
  582. \*------------------------------------------------------------------*/
  583. long NewGroupID(int parentID, CString text)
  584. {
  585. long lID=0;
  586. CTime time;
  587. time = CTime::GetCurrentTime();
  588. try
  589. {
  590. //sqlite doesn't like single quotes ' replace them with double ''
  591. if(text.IsEmpty())
  592. text = time.Format("NewGroup %y/%m/%d %H:%M:%S");
  593. text.Replace(_T("'"), _T("''"));
  594. CString cs;
  595. cs.Format(_T("insert into Main (lDate, mText, lDontAutoDelete, bIsGroup, lParentID) values(%d, '%s', %d, 1, %d);"),
  596. (long)time.GetTime(),
  597. text,
  598. (long)time.GetTime(),
  599. parentID);
  600. theApp.m_db.execDML(cs);
  601. lID = (long)theApp.m_db.lastRowId();
  602. }
  603. CATCH_SQLITE_EXCEPTION_AND_RETURN(0)
  604. return lID;
  605. }
  606. BOOL DeleteAllIDs()
  607. {
  608. try
  609. {
  610. theApp.m_db.execDML(_T("DELETE FROM Data;"));
  611. theApp.m_db.execDML(_T("DELETE FROM Main;"));
  612. }
  613. CATCH_SQLITE_EXCEPTION
  614. return TRUE;
  615. }
  616. BOOL DeleteFormats(int parentID, ARRAY& formatIDs)
  617. {
  618. if(formatIDs.GetSize() <= 0)
  619. return TRUE;
  620. try
  621. {
  622. //Delete the requested data formats
  623. INT_PTR count = formatIDs.GetSize();
  624. for(int i = 0; i < count; i++)
  625. {
  626. theApp.m_db.execDMLEx(_T("DELETE FROM Data WHERE lID = %d;"), formatIDs[i]);
  627. }
  628. CClip clip;
  629. if(clip.LoadFormats(parentID))
  630. {
  631. DWORD CRC = clip.GenerateCRC();
  632. //Update the main table with new size
  633. theApp.m_db.execDMLEx(_T("UPDATE Main SET CRC = %d WHERE lID = %d"), CRC, parentID);
  634. }
  635. }
  636. CATCH_SQLITE_EXCEPTION
  637. return TRUE;
  638. }
  639. BOOL EnsureWindowVisible(CRect *pcrRect)
  640. {
  641. int nMonitor = GetMonitorFromRect(pcrRect);
  642. if(nMonitor < 0)
  643. {
  644. GetMonitorRect(0, pcrRect);
  645. pcrRect->right = pcrRect->left + 300;
  646. pcrRect->bottom = pcrRect->top + 300;
  647. return TRUE;
  648. }
  649. CRect crMonitor;
  650. GetMonitorRect(nMonitor, crMonitor);
  651. //Validate the left
  652. long lDiff = pcrRect->left - crMonitor.left;
  653. if(lDiff < 0)
  654. {
  655. pcrRect->left += abs(lDiff);
  656. pcrRect->right += abs(lDiff);
  657. }
  658. //Right side
  659. lDiff = pcrRect->right - crMonitor.right;
  660. if(lDiff > 0)
  661. {
  662. pcrRect->left -= abs(lDiff);
  663. pcrRect->right -= abs(lDiff);
  664. }
  665. //Top
  666. lDiff = pcrRect->top - crMonitor.top;
  667. if(lDiff < 0)
  668. {
  669. pcrRect->top += abs(lDiff);
  670. pcrRect->bottom += abs(lDiff);
  671. }
  672. //Bottom
  673. lDiff = pcrRect->bottom - crMonitor.bottom;
  674. if(lDiff > 0)
  675. {
  676. pcrRect->top -= abs(lDiff);
  677. pcrRect->bottom -= abs(lDiff);
  678. }
  679. return TRUE;
  680. }
  681. __int64 GetLastWriteTime(const CString &csFile)
  682. {
  683. __int64 nLastWrite = 0;
  684. CFileFind finder;
  685. BOOL bResult = finder.FindFile(csFile);
  686. if (bResult)
  687. {
  688. finder.FindNextFile();
  689. FILETIME ft;
  690. finder.GetLastWriteTime(&ft);
  691. memcpy(&nLastWrite, &ft, sizeof(ft));
  692. }
  693. return nLastWrite;
  694. }
  695. CString GetProcessName(HWND hWnd)
  696. {
  697. DWORD Id;
  698. GetWindowThreadProcessId(hWnd, &Id);
  699. PROCESSENTRY32 processEntry = { 0 };
  700. HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  701. processEntry.dwSize = sizeof(PROCESSENTRY32);
  702. if (Process32First(hSnapShot, &processEntry))
  703. {
  704. do
  705. {
  706. if (processEntry.th32ProcessID == Id)
  707. {
  708. return processEntry.szExeFile;
  709. }
  710. } while(Process32Next(hSnapShot, &processEntry));
  711. }
  712. CloseHandle(hSnapShot);
  713. return "";
  714. }
  715. BOOL IsVista()
  716. {
  717. OSVERSIONINFO osver;
  718. osver.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
  719. if (::GetVersionEx( &osver ) &&
  720. osver.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  721. (osver.dwMajorVersion >= 6 ) )
  722. {
  723. return TRUE;
  724. }
  725. return FALSE;
  726. }
  727. bool IsRunningLimited()
  728. {
  729. LPCTSTR pszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Policies\\System");
  730. LPCTSTR pszValue = _T("EnableLUA");
  731. DWORD dwType = 0;
  732. DWORD dwValue = 0;
  733. DWORD dwValueSize = sizeof(DWORD);
  734. if(ERROR_SUCCESS != SHGetValue(HKEY_LOCAL_MACHINE, pszSubKey, pszValue, &dwType, &dwValue, &dwValueSize))
  735. {
  736. //failed to read the reg key, either it's not there or we don't have access to the registry
  737. //If we are vista then assume we don't have access and we are running as a limited app
  738. //otherwise we are xp and the reg key probably doesn't exist and we are not a limited running app
  739. if(IsVista())
  740. {
  741. OutputDebugString(_T("Ditto - Failed to read registry entry finding UAC, Running as limited application"));
  742. return true;
  743. }
  744. }
  745. if(dwValue == 1)
  746. {
  747. OutputDebugString(_T("Ditto - UAC ENABLED, Running as limited application"));
  748. return true;
  749. }
  750. OutputDebugString(_T("Ditto - Running as standard application"));
  751. return false;
  752. }
  753. void DeleteReceivedFiles(CString csDir)
  754. {
  755. if(csDir.Find(_T("\\ReceivedFiles\\")) == -1)
  756. return;
  757. FIX_CSTRING_PATH(csDir);
  758. CTime ctOld = CTime::GetCurrentTime();
  759. CTime ctFile;
  760. ctOld -= CTimeSpan(0, 0, 0, 1);
  761. CFileFind Find;
  762. CString csFindString;
  763. csFindString.Format(_T("%s*.*"), csDir);
  764. BOOL bFound = Find.FindFile(csFindString);
  765. while(bFound)
  766. {
  767. bFound = Find.FindNextFile();
  768. if(Find.IsDots())
  769. continue;
  770. if(Find.IsDirectory())
  771. {
  772. CString csDir(Find.GetFilePath());
  773. DeleteReceivedFiles(csDir);
  774. RemoveDirectory(csDir);
  775. }
  776. if(Find.GetLastAccessTime(ctFile))
  777. {
  778. //Delete the remote copied file if it has'nt been used for the last day
  779. if(ctFile < ctOld)
  780. {
  781. DeleteFile(Find.GetFilePath());
  782. }
  783. }
  784. else
  785. {
  786. DeleteFile(Find.GetFilePath());
  787. }
  788. }
  789. }