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