@@ -137,6 +137,52 @@ namespace iosync
137137 }
138138
139139 #ifdef PLATFORM_WINDOWS
140+ static inline bool __winnt__startProcess (LPCTSTR applicationName, const string& commandLine=string(), DWORD flags=CREATE_NO_WINDOW)
141+ {
142+ // Local variable(s):
143+ STARTUPINFO si;
144+ PROCESS_INFORMATION pi;
145+
146+ // Set the size of the structures:
147+ ZeroMemory (&si, sizeof (si));
148+ ZeroMemory (&pi, sizeof (pi));
149+
150+ si.cb = sizeof (si);
151+
152+ CHAR cmd[MAX_PATH];
153+
154+ memcpy (cmd, commandLine.c_str (), min (commandLine.size (), MAX_PATH));
155+ cmd[commandLine.length ()] = ' \0 ' ;
156+
157+ // Start the specified program:
158+ if
159+ (
160+ CreateProcess
161+ (
162+ applicationName,
163+ (LPSTR)cmd,
164+ NULL ,
165+ NULL ,
166+ FALSE ,
167+ flags,
168+ NULL ,
169+ NULL ,
170+ &si,
171+ &pi
172+ ) == FALSE
173+ )
174+ {
175+ return false ;
176+ }
177+
178+ // Close the process and thread handles:
179+ CloseHandle (pi.hProcess );
180+ CloseHandle (pi.hThread );
181+
182+ // Return the default response.
183+ return true ;
184+ }
185+
140186 // This command will inject the library specified into the process with the PID specified by 'processID'.
141187 // The return-value of this command indicates if injection was successful.
142188 static inline bool __winnt__injectLibrary (string library, DWORD processID)
@@ -158,6 +204,7 @@ namespace iosync
158204 // cout << "Directory: " << buffer << endl;
159205
160206 SIZE_T strLength = (SIZE_T)strlen (buffer);
207+ // buffer[strLength] = '\0';
161208
162209 // Open the remote process with specific rights.
163210 remoteProc = OpenProcess (PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE , processID);
@@ -169,24 +216,24 @@ namespace iosync
169216 LoadLibraryAddr = (LPVOID)GetProcAddress (GetModuleHandleA (" kernel32.dll" ), " LoadLibraryA" );
170217
171218 // Allocate a buffer using the remote process.
172- remoteLibraryName = (LPVOID)VirtualAllocEx (remoteProc, NULL , strLength, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
219+ remoteLibraryName = (LPVOID)VirtualAllocEx (remoteProc, NULL , strLength, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); // strLength+1
173220
174221 // Write the 'library' string to the newly allocated portion of memory.
175- BOOL test = WriteProcessMemory (remoteProc, remoteLibraryName, buffer, strLength, NULL ); // library.c_str()
222+ BOOL test = WriteProcessMemory (remoteProc, remoteLibraryName, buffer, strLength, NULL ); // strLength+1
176223
177224 // Create a remote thread that will immediately load the library specified into the remote process.
178225 HANDLE h = CreateRemoteThread (remoteProc, NULL , NULL , (LPTHREAD_START_ROUTINE)LoadLibraryAddr, remoteLibraryName, NULL , NULL );
179226
180- // Close the handle to the remote process.
181- CloseHandle (remoteProc);
182-
183227 // Close our local handle to the remote thread.
184228 CloseHandle (h);
185229
230+ // Close the handle to the remote process.
231+ CloseHandle (remoteProc);
232+
186233 // Free the memory we allocated.
187234 VirtualFreeEx (remoteProc, remoteLibraryName, 0 , MEM_RELEASE | MEM_DECOMMIT);
188235
189- delete buffer;
236+ delete[] buffer;
190237
191238 // Return the default response.
192239 return true ;
0 commit comments