> For the complete documentation index, see [llms.txt](https://malwaresourcecode.com/home/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://malwaresourcecode.com/home/shellcode-execution/createthreadandwaitforcompletion.md).

# CreateThreadAndWaitForCompletion

This function is necessary for the shellcode execution methods here. This function creates a thread for the shellcode to execute in.

```
DWORD CreateThreadAndWaitForCompletion(_In_ LPTHREAD_START_ROUTINE StartAddress, _In_ LPVOID Parameters, _In_ DWORD dwMilliseconds)
{
	HANDLE hThread = NULL;
	DWORD ExitCode = ERROR_SUCCESS;

	hThread = CreateThread(NULL, 0, StartAddress, Parameters, 0, NULL);
	if (hThread == NULL)
		return FALSE;

	WaitForSingleObject(hThread, dwMilliseconds);

	if (!GetExitCodeThread(hThread, &ExitCode))
		return -1;

	return ExitCode;
}
```
