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.7KB

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 <stdio.h>
  29. namespace rfb {
  30. namespace win32 {
  31. FileVersionInfo::FileVersionInfo(const TCHAR* filename) {
  32. // Get executable name
  33. ModuleFileName exeName;
  34. if (!filename)
  35. filename = exeName.buf;
  36. // Attempt to open the file, to cause Access Denied, etc, errors
  37. // to be correctly reported, since the GetFileVersionInfoXXX calls lie...
  38. {
  39. Handle file(CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0));
  40. if (file.h == INVALID_HANDLE_VALUE)
  41. throw rdr::SystemException("Failed to open file", GetLastError());
  42. }
  43. // Get version info size
  44. DWORD handle;
  45. int size = GetFileVersionInfoSize((TCHAR*)filename, &handle);
  46. if (!size)
  47. throw rdr::SystemException("GetVersionInfoSize failed", GetLastError());
  48. // Get version info
  49. buf = new TCHAR[size];
  50. if (!GetFileVersionInfo((TCHAR*)filename, handle, size, buf))
  51. throw rdr::SystemException("GetVersionInfo failed", GetLastError());
  52. }
  53. const TCHAR* FileVersionInfo::getVerString(const TCHAR* name, DWORD langId) {
  54. char langIdBuf[sizeof(langId)];
  55. for (int i=sizeof(langIdBuf)-1; i>=0; i--) {
  56. langIdBuf[i] = (char) (langId & 0xff);
  57. langId = langId >> 8;
  58. }
  59. TCharArray langIdStr(rdr::HexOutStream::binToHexStr(langIdBuf, sizeof(langId)));
  60. TCharArray infoName(_tcslen(_T("StringFileInfo")) + 4 + _tcslen(name) + _tcslen(langIdStr.buf));
  61. _stprintf(infoName.buf, _T("\\StringFileInfo\\%s\\%s"), langIdStr.buf, name);
  62. // Locate the required version string within the version info
  63. TCHAR* buffer = 0;
  64. UINT length = 0;
  65. if (!VerQueryValue(buf, infoName.buf, (void**)&buffer, &length)) {
  66. printf("unable to find %s version string", infoName.buf);
  67. throw rdr::Exception("VerQueryValue failed");
  68. }
  69. return buffer;
  70. }
  71. bool splitPath(const TCHAR* path, TCHAR** dir, TCHAR** file) {
  72. return tstrSplit(path, '\\', dir, file, true);
  73. }
  74. void centerWindow(HWND handle, HWND parent) {
  75. RECT r;
  76. MonitorInfo mi(parent ? parent : handle);
  77. if (!parent || !IsWindowVisible(parent) || !GetWindowRect(parent, &r))
  78. r=mi.rcWork;
  79. centerWindow(handle, r);
  80. mi.clipTo(handle);
  81. }
  82. void centerWindow(HWND handle, const RECT& r) {
  83. RECT wr;
  84. if (!GetWindowRect(handle, &wr)) return;
  85. int w = wr.right-wr.left;
  86. int h = wr.bottom-wr.top;
  87. int x = (r.left + r.right - w)/2;
  88. int y = (r.top + r.bottom - h)/2;
  89. UINT flags = SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOSIZE;
  90. SetWindowPos(handle, 0, x, y, 0, 0, flags);
  91. }
  92. void resizeWindow(HWND handle, int width, int height) {
  93. RECT r;
  94. GetWindowRect(handle, &r);
  95. SetWindowPos(handle, 0, 0, 0, width, height, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
  96. centerWindow(handle, r);
  97. }
  98. };
  99. };