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