Skip to content

Commit 90a5149

Browse files
committed
Spore ModAPI Launcher: cleanup ThrowWin32Exception() and use it more
1 parent f79d105 commit 90a5149

File tree

2 files changed

+17
-64
lines changed

2 files changed

+17
-64
lines changed

Spore ModAPI Launcher/Injector.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4-
5-
using System.Runtime.InteropServices;
64
using System.Threading;
75
using ModAPI.Common;
86

@@ -29,9 +27,7 @@ public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPat
2927
IntPtr objPtr = NativeMethods.VirtualAllocEx(hProc, IntPtr.Zero, (uint)dllPath.Length + 1, NativeTypes.AllocationType.Commit, NativeTypes.MemoryProtection.ReadWrite);
3028
if (objPtr == IntPtr.Zero)
3129
{
32-
int lastError = Marshal.GetLastWin32Error();
33-
System.Windows.Forms.MessageBox.Show("Error: " + lastError.ToString() + "\n" + "hProc: " + hProc.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero), "Virtual alloc failure.");
34-
throw new System.ComponentModel.Win32Exception(lastError);
30+
Program.ThrowWin32Exception("Virtual alloc failure.");
3531
}
3632

3733
//Write the path to the DLL file in the location just created
@@ -45,7 +41,6 @@ public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPat
4541
bool writeProcessMemoryOutput = NativeMethods.WriteProcessMemory(hProc, objPtr, bytes, (uint)bytes.Length, out numBytesWritten);
4642
if (!writeProcessMemoryOutput || numBytesWritten.ToUInt32() != bytes.Length)
4743
{
48-
/*throw new InjectException("Write process memory failed.");*/
4944
Program.ThrowWin32Exception("Write process memory failed.");
5045
}
5146

@@ -70,9 +65,8 @@ public static IntPtr InjectDLL(NativeTypes.PROCESS_INFORMATION pi, string dllPat
7065
}
7166
else
7267
{
73-
int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
74-
System.Windows.Forms.MessageBox.Show("Error: " + lastError.ToString(), "Create remote thread failed.");
75-
throw new System.ComponentModel.Win32Exception(lastError);
68+
Program.ThrowWin32Exception("Create remote thread failed.");
69+
return IntPtr.Zero; // silence a warning
7670
}
7771

7872
NativeMethods.VirtualFreeEx(hProc, objPtr, (uint)0, NativeTypes.AllocationType.Release);
@@ -89,9 +83,8 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
8983

9084
if (SetInjectDataPtr == IntPtr.Zero)
9185
{
92-
int lastError = Marshal.GetLastWin32Error();
93-
System.Windows.Forms.MessageBox.Show("Error: " + lastError.ToString() + "\n" + "hDLLInjectorHandle: " + hDLLInjectorHandle.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero), "Get proc address failure.");
94-
throw new System.ComponentModel.Win32Exception(lastError);
86+
string additionalErrorText = "\n" + "hDLLInjectorHandle: " + hDLLInjectorHandle.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero);
87+
Program.ThrowWin32Exception("Get proc address failure.", additionalErrorText);
9588
}
9689

9790
SetInjectDataPtr = new IntPtr(hDLLInjectorHandle.ToInt64() + (SetInjectDataPtr.ToInt64() - hLocalDLLInjectorHandle.ToInt64()));
@@ -112,9 +105,8 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
112105
IntPtr objPtr = NativeMethods.VirtualAllocEx(hProc, IntPtr.Zero, (uint)total_alloc_size, NativeTypes.AllocationType.Commit, NativeTypes.MemoryProtection.ReadWrite);
113106
if (objPtr == IntPtr.Zero)
114107
{
115-
int lastError = Marshal.GetLastWin32Error();
116-
System.Windows.Forms.MessageBox.Show("Error: " + lastError.ToString() + "\n" + "hProc: " + hProc.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero), "Virtual alloc failure.");
117-
throw new System.ComponentModel.Win32Exception(lastError);
108+
string additionalErrorText = "\n" + "hProc: " + hProc.ToString() + "\nProgram.processHandle: " + (Program.processHandle == IntPtr.Zero);
109+
Program.ThrowWin32Exception("Virtual alloc failure.", additionalErrorText);
118110
}
119111

