Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. HRESULT SaveIcon(HICON hIcon, const char* path)
  2. {
  3. // Create the IPicture intrface
  4. PICTDESC desc = { sizeof(PICTDESC) };
  5. desc.picType = PICTYPE_ICON;
  6. desc.icon.hicon = hIcon;
  7. IPicture* pPicture = 0;
  8. HRESULT hr = OleCreatePictureIndirect(&desc, IID_IPicture, FALSE, (void**)&pPicture);
  9. if (FAILED(hr)) return hr;
  10.  
  11. // Create a stream and save the image
  12. IStream* pStream = 0;
  13. CreateStreamOnHGlobal(0, TRUE, &pStream);
  14. LONG cbSize = 0;
  15. hr = pPicture->SaveAsFile(pStream, TRUE, &cbSize);
  16.  
  17. // Write the stream content to the file
  18. if (!FAILED(hr))
  19. {
  20. HGLOBAL hBuf = 0;
  21. GetHGlobalFromStream(pStream, &hBuf);
  22. void* buffer = GlobalLock(hBuf);
  23. HANDLE hFile = CreateFileA(path, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
  24. if (!hFile)
  25. {
  26. hr = HRESULT_FROM_WIN32(GetLastError());
  27. }
  28. else
  29. {
  30. DWORD written = 0;
  31. WriteFile(hFile, buffer, cbSize, &written, 0);
  32. CloseHandle(hFile);
  33. }
  34. GlobalUnlock(buffer);
  35. }
  36. // Cleanup
  37. pStream->Release();
  38. pPicture->Release();
  39. return hr;
  40. }
  41.  
  42. //Capture cursor.
  43. CURSORINFO getHCursor()
  44. {
  45. CURSORINFO cursorInfo;
  46. cursorInfo.cbSize = sizeof(CURSORINFO);
  47.  
  48. if (GetCursorInfo(&cursorInfo) == 0)
  49. {
  50. MessageBox(NULL, _T("Exception : GetCursorInfo creation failed"),_T("message"),MB_OK|MB_SYSTEMMODAL);
  51. cursorInfo.hCursor = NULL;
  52. return cursorInfo;
  53. }
  54.  
  55. return cursorInfo;
  56. }
  57.  
  58. //Main Call
  59. int _tmain(int argc, _TCHAR* argv[])
  60. {
  61. while (true)
  62. {
  63. CURSORINFO CursorInfo = getHCursor();
  64. if (CursorInfo.hCursor == NULL)
  65. {
  66. ::Sleep(MinSleep);
  67. continue;
  68. }
  69.  
  70. SaveIcon((HICON)CursorInfo.hCursor, "C:\Users\Desktop\myicon.ico");
  71.  
  72. Sleep(MaxSleep);
  73. }
  74. return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement