Skip to content

Commit 6ef8e0d

Browse files
committed
add some try/catch
1 parent 8d3c88a commit 6ef8e0d

3 files changed

Lines changed: 130 additions & 100 deletions

File tree

FlashpointSecurePlayer/ProcessSync.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ public static void Start(Process process = null) {
109109
IntPtr processHandle = IntPtr.Zero;
110110

111111
if (process == null) {
112-
using (process = Process.GetCurrentProcess()) {
113-
processHandle = process.Handle;
112+
try {
113+
using (process = Process.GetCurrentProcess()) {
114+
processHandle = process.Handle;
115+
}
116+
} catch {
117+
// fail silently
114118
}
115119
} else {
116120
processHandle = process.Handle;

FlashpointSecurePlayer/RegistryStates.cs

Lines changed: 117 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,11 @@ private string TestKeyDeletedInRegistryView(string keyName, RegistryView registr
496496
return keyNames.First();
497497
}
498498

499-
RegistryKey registrySubKey = null;
500-
501499
try {
502-
for (int i = 0; i < keyNames.Count - 1; i++) {
500+
RegistryKey registrySubKey = null;
501+
int subKeyNamesCount = keyNames.Count - 1;
502+
503+
for (int i = 0; i < subKeyNamesCount; i++) {
503504
keyName = keyNames[i + 1];
504505

505506
try {
@@ -1368,7 +1369,7 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod = M
13681369
}
13691370

13701371
TaskRequiresElevationException taskRequiresElevationException = null;
1371-
InvalidRegistryStateException invalidRegistryStateException = null;
1372+
Exception exception = null;
13721373

13731374
while (activeModificationsElement.RegistryStates.Count > 0) {
13741375
try {
@@ -1475,8 +1476,8 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod = M
14751476
}
14761477
} catch (TaskRequiresElevationException ex) {
14771478
taskRequiresElevationException = ex;
1478-
} catch (InvalidRegistryStateException ex) {
1479-
invalidRegistryStateException = ex;
1479+
} catch (Exception ex) {
1480+
exception = ex;
14801481
}
14811482

14821483
ProgressManager.CurrentGoal.Steps++;
@@ -1491,8 +1492,8 @@ public void Deactivate(MODIFICATIONS_REVERT_METHOD modificationsRevertMethod = M
14911492
throw taskRequiresElevationException;
14921493
}
14931494

1494-
if (invalidRegistryStateException != null) {
1495-
throw invalidRegistryStateException;
1495+
if (exception != null) {
1496+
throw exception;
14961497
}
14971498
} finally {
14981499
ProgressManager.CurrentGoal.Stop();
@@ -1526,27 +1527,35 @@ private void QueueModification(ulong safeKeyHandle, DateTime timeStamp, Registry
15261527
}
15271528

15281529
private void GotValue(RegistryTraceData registryTraceData) {
1529-
if (registryTraceData.ValueName != null
1530-
&& (registryTraceData.ProcessID == CurrentProcessId
1531-
|| registryTraceData.ProcessID == -1)) {
1532-
if (ImportPaused) {
1533-
if (registryTraceData.ValueName.Equals(IMPORT_RESUME, StringComparison.OrdinalIgnoreCase)) {
1534-
ImportPaused = false;
1535-
1536-
// hold here until after the control has installed
1537-
// that way we can recieve registry messages as they come in
1538-
// with reassurance the control has installed already
1539-
// therefore, key names will be redirected properly
1540-
if (resumeEventWaitHandle == null) {
1541-
throw new InvalidOperationException("resumeEventWaitHandle must not be NULL.");
1542-
}
1530+
try {
1531+
if (registryTraceData.ProcessID != CurrentProcessId && registryTraceData.ProcessID != -1) {
1532+
return;
1533+
}
1534+
} catch {
1535+
return;
1536+
}
15431537

1544-
resumeEventWaitHandle.WaitOne();
1545-
}
1546-
} else {
1547-
if (registryTraceData.ValueName.Equals(IMPORT_PAUSE, StringComparison.OrdinalIgnoreCase)) {
1548-
ImportPaused = true;
1538+
if (registryTraceData.ValueName == null) {
1539+
return;
1540+
}
1541+
1542+
if (ImportPaused) {
1543+
if (registryTraceData.ValueName.Equals(IMPORT_RESUME, StringComparison.OrdinalIgnoreCase)) {
1544+
ImportPaused = false;
1545+
1546+
// hold here until after the control has installed
1547+
// that way we can recieve registry messages as they come in
1548+
// with reassurance the control has installed already
1549+
// therefore, key names will be redirected properly
1550+
if (resumeEventWaitHandle == null) {
1551+
throw new InvalidOperationException("resumeEventWaitHandle must not be NULL.");
15491552
}
1553+
1554+
resumeEventWaitHandle.WaitOne();
1555+
}
1556+
} else {
1557+
if (registryTraceData.ValueName.Equals(IMPORT_PAUSE, StringComparison.OrdinalIgnoreCase)) {
1558+
ImportPaused = true;
15501559
}
15511560
}
15521561
}
@@ -1560,7 +1569,11 @@ private void ModificationAdded(RegistryTraceData registryTraceData) {
15601569
// if KCBModificationKeyNames has KeyHandle, add it to RegistryStates
15611570
// otherwise queue the modification
15621571
// check the registry change was made by this process (or an unknown process - might be ours)
1563-
if (registryTraceData.ProcessID != CurrentProcessId && registryTraceData.ProcessID != -1) {
1572+
try {
1573+
if (registryTraceData.ProcessID != CurrentProcessId && registryTraceData.ProcessID != -1) {
1574+
return;
1575+
}
1576+
} catch {
15641577
return;
15651578
}
15661579

@@ -1740,7 +1753,11 @@ private void ModificationRemoved(RegistryTraceData registryTraceData) {
17401753
// if KCBModificationKeyNames has KeyHandle, remove it from RegistryStates
17411754
// otherwise queue the modification
17421755
// check the registry change was made by this process (or an unknown process - might be ours)
1743-
if (registryTraceData.ProcessID != CurrentProcessId && registryTraceData.ProcessID != -1) {
1756+
try {
1757+
if (registryTraceData.ProcessID != CurrentProcessId && registryTraceData.ProcessID != -1) {
1758+
return;
1759+
}
1760+
} catch {
17441761
return;
17451762
}
17461763

@@ -1861,86 +1878,90 @@ private void KCBStopped(RegistryTraceData registryTraceData) {
18611878

18621879
// we want to take care of any queued registry timeline events
18631880
// an event entails the date and time of the registry modification
1864-
if (queuedModifications != null) {
1865-
queuedModifications.TryGetValue(safeKeyHandle, out SortedList<DateTime, List<RegistryStateElement>> timeStamps);
1881+
if (queuedModifications == null) {
1882+
return;
1883+
}
18661884

1867-
if (timeStamps != null) {
1868-
foreach (List<RegistryStateElement> registryStateElements in timeStamps.Values) {
1869-
// add its BaseKeyName
1870-
for (int j = 0; j < registryStateElements.Count; j++) {
1871-
registryStateElement = registryStateElements[j];
1885+
queuedModifications.TryGetValue(safeKeyHandle, out SortedList<DateTime, List<RegistryStateElement>> timeStamps);
18721886

1873-
registryStateElement.KeyName = GetRedirectedKeyValueName(
1874-
GetKeyValueNameFromKernelRegistryString(registryTraceData.KeyName + "\\" + registryStateElement.KeyName),
1875-
modificationsElement.RegistryStates.BinaryType
1876-
);
1887+
if (timeStamps == null) {
1888+
return;
1889+
}
18771890

1878-
try {
1879-
registryStateElement.ValueKind = GetValueKindInRegistryView(registryStateElement.KeyName, registryStateElement.ValueName, registryView);
1880-
} catch {
1881-
// value doesn't exist
1882-
registryStateElement.ValueKind = null;
1883-
}
1891+
foreach (List<RegistryStateElement> registryStateElements in timeStamps.Values) {
1892+
// add its BaseKeyName
1893+
for (int j = 0; j < registryStateElements.Count; j++) {
1894+
registryStateElement = registryStateElements[j];
18841895

1885-
try {
1886-
value = ReplaceStartupPathEnvironmentVariable(
1887-
LengthenValue(
1888-
GetValueInRegistryView(
1889-
registryStateElement.KeyName,
1890-
registryStateElement.ValueName,
1891-
registryView
1892-
) as string,
1893-
1894-
fullPath,
1895-
pathNames
1896-
),
1897-
1898-
pathNames
1899-
);
1900-
} catch (SecurityException ex) {
1901-
// value exists but we can't get it
1902-
// this shouldn't happen because this task requires elevation
1903-
LogExceptionToLauncher(ex);
1904-
value = String.Empty;
1905-
} catch (UnauthorizedAccessException ex) {
1906-
// value exists but we can't get it
1907-
// this shouldn't happen because this task requires elevation
1908-
LogExceptionToLauncher(ex);
1909-
value = String.Empty;
1910-
} catch {
1911-
// value doesn't exist
1912-
value = null;
1913-
}
1896+
registryStateElement.KeyName = GetRedirectedKeyValueName(
1897+
GetKeyValueNameFromKernelRegistryString(registryTraceData.KeyName + "\\" + registryStateElement.KeyName),
1898+
modificationsElement.RegistryStates.BinaryType
1899+
);
19141900

1915-
registryStateElement.Type = TYPE.VALUE;
1901+
try {
1902+
registryStateElement.ValueKind = GetValueKindInRegistryView(registryStateElement.KeyName, registryStateElement.ValueName, registryView);
1903+
} catch {
1904+
// value doesn't exist
1905+
registryStateElement.ValueKind = null;
1906+
}
19161907

1917-
if (value == null) {
1918-
try {
1919-
if (String.IsNullOrEmpty(registryStateElement.ValueName)
1920-
&& String.IsNullOrEmpty(TestKeyDeletedInRegistryView(registryStateElement.KeyName, registryView))) {
1921-
registryStateElement.Type = TYPE.KEY;
1922-
modificationsElement.RegistryStates.Set(registryStateElement);
1923-
}
1924-
} catch {
1925-
// fail silently
1926-
}
1908+
try {
1909+
value = ReplaceStartupPathEnvironmentVariable(
1910+
LengthenValue(
1911+
GetValueInRegistryView(
1912+
registryStateElement.KeyName,
1913+
registryStateElement.ValueName,
1914+
registryView
1915+
) as string,
19271916

1928-
if (registryStateElement.Type == TYPE.VALUE) {
1929-
modificationsElement.RegistryStates.Remove(registryStateElement.Name);
1930-
}
1931-
} else {
1932-
registryStateElement.Value = value;
1917+
fullPath,
1918+
pathNames
1919+
),
1920+
1921+
pathNames
1922+
);
1923+
} catch (SecurityException ex) {
1924+
// value exists but we can't get it
1925+
// this shouldn't happen because this task requires elevation
1926+
LogExceptionToLauncher(ex);
1927+
value = String.Empty;
1928+
} catch (UnauthorizedAccessException ex) {
1929+
// value exists but we can't get it
1930+
// this shouldn't happen because this task requires elevation
1931+
LogExceptionToLauncher(ex);
1932+
value = String.Empty;
1933+
} catch {
1934+
// value doesn't exist
1935+
value = null;
1936+
}
1937+
1938+
registryStateElement.Type = TYPE.VALUE;
1939+
1940+
if (value == null) {
1941+
try {
1942+
if (String.IsNullOrEmpty(registryStateElement.ValueName)
1943+
&& String.IsNullOrEmpty(TestKeyDeletedInRegistryView(registryStateElement.KeyName, registryView))) {
1944+
registryStateElement.Type = TYPE.KEY;
19331945
modificationsElement.RegistryStates.Set(registryStateElement);
19341946
}
1947+
} catch {
1948+
// fail silently
19351949
}
1936-
}
19371950

1938-
// and out of the queue
1939-
// (the Key is the TimeStamp)
1940-
//SetFlashpointSecurePlayerSection(TemplateName);
1941-
queuedModifications[safeKeyHandle].Clear();
1951+
if (registryStateElement.Type == TYPE.VALUE) {
1952+
modificationsElement.RegistryStates.Remove(registryStateElement.Name);
1953+
}
1954+
} else {
1955+
registryStateElement.Value = value;
1956+
modificationsElement.RegistryStates.Set(registryStateElement);
1957+
}
19421958
}
19431959
}
1960+
1961+
// and out of the queue
1962+
// (the Key is the TimeStamp)
1963+
//SetFlashpointSecurePlayerSection(TemplateName);
1964+
queuedModifications[safeKeyHandle].Clear();
19441965
}
19451966
}
19461967
}

FlashpointSecurePlayer/Shared.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,8 +1955,13 @@ private static Configuration ActiveEXEConfiguration {
19551955
public static int CurrentProcessId {
19561956
get {
19571957
if (currentProcessId == 0) {
1958-
using (Process currentProcess = Process.GetCurrentProcess()) {
1959-
currentProcessId = currentProcess.Id;
1958+
try {
1959+
using (Process currentProcess = Process.GetCurrentProcess()) {
1960+
currentProcessId = currentProcess.Id;
1961+
}
1962+
} catch (Exception ex) {
1963+
Exceptions.LogExceptionToLauncher(ex);
1964+
throw new InvalidOperationException("Failed to Get Current Process.");
19601965
}
19611966
}
19621967
return currentProcessId;

0 commit comments

Comments
 (0)