# 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;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://malwaresourcecode.com/home/shellcode-execution/createthreadandwaitforcompletion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
