Menu

[r1536]: / trunk / GoogleContactsSync / OutlookRegistryUtils.cs  Maximize  Restore  History

Download this file

343 lines (317 with data), 15.0 kB

  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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
namespace GoContactSyncMod
{
internal static class OutlookRegistryUtils
{
public static string GetOutlookVersion()
{
var ret = string.Empty;
RegistryKey registryOutlookKey = null;
try
{
var outlookVersion = GetMajorVersion(GetOutlookPath());
ret = GetMajorVersionToString(outlookVersion);
var outlookKey = @"Software\Wow6432Node\Microsoft\Office\" + outlookVersion + @".0\Outlook";
registryOutlookKey = Registry.LocalMachine.OpenSubKey(outlookKey, false);
if (registryOutlookKey == null)
{
outlookKey = @"Software\Microsoft\Office\" + outlookVersion + @".0\Outlook";
registryOutlookKey = Registry.LocalMachine.OpenSubKey(outlookKey, false);
}
if (registryOutlookKey != null)
{
var bitness = registryOutlookKey.GetValue(@"Bitness", string.Empty).ToString();
if (string.IsNullOrEmpty(bitness))
{
return ret;
}
else
{
if (bitness == @"x86")
{
return ret + @" (32-bit)";
}
else
{
return bitness == @"x64" ? ret + @" (64-bit)" : ret;
}
}
}
}
catch
{
}
finally
{
if (registryOutlookKey != null)
{
registryOutlookKey.Close();
}
}
return ret;
}
public static string GetPossibleErrorDiagnosis()
{
var outlookVersion = GetMajorVersion(GetOutlookPath());
var diagnosis = CheckOfficeRegistry(outlookVersion);
var c2r = GetClickToRunVersion();
var ret = "Could not connect to 'Microsoft Outlook'.\r\nYou have " + GetMajorVersionToString(outlookVersion) + " installed.";
if (!string.IsNullOrEmpty(c2r))
{
ret += "Click to Run was also detected (" + c2r + ").";
}
return ret + "\r\n" + diagnosis;
}
private static string CheckOfficeRegistry(int outlookVersion)
{
var registryVersion = ConvertMajorVersionToRegistryVersion(outlookVersion);
var interfaceVersion = @"Interface\{00063001-0000-0000-C000-000000000046}\TypeLib";
var interfaceKey = Registry.ClassesRoot.OpenSubKey(interfaceVersion, false);
if (interfaceKey == null)
{
interfaceVersion = @"WOW6432Node\Interface\{00063001-0000-0000-C000-000000000046}\TypeLib";
interfaceKey = Registry.ClassesRoot.OpenSubKey(interfaceVersion, false);
}
if (interfaceKey != null)
{
var typeLib = interfaceKey.GetValue(string.Empty).ToString();
if (typeLib != "{00062FFF-0000-0000-C000-000000000046}")
{
return "Your registry " + interfaceKey.ToString() + " points to TypeLib " + typeLib + " and should to {00062FFF-0000-0000-C000-000000000046}" + registryVersion + ".\r\nPlease read FAQ (https://sourceforge.net/p/googlesyncmod/faq/2016/05/fixing-office-installation/) and fix your Office installation";
}
var versionObj = interfaceKey.GetValue("Version");
if (versionObj != null)
{
var version = versionObj.ToString();
if (version != registryVersion)
{
return "Your registry " + interfaceKey.ToString() + " points to version " + version + " and your Outlook is installed with version " + registryVersion + ".\r\nPlease read FAQ (https://sourceforge.net/p/googlesyncmod/faq/2016/05/fixing-office-installation/) and fix your Office installation";
}
}
else
{
return "There is no version key in registry " + interfaceKey.ToString() + ".\r\nPlease read FAQ (https://sourceforge.net/p/googlesyncmod/faq/2016/05/fixing-office-installation/) and fix your Office installation";
}
}
else
{
return "Cannot open registry " + interfaceVersion + ".\r\nPlease read FAQ (https://sourceforge.net/p/googlesyncmod/faq/2016/05/fixing-office-installation/) and fix your Office installation";
}
if (!string.IsNullOrEmpty(registryVersion))
{
var RegKey = @"TypeLib\{00062FFF-0000-0000-C000-000000000046}\" + registryVersion + @"\0\";
var mainKey = Registry.ClassesRoot.OpenSubKey(RegKey + "win32", false);
if (mainKey != null)
{
var path = mainKey.GetValue(string.Empty).ToString();
if (!File.Exists(path))
{
return "Your registry " + mainKey.ToString() + " points to file " + path + " and this file does not exist.\r\nPlease read FAQ (https://sourceforge.net/p/googlesyncmod/faq/2016/05/fixing-office-installation/) and fix your Office installation";
}
}
mainKey = Registry.ClassesRoot.OpenSubKey(RegKey + "win64", false);
if (mainKey != null)
{
var path = mainKey.GetValue(string.Empty).ToString();
if (!File.Exists(path))
{
return "Your registry " + mainKey.ToString() + " points to file " + path + " and this file does not exist.\r\nPlease read FAQ (https://sourceforge.net/p/googlesyncmod/faq/2016/05/fixing-office-installation/) and fix your Office installation";
}
}
mainKey = Registry.ClassesRoot.OpenSubKey(@"TypeLib\{00062FFF-0000-0000-C000-000000000046}\", false);
if (mainKey != null)
{
var keys = mainKey.GetSubKeyNames();
if (keys.Length > 1)
{
var allKeys = "";
for (var i = 0; i < keys.Length; i++)
{
var element = keys[i];
if (element != registryVersion)
{
allKeys = allKeys + '"' + ConvertRegistryVersionToString(element) + '"' + ",";
}
}
allKeys = allKeys.Substring(0, allKeys.Length - 1);
return "Your registry " + mainKey.ToString() + " points to Office versions: " + allKeys + " other than you have installed \"" + ConvertRegistryVersionToString(registryVersion) + "\".\r\nPlease read FAQ (https://sourceforge.net/p/googlesyncmod/faq/2016/05/fixing-office-installation/) and fix your Office installation";
}
}
}
return "Please read FAQ (https://sourceforge.net/p/googlesyncmod/faq/2016/05/fixing-office-installation/) and fix your Office installation";
}
private static string GetClickToRunVersion()
{
string[] keys = { @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Office\", @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\" };
string[] versions = { "9.0", "10.0", "11.0", "12.0", "14.0", "15.0", "16.0" };
for (var j = 0; j < keys.Length; j++)
{
for (var i = 0; i < versions.Length; i++)
{
try
{
var registryKey = Registry.LocalMachine.OpenSubKey(keys[j] + versions[i] + @"\Common\InstallRoot\Virtual\VirtualOutlook", false);
if (registryKey != null)
{
return versions[i];
}
}
finally
{
}
}
}
return string.Empty;
}
private static string GetOutlookPath()
{
const string regKey = @"Software\Microsoft\Windows\CurrentVersion\App Paths\outlook.exe";
var toReturn = string.Empty;
try
{
var mainKey = Registry.CurrentUser.OpenSubKey(regKey, false);
if (mainKey != null)
{
toReturn = mainKey.GetValue(string.Empty).ToString();
}
}
catch
{ }
if (string.IsNullOrEmpty(toReturn))
{
try
{
var mainKey = Registry.LocalMachine.OpenSubKey(regKey, false);
if (mainKey != null)
{
toReturn = mainKey.GetValue(string.Empty).ToString();
}
}
catch
{ }
}
return toReturn;
}
private static int GetMajorVersion(string path)
{
var toReturn = 0;
if (File.Exists(path))
{
try
{
toReturn = FileVersionInfo.GetVersionInfo(path).FileMajorPart;
}
catch
{ }
}
return toReturn;
}
private static string ConvertMajorVersionToRegistryVersion(int version)
{
switch (version)
{
case 9: return "9.0";
case 10: return "9.1";
case 11: return "9.2";
case 12: return "9.3";
case 14: return "9.4";
case 15: return "9.5";
case 16: return "9.6";
default: return Convert.ToString(version);
}
}
private static string ConvertRegistryVersionToString(string version)
{
switch (version)
{
case "9.0": return "Office 2000";
case "9.1": return "Office XP";
case "9.2": return "Office 2003";
case "9.3": return "Office 2007";
case "9.4": return "Office 2010";
case "9.5": return "Office 2013";
case "9.6": return "Office 2016";
default: return version;
}
}
private static string GetMajorVersionToString(int version)
{
switch (version)
{
case 7: return "Office 97";
case 8: return "Office 98";
case 9: return "Office 2000";
case 10: return "Office XP";
case 11: return "Office 2003";
case 12: return "Office 2007";
case 14: return "Office 2010";
case 15: return "Office 2013";
case 16:
{
var regKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Office\ClickToRun";
try
{
var mainKey = Registry.LocalMachine.OpenSubKey(regKey, false);
if (mainKey == null)
{
regKey = @"Software\Microsoft\Office\ClickToRun";
mainKey = Registry.LocalMachine.OpenSubKey(regKey, false);
if (mainKey == null)
{
return "Office 2016 / Office 2019 / Office 365";
}
}
}
catch
{
return "Office 2016 / Office 2019 / Office 365";
}
//regKey = @"Software\Microsoft\Office\ClickToRun\Configuration\ProductReleaseIDs";
regKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Office\ClickToRun\Configuration\ProductReleaseIDs";
try
{
var mainKey = Registry.LocalMachine.OpenSubKey(regKey, false);
if (mainKey != null)
{
var s = mainKey.GetValue(string.Empty).ToString();
if (s == "ProPlus2019Retail")
{
return "Office 2019";
}
else
{
return s == "O365ProPlusRetail" ? "Office 365" : s;
}
}
else
{
regKey = @"Software\Microsoft\Office\ClickToRun\Configuration\ProductReleaseIDs";
mainKey = Registry.LocalMachine.OpenSubKey(regKey, false);
if (mainKey != null)
{
var s = mainKey.GetValue(string.Empty).ToString();
if (s == "ProPlus2019Retail")
{
return "Office 2019";
}
else
{
return s == "O365ProPlusRetail" ? "Office 365" : s;
}
}
}
}
catch
{
return "Office 2016 / Office 2019 / Office 365";
}
return "Office 2016 / Office 2019 / Office 365";
}
default: return "unknown Office version (" + version + ")";
}
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.