You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Win32Util.cxx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. // Win32Util.cxx
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <rfb_win32/ModuleFileName.h>
  23. #include <rfb_win32/Win32Util.h>
  24. #include <rfb_win32/MonitorInfo.h>
  25. #include <rfb_win32/Handle.h>
  26. #include <rdr/HexOutStream.h>
  27. #include <rdr/Exception.h>
  28. #include <rfb/util.h>
  29. #include <stdio.h>
  30. namespace rfb {
  31. namespace win32 {
  32. FileVersionInfo::FileVersionInfo(const char* filename) {
  33. // Get executable name
  34. ModuleFileName exeName;
  35. if (!filename)
  36. filename = exeName.buf;
  37. // Attempt to open the file, to cause Access Denied, etc, errors
  38. // to be correctly reported, since the GetFileVersionInfoXXX calls lie...
  39. {
  40. Handle file(CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0));
  41. if (file.h == INVALID_HANDLE_VALUE)
  42. throw rdr::SystemException("Failed to open file", GetLastError());
  43. }
  44. // Get version info size
  45. DWORD handle;
  46. int size = GetFileVersionInfoSize((char*)filename, &handle);
  47. if (!size)
  48. throw rdr::SystemException("GetVersionInfoSize failed", GetLastError());
  49. // Get version info
  50. buf = new char[size];
  51. if (!GetFileVersionInfo((char*)filename, handle, size, buf))
  52. throw rdr::SystemException("GetVersionInfo failed", GetLastError());
  53. }
  54. FileVersionInfo::~FileVersionInfo() {
  55. delete [] buf;
  56. }
  57. const char* FileVersionInfo::getVerString(const char* name, DWORD langId) {
  58. uint8_t langIdBuf[sizeof(langId)];
  59. for (int i=sizeof(langIdBuf)-1; i>=0; i--) {
  60. langIdBuf[i] = (langId & 0xff);
  61. langId = langId >> 8;
  62. }
  63. std::string langIdStr(binToHex(langIdBuf, sizeof(langId)));
  64. std::string infoName;
  65. infoName = strFormat("\\StringFileInfo\\%s\\%s", langIdStr.c_str(), name);
  66. // Locate the required version string within the version info
  67. char* buffer = 0;
  68. UINT length = 0;
  69. if (!VerQueryValue(buf, infoName.c_str(), (void**)&buffer, &length)) {
  70. printf("unable to find %s version string", infoName.c_str());
  71. throw rdr::Exception("VerQueryValue failed");
  72. }
  73. return buffer;
  74. }
  75. void centerWindow(HWND handle, HWND parent) {
  76. RECT r;
  77. MonitorInfo mi(parent ? parent : handle);
  78. if (!parent || !IsWindowVisible(parent) || !GetWindowRect(parent, &r))
  79. r=mi.rcWork;
  80. centerWindow(handle, r);
  81. mi.clipTo(handle);
  82. }
  83. void centerWindow(HWND handle, const RECT& r) {
  84. RECT wr;
  85. if (!GetWindowRect(handle, &wr)) return;
  86. int w = wr.right-wr.left;
  87. int h = wr.bottom-wr.top;
  88. int x = (r.left + r.right - w)/2;
  89. int y = (r.top + r.bottom - h)/2;
  90. UINT flags = SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOSIZE;
  91. SetWindowPos(handle, 0, x, y, 0, 0, flags);
  92. }
  93. void resizeWindow(HWND handle, int width, int height) {
  94. RECT r;
  95. GetWindowRect(handle, &r);
  96. SetWindowPos(handle, 0, 0, 0, width, height, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
  97. centerWindow(handle, r);
  98. }
  99. };
  100. };