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

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