aboutsummaryrefslogtreecommitdiffstats
path: root/win/rfb_win32/Win32Util.cxx
blob: 2fbce0dd2b007c68ab97ba55b7056ac3a0c98756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Copyright (C) 2002-2005 RealVNC Ltd.  All Rights Reserved.
 * 
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

// Win32Util.cxx

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <rfb_win32/ModuleFileName.h>
#include <rfb_win32/Win32Util.h>
#include <rfb_win32/MonitorInfo.h>
#include <rfb_win32/Handle.h>
#include <rdr/HexOutStream.h>
#include <rdr/Exception.h>
#include <stdio.h>

namespace rfb {
namespace win32 {


FileVersionInfo::FileVersionInfo(const TCHAR* filename) {
  // Get executable name
  ModuleFileName exeName;
  if (!filename)
    filename = exeName.buf;

  // Attempt to open the file, to cause Access Denied, etc, errors
  // to be correctly reported, since the GetFileVersionInfoXXX calls lie...
  {
    Handle file(CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0));
	  if (file.h == INVALID_HANDLE_VALUE)
      throw rdr::SystemException("Failed to open file", GetLastError());
  }

  // Get version info size
  DWORD handle;
  int size = GetFileVersionInfoSize((TCHAR*)filename, &handle);
  if (!size)
    throw rdr::SystemException("GetVersionInfoSize failed", GetLastError());

  // Get version info
  buf = new TCHAR[size];
  if (!GetFileVersionInfo((TCHAR*)filename, handle, size, buf))
    throw rdr::SystemException("GetVersionInfo failed", GetLastError());
}

const TCHAR* FileVersionInfo::getVerString(const TCHAR* name, DWORD langId) {
  uint8_t langIdBuf[sizeof(langId)];
  for (int i=sizeof(langIdBuf)-1; i>=0; i--) {
    langIdBuf[i] = (langId & 0xff);
    langId = langId >> 8;
  }

  TCharArray langIdStr(binToHex(langIdBuf, sizeof(langId)));
  TCharArray infoName(_tcslen(_T("StringFileInfo")) + 4 + _tcslen(name) + _tcslen(langIdStr.buf));
  _stprintf(infoName.buf, _T("\\StringFileInfo\\%s\\%s"), langIdStr.buf, name);

  // Locate the required version string within the version info
  TCHAR* buffer = 0;
  UINT length = 0;
  if (!VerQueryValue(buf, infoName.buf, (void**)&buffer, &length)) {
    printf("unable to find %s version string", infoName.buf);
    throw rdr::Exception("VerQueryValue failed");
  }
  return buffer;
}


bool splitPath(const TCHAR* path, TCHAR** dir, TCHAR** file) {
  return tstrSplit(path, '\\', dir, file, true);
}


void centerWindow(HWND handle, HWND parent) {
  RECT r;
  MonitorInfo mi(parent ? parent : handle);
  if (!parent || !IsWindowVisible(parent) || !GetWindowRect(parent, &r))
    r=mi.rcWork;
  centerWindow(handle, r);
  mi.clipTo(handle);
}

void centerWindow(HWND handle, const RECT& r) {
  RECT wr;
  if (!GetWindowRect(handle, &wr)) return;
  int w = wr.right-wr.left;
  int h = wr.bottom-wr.top;
  int x = (r.left + r.right - w)/2;
  int y = (r.top + r.bottom - h)/2;
  UINT flags = SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOSIZE;
  SetWindowPos(handle, 0, x, y, 0, 0, flags);
}

void resizeWindow(HWND handle, int width, int height) {
  RECT r;
  GetWindowRect(handle, &r);
  SetWindowPos(handle, 0, 0, 0, width, height, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
  centerWindow(handle, r);
}


};
};