Skip to content

Commit 24e05d2

Browse files
committed
Updating IE driver C++ code to use Visual Studio 2015.
Also cleaning up deprecation warnings and compile errors.
1 parent c81d9e2 commit 24e05d2

12 files changed

+148
-77
lines changed

cpp/iedriver/BrowserFactory.cpp

+12-52
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#include <sddl.h>
2424
#include <shlguid.h>
2525
#include <shlobj.h>
26+
#include <VersionHelpers.h>
2627
#include <WinInet.h>
28+
#include "FileUtilities.h"
2729
#include "logging.h"
2830
#include "psapi.h"
2931
#include "RegistryUtilities.h"
@@ -34,7 +36,6 @@ BrowserFactory::BrowserFactory(void) {
3436
// Must be done in the constructor. Do not move to Initialize().
3537
this->GetExecutableLocation();
3638
this->GetIEVersion();
37-
this->GetOSVersion();
3839
}
3940

4041
BrowserFactory::~BrowserFactory(void) {
@@ -63,7 +64,7 @@ void BrowserFactory::Initialize(BrowserFactorySettings settings) {
6364
void BrowserFactory::ClearCache() {
6465
LOG(TRACE) << "Entering BrowserFactory::ClearCache";
6566
if (this->clear_cache_) {
66-
if (this->windows_major_version_ >= 6) {
67+
if (::IsWindowsVistaOrGreater()) {
6768
LOG(DEBUG) << "Clearing cache with low mandatory integrity level as required on Windows Vista or later.";
6869
this->InvokeClearCacheUtility(true);
6970
}
@@ -634,7 +635,7 @@ IWebBrowser2* BrowserFactory::CreateBrowser() {
634635

635636
IWebBrowser2* browser = NULL;
636637
DWORD context = CLSCTX_LOCAL_SERVER;
637-
if (this->ie_major_version_ == 7 && this->windows_major_version_ >= 6) {
638+
if (this->ie_major_version_ == 7 && ::IsWindowsVistaOrGreater()) {
638639
// ONLY for IE 7 on Windows Vista. XP and below do not have Protected Mode;
639640
// Windows 7 shipped with IE8.
640641
context = context | CLSCTX_ENABLE_CLOAKING;
@@ -972,15 +973,9 @@ void BrowserFactory::GetExecutableLocation() {
972973
void BrowserFactory::GetIEVersion() {
973974
LOG(TRACE) << "Entering BrowserFactory::GetIEVersion";
974975

975-
struct LANGANDCODEPAGE {
976-
WORD language;
977-
WORD code_page;
978-
} *lpTranslate;
979-
980-
DWORD dummy = 0;
981-
DWORD length = ::GetFileVersionInfoSize(this->ie_executable_location_.c_str(),
982-
&dummy);
983-
if (length == 0) {
976+
std::string ie_version = FileUtilities::GetFileVersion(this->ie_executable_location_);
977+
978+
if (ie_version.size() == 0) {
984979
// 64-bit Windows 8 has a bug where it does not return the executable location properly
985980
this->ie_major_version_ = -1;
986981
LOG(WARN) << "Couldn't find IE version for executable "
@@ -989,60 +984,25 @@ void BrowserFactory::GetIEVersion() {
989984
<< this->ie_major_version_;
990985
return;
991986
}
992-
std::vector<BYTE> version_buffer(length);
993-
::GetFileVersionInfo(this->ie_executable_location_.c_str(),
994-
0, /* ignored */
995-
length,
996-
&version_buffer[0]);
997-
998-
UINT page_count;
999-
BOOL query_result = ::VerQueryValue(&version_buffer[0],
1000-
FILE_LANGUAGE_INFO,
1001-
reinterpret_cast<void**>(&lpTranslate),
1002-
&page_count);
1003-
1004-
wchar_t sub_block[MAX_PATH];
1005-
_snwprintf_s(sub_block,
1006-
MAX_PATH,
1007-
MAX_PATH,
1008-
FILE_VERSION_INFO,
1009-
lpTranslate->language,
1010-
lpTranslate->code_page);
1011-
LPVOID value = NULL;
1012-
UINT size;
1013-
query_result = ::VerQueryValue(&version_buffer[0],
1014-
sub_block,
1015-
&value,
1016-
&size);
1017-
std::wstring ie_version;
1018-
ie_version.assign(static_cast<wchar_t*>(value));
1019-
std::wstringstream version_stream(ie_version);
1020-
version_stream >> this->ie_major_version_;
1021-
}
1022-
1023-
void BrowserFactory::GetOSVersion() {
1024-
LOG(TRACE) << "Entering BrowserFactory::GetOSVersion";
1025987

1026-
OSVERSIONINFO osVersion;
1027-
osVersion.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
1028-
::GetVersionEx(&osVersion);
1029-
this->windows_major_version_ = osVersion.dwMajorVersion;
1030-
this->windows_minor_version_ = osVersion.dwMinorVersion;
988+
std::stringstream version_stream(ie_version);
989+
version_stream >> this->ie_major_version_;
1031990
}
1032991

1033992
bool BrowserFactory::ProtectedModeSettingsAreValid() {
1034993
LOG(TRACE) << "Entering BrowserFactory::ProtectedModeSettingsAreValid";
1035994

1036995
bool settings_are_valid = true;
1037996
LOG(DEBUG) << "Detected IE version: " << this->ie_major_version_
1038-
<< ", detected Windows version: " << this->windows_major_version_;
997+
<< ", Windows version supports Protected Mode: "
998+
<< ::IsWindowsVistaOrGreater() ? "true" : "false";
1039999
// Only need to check Protected Mode settings on IE 7 or higher
10401000
// and on Windows Vista or higher. Otherwise, Protected Mode
10411001
// doesn't come into play, and are valid.
10421002
// Documentation of registry settings can be found at the following
10431003
// Microsoft KnowledgeBase article:
10441004
// http://support.microsoft.com/kb/182569
1045-
if (this->ie_major_version_ >= 7 && this->windows_major_version_ >= 6) {
1005+
if (this->ie_major_version_ >= 7 && ::IsWindowsVistaOrGreater()) {
10461006
HKEY key_handle;
10471007
if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_CURRENT_USER,
10481008
IE_SECURITY_ZONES_REGISTRY_KEY,

cpp/iedriver/BrowserFactory.h

-8
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
#define ALERT_WINDOW_CLASS "#32770"
3131
#define HTML_DIALOG_WINDOW_CLASS "Internet Explorer_TridentDlgFrame"
3232

33-
#define FILE_LANGUAGE_INFO L"\\VarFileInfo\\Translation"
34-
#define FILE_VERSION_INFO L"\\StringFileInfo\\%04x%04x\\FileVersion"
35-
3633
#define IE_CLSID_REGISTRY_KEY L"SOFTWARE\\Classes\\InternetExplorer.Application\\CLSID"
3734
#define IE_SECURITY_ZONES_REGISTRY_KEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones"
3835
#define IE_TABPROCGROWTH_REGISTRY_KEY L"Software\\Microsoft\\Internet Explorer\\Main"
@@ -126,8 +123,6 @@ class BrowserFactory {
126123
}
127124

128125
int browser_version(void) const { return this->ie_major_version_; }
129-
int windows_major_version(void) const { return this->windows_major_version_; }
130-
int windows_minor_version(void) const { return this->windows_minor_version_; }
131126

132127
static BOOL CALLBACK FindChildWindowForProcess(HWND hwnd, LPARAM arg);
133128
static BOOL CALLBACK FindDialogWindowForProcess(HWND hwnd, LPARAM arg);
@@ -150,7 +145,6 @@ class BrowserFactory {
150145

151146
void GetExecutableLocation(void);
152147
void GetIEVersion(void);
153-
void GetOSVersion(void);
154148
bool ProtectedModeSettingsAreValid(void);
155149
int GetZoneProtectedModeSetting(const HKEY key_handle,
156150
const std::wstring& zone_subkey_name);
@@ -176,8 +170,6 @@ class BrowserFactory {
176170
int browser_attach_timeout_;
177171

178172
int ie_major_version_;
179-
int windows_major_version_;
180-
int windows_minor_version_;
181173
std::wstring ie_executable_location_;
182174
};
183175

cpp/iedriver/FileUtilities.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#include "FileUtilities.h"
18+
19+
#define FILE_LANGUAGE_INFO L"\\VarFileInfo\\Translation"
20+
#define FILE_VERSION_INFO L"\\StringFileInfo\\%04x%04x\\FileVersion"
21+
22+
namespace webdriver {
23+
24+
FileUtilities::FileUtilities(void) {
25+
}
26+
27+
FileUtilities::~FileUtilities(void) {
28+
}
29+
30+
std::string FileUtilities::GetFileVersion(const std::string& file_name) {
31+
return GetFileVersion(StringUtilities::ToWString(file_name));
32+
}
33+
34+
std::string FileUtilities::GetFileVersion(const std::wstring& file_name) {
35+
struct LANGANDCODEPAGE {
36+
WORD language;
37+
WORD code_page;
38+
} *language_info;
39+
40+
DWORD dummy = 0;
41+
DWORD length = ::GetFileVersionInfoSize(file_name.c_str(), &dummy);
42+
if (length == 0) {
43+
return "";
44+
}
45+
46+
std::vector<char> version_buffer(length);
47+
::GetFileVersionInfo(file_name.c_str(),
48+
0, /* ignored */
49+
length,
50+
&version_buffer[0]);
51+
52+
UINT page_count;
53+
BOOL query_result = ::VerQueryValue(&version_buffer[0],
54+
FILE_LANGUAGE_INFO,
55+
reinterpret_cast<void**>(&language_info),
56+
&page_count);
57+
58+
std::vector<wchar_t> sub_block(MAX_PATH);
59+
_snwprintf_s(&sub_block[0],
60+
MAX_PATH,
61+
MAX_PATH,
62+
FILE_VERSION_INFO,
63+
language_info->language,
64+
language_info->code_page);
65+
std::wstring sub_block_string = &sub_block[0];
66+
67+
void* value = NULL;
68+
unsigned int size;
69+
query_result = ::VerQueryValue(&version_buffer[0],
70+
sub_block_string.c_str(),
71+
&value,
72+
&size);
73+
std::wstring wide_version;
74+
wide_version.assign(static_cast<wchar_t*>(value));
75+
std::string version = StringUtilities::ToString(wide_version);
76+
return version;
77+
}
78+
79+
} // namespace webdriver

cpp/iedriver/FileUtilities.h

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#ifndef WEBDRIVER_IE_FILEUTILITIES_H_
18+
#define WEBDRIVER_IE_FILEUTILITIES_H_
19+
20+
#include <string>
21+
22+
23+
namespace webdriver {
24+
25+
class FileUtilities {
26+
private:
27+
FileUtilities();
28+
~FileUtilities();
29+
public:
30+
static std::string GetFileVersion(const std::string& file_name);
31+
static std::string GetFileVersion(const std::wstring& file_name);
32+
};
33+
34+
} // namespace webdriver
35+
36+
#endif // WEBDRIVER_IE_FILEUTILITIES_H_

cpp/iedriver/IEDriver.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
<ClCompile Include="Element.cpp" />
236236
<ClCompile Include="ElementFinder.cpp" />
237237
<ClCompile Include="ElementRepository.cpp" />
238+
<ClCompile Include="FileUtilities.cpp" />
238239
<ClCompile Include="HookProcessor.cpp" />
239240
<ClCompile Include="HtmlDialog.cpp" />
240241
<ClCompile Include="IECommandExecutor.cpp" />
@@ -277,6 +278,7 @@
277278
<ClInclude Include="Element.h" />
278279
<ClInclude Include="ElementFinder.h" />
279280
<ClInclude Include="ElementRepository.h" />
281+
<ClInclude Include="FileUtilities.h" />
280282
<ClInclude Include="HookProcessor.h" />
281283
<ClInclude Include="HtmlDialog.h" />
282284
<ClInclude Include="IECommandExecutor.h" />

cpp/iedriver/IEDriver.vcxproj.filters

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@
9898
<ClCompile Include="BrowserCookie.cpp">
9999
<Filter>Source Files</Filter>
100100
</ClCompile>
101+
<ClCompile Include="FileUtilities.cpp">
102+
<Filter>Source Files</Filter>
103+
</ClCompile>
101104
</ItemGroup>
102105
<ItemGroup>
103106
<ClInclude Include="Browser.h">
@@ -379,6 +382,9 @@
379382
<ClInclude Include="CommandHandlers\SetAlertCredentialsCommandHandler.h">
380383
<Filter>Header Files\CommandHandlers</Filter>
381384
</ClInclude>
385+
<ClInclude Include="FileUtilities.h">
386+
<Filter>Header Files</Filter>
387+
</ClInclude>
382388
</ItemGroup>
383389
<ItemGroup>
384390
<None Include="IEDriver.def">

cpp/iedriver/IEServer.cpp

+3-9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "IEServer.h"
1818
#include "IESession.h"
19+
#include <VersionHelpers.h>
20+
#include "FileUtilities.h"
1921
#include "logging.h"
2022

2123
namespace webdriver {
@@ -51,15 +53,7 @@ std::string IEServer::GetStatus() {
5153
::ZeroMemory(&system_info, sizeof(SYSTEM_INFO));
5254
::GetNativeSystemInfo(&system_info);
5355

54-
OSVERSIONINFO os_version_info;
55-
::ZeroMemory(&os_version_info, sizeof(OSVERSIONINFO));
56-
os_version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
57-
::GetVersionEx(&os_version_info);
58-
59-
std::string major_version = std::to_string(static_cast<long long>(os_version_info.dwMajorVersion));
60-
std::string minor_version = std::to_string(static_cast<long long>(os_version_info.dwMinorVersion));
61-
std::string build_version = std::to_string(static_cast<long long>(os_version_info.dwBuildNumber));
62-
std::string os_version = major_version + "." + minor_version + "." + build_version;
56+
std::string os_version = FileUtilities::GetFileVersion("kernel32.dll");
6357

6458
std::string arch = "x86";
6559
if (system_info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) {

cpp/iedriver/InputManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ bool InputManager::SetFocusToBrowser(BrowserHandle browser_wrapper) {
213213
}
214214
::SetForegroundWindow(browser_wrapper->GetTopLevelWindowHandle());
215215
if (current_thread_id != thread_id) {
216-
::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (PVOID)lock_timeout, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
216+
::SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, reinterpret_cast<void*>(lock_timeout), SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
217217
::AttachThreadInput(current_thread_id, thread_id, FALSE);
218218
}
219219
}

cpp/webdriver-interactions/webdriver-interactions.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
101101
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
102102
<WarningLevel>Level3</WarningLevel>
103-
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
103+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
104104
</ClCompile>
105105
<PostBuildEvent>
106106
<Command>

cpp/webdriver-server/webdriver-server.vcxproj

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
9191
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
9292
<PrecompiledHeaderFile>precompile.h</PrecompiledHeaderFile>
93+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
9394
</ClCompile>
9495
<Link>
9596
<GenerateDebugInformation>true</GenerateDebugInformation>
@@ -104,6 +105,7 @@
104105
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
105106
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
106107
<PrecompiledHeaderFile>precompile.h</PrecompiledHeaderFile>
108+
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
107109
</ClCompile>
108110
<Link>
109111
<GenerateDebugInformation>true</GenerateDebugInformation>

0 commit comments

Comments
 (0)