120112
//write injection data
@@ -148,7 +140,6 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
148140
{
149141
if (NativeMethods.WaitForSingleObject(hRemoteThread, MAXWAIT) == WAIT_TIMEOUT)
150142
{
151-
//throw new InjectException("Wait for single object failed.");
152143
Program.ThrowWin32Exception("Wait for single object failed. This usually occurs if something has become stuck during injection, or if another error was left open for too long.");
153144
}
154145
while (NativeMethods.GetExitCodeThread(hRemoteThread, out var thread_result))
@@ -160,9 +151,7 @@ public static void SetInjectionData(NativeTypes.PROCESS_INFORMATION pi, IntPtr h
160151
}
161152
else
162153
{
163-
int lastError = Marshal.GetLastWin32Error();
164-
System.Windows.Forms.MessageBox.Show("Error: " + lastError, "Create remote thread failed.");
165-
throw new System.ComponentModel.Win32Exception(lastError);
154+
Program.ThrowWin32Exception("Create remote thread failed.");
166155
}
167156

168157
NativeMethods.VirtualFreeEx(hProc, objPtr, (uint)0, NativeTypes.AllocationType.Release);

Spore ModAPI Launcher/Program.cs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
using System.IO.Compression;
1111
using System.Linq;
1212
using System.Reflection;
13+
using System.Runtime.InteropServices;
1314
using System.Text;
1415
using System.Threading;
1516
using System.Windows.Forms;
16-
using System.Xml;
1717

1818
namespace SporeModAPI_Launcher
1919
{
@@ -162,11 +162,6 @@ void Execute()
162162
}
163163

164164
InjectSporeProcess(dllEnding);
165-
166-
int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
167-
168-
if (lastError != 0 && lastError != 18)
169-
ThrowWin32Exception("Something went wrong", lastError);
170165
}
171166
catch (Exception ex)
172167
{
@@ -177,7 +172,7 @@ void Execute()
177172
public void ShowError(Exception ex)
178173
{
179174
string versionInfo = "Launcher Kit version: " + UpdateManager.CurrentVersion + "\nModAPI DLLs version: " + UpdateManager.CurrentDllsBuild + "\nLauncher Kit path: " + Assembly.GetEntryAssembly().Location;
180-
if(this.ExecutablePath != null && File.Exists(this.ExecutablePath))
175+
if (this.ExecutablePath != null && File.Exists(this.ExecutablePath))
181176
{
182177
versionInfo += "\n\nSpore version: " + FileVersionInfo.GetVersionInfo(this.ExecutablePath).FileVersion + " - " + this.ExecutableType + "\nSpore path: " + this.ExecutablePath;
183178
}
@@ -186,8 +181,8 @@ public void ShowError(Exception ex)
186181
{
187182
var exc = ex as System.ComponentModel.Win32Exception;
188183
MessageBox.Show("ErrorCode: " + exc.ErrorCode + "\n" +
189-
"NativeErrorCode: " + exc.NativeErrorCode + "\n" +
190-
"HResult: " + exc.HResult + "\n", "Additional Win32Exception Error Info");
184+
"NativeErrorCode: " + exc.NativeErrorCode + "\n" +
185+
"HResult: " + exc.HResult + "\n", "Additional Win32Exception Error Info");
191186

192187
if (exc.InnerException != null)
193188
{
@@ -300,18 +295,14 @@ void CreateSporeProcess()
300295
if (!NativeMethods.CreateProcess(null, "\"" + this.ExecutablePath + "\" " + sb,
301296
IntPtr.Zero, IntPtr.Zero, false, NativeTypes.ProcessCreationFlags.CREATE_SUSPENDED, IntPtr.Zero, this.SporebinPath, ref this.StartupInfo, out this.ProcessInfo))
302297
{
303-
//throw new InjectException(Strings.ProcessNotStarted);
304-
int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
305-
System.Windows.Forms.MessageBox.Show("Error: " + lastError, Strings.ProcessNotStarted);
306-
throw new System.ComponentModel.Win32Exception(lastError);
298+
ThrowWin32Exception(Strings.ProcessNotStarted);
307299
}
308300
}
309301

310302
void ResumeSporeProcess()
311303
{
312304
if (NativeMethods.ResumeThread(this.ProcessInfo.hThread) != 1)
313305
{
314-
/*throw new InjectException(Strings.ProcessNotResumed);*/
315306
ThrowWin32Exception(Strings.ProcessNotResumed);
316307
}
317308
}
@@ -394,38 +385,11 @@ static bool ExtractFixFiles(MemoryStream zipStream, string outputPath)
394385
}
395386
}
396387

397-
public static void ThrowWin32Exception(string info)
398-
{
399-
ThrowWin32Exception(info, System.Runtime.InteropServices.Marshal.GetLastWin32Error());
400-
}
401-
402-
public static void ThrowWin32Exception(string info, int error)
403-
{
404-
if ((error != 0) && (error != 18))
405-
{
406-
System.Windows.Forms.MessageBox.Show("Error: " + error, info);
407-
throw new System.ComponentModel.Win32Exception(error);
408-
}
409-
}
410-
411-
private static List<string> GetModFiles(XmlNode componentNode)
388+
public static void ThrowWin32Exception(string title, string additionalErrorText = "")
412389
{
413-
var modFiles = new List<string>();
414-
foreach (XmlNode child in componentNode.ChildNodes)
415-
{
416-
switch (child.Name.ToLower())
417-
{
418-
case "component":
419-
case "prerequisite":
420-
case "compatfile":
421-
modFiles.AddRange(child.InnerText.Split('?'));
422-
break;
423-
case "componentgroup":
424-
modFiles.AddRange(GetModFiles(child));
425-
break;
426-
}
427-
}
428-
return modFiles;
390+
int error = Marshal.GetLastWin32Error();
391+
MessageBox.Show("Error: " + error + additionalErrorText, title);
392+
throw new Win32Exception(error);
429393
}
430394
}
431395
}

0 commit comments

Comments
 (0)