UAC_Thread.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "stdafx.h"
  2. #include "UAC_Thread.h"
  3. #include "Misc.h"
  4. #include "Options.h"
  5. #include "QPasteWnd.h"
  6. #include "cp_main.h"
  7. CUAC_Thread::CUAC_Thread(int processId)
  8. {
  9. m_processId = processId;
  10. AddEvent(UAC_PASTE, StrF(_T("Global\\UAC_PASTE_%d"), m_processId));
  11. AddEvent(UAC_EXIT, StrF(_T("Global\\UAC_EXIT_%d"), m_processId));
  12. m_waitTimeout = 30000;
  13. }
  14. CUAC_Thread::~CUAC_Thread(void)
  15. {
  16. }
  17. void CUAC_Thread::OnTimeOut(void *param)
  18. {
  19. }
  20. void CUAC_Thread::OnEvent(int eventId, void *param)
  21. {
  22. DWORD startTick = GetTickCount();
  23. Log(StrF(_T("Start of OnEvent, eventId: %s"), EnumName((eUacThreadEvents)eventId)));
  24. switch((eUacThreadEvents)eventId)
  25. {
  26. case UAC_PASTE:
  27. theApp.m_activeWnd.SendPaste(false);
  28. break;
  29. case UAC_EXIT:
  30. this->CancelThread();
  31. break;
  32. }
  33. DWORD length = GetTickCount() - startTick;
  34. Log(StrF(_T("End of OnEvent, eventId: %s, Time: %d(ms)"), EnumName((eUacThreadEvents)eventId), length));
  35. }
  36. CString CUAC_Thread::EnumName(eUacThreadEvents e)
  37. {
  38. switch(e)
  39. {
  40. case UAC_PASTE:
  41. return _T("Paste Elevated");
  42. case UAC_EXIT:
  43. return _T("Save Startup Elevated");
  44. }
  45. return _T("");
  46. }
  47. void CUAC_Thread::UACPaste()
  48. {
  49. CString mutexName;
  50. mutexName.Format(_T("DittoAdminPaste_%d"), GetCurrentProcessId());
  51. HANDLE mutex = CreateMutex(NULL, FALSE, mutexName);
  52. DWORD dwError = GetLastError();
  53. if(dwError == ERROR_ALREADY_EXISTS)
  54. {
  55. }
  56. else
  57. {
  58. wchar_t szPath[MAX_PATH];
  59. if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
  60. {
  61. // Launch itself as administrator.
  62. SHELLEXECUTEINFO sei = { sizeof(sei) };
  63. sei.lpVerb = L"runas";
  64. sei.lpFile = szPath;
  65. CString csParam;
  66. csParam.Format(_T("/uacpaste:%d"), GetCurrentProcessId());
  67. sei.lpParameters = csParam;
  68. sei.nShow = SW_NORMAL;
  69. if (!ShellExecuteEx(&sei))
  70. {
  71. }
  72. }
  73. }
  74. FirePaste();
  75. CloseHandle(mutex);
  76. }