11using System ;
22using System . Collections . Generic ;
33using System . Text ;
4-
5- using System . Runtime . InteropServices ;
64using System . Threading ;
75using 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 ( ) + "\n Program.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 ( ) + "\n Program.processHandle: " + ( Program . processHandle == IntPtr . Zero ) , "Get proc address failure." ) ;
94- throw new System . ComponentModel . Win32Exception ( lastError ) ;
86+ string additionalErrorText = "\n " + "hDLLInjectorHandle: " + hDLLInjectorHandle . ToString ( ) + "\n Program.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 ( ) + "\n Program.processHandle: " + ( Program . processHandle == IntPtr . Zero ) , "Virtual alloc failure." ) ;
117- throw new System . ComponentModel . Win32Exception ( lastError ) ;
108+ string additionalErrorText = "\n " + "hProc: " + hProc . ToString ( ) + "\n Program.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 ) ;
0 commit comments