# CreateThread

CreateThread는 호출 프로세스의 가상 주소 공간 내에서 실행할 스레드를 생성합니다.

```cpp
HANDLE CreateThread(
  [in, optional]  LPSECURITY_ATTRIBUTES   lpThreadAttributes,
  [in]            SIZE_T                  dwStackSize,
  [in]            LPTHREAD_START_ROUTINE  lpStartAddress,
  [in, optional]  __drv_aliasesMem LPVOID lpParameter,
  [in]            DWORD                   dwCreationFlags,
  [out, optional] LPDWORD                 lpThreadId
);
```

<table><thead><tr><th width="173">인자</th><th width="322">설명</th><th>보편적인 값</th></tr></thead><tbody><tr><td>lpThreadAttributes</td><td>스레드의 보안 속성</td><td>NULL</td></tr><tr><td>dwStackSize</td><td>생성할 스레드의 스택 크기</td><td>0(PE 기본 스택 크기 사용)</td></tr><tr><td>lpStartAddress</td><td>실행할 함수의 이름/주소</td><td></td></tr><tr><td>lpParameter</td><td>함수에 전달할 매개변수</td><td></td></tr><tr><td>dwCreationFlags</td><td>생성 플래그</td><td>0 : 즉시 실행<br>CREATE_SUSPENDED : 일시 정지</td></tr><tr><td>lpThreadId</td><td>스레드 ID를 받을 포인터</td><td>변수 지정</td></tr></tbody></table>

## Example

```cpp
#include <iostream>
#include <windows.h>

struct ThreadArgs {
    int a;
    int b;
};

DWORD WINAPI ThreadProc(LPVOID lpParam)
{
    ThreadArgs* args = static_cast<ThreadArgs*>(lpParam); // 인자를 C++ 타입으로 복원
    std::cout << "a=" << args->a << ", b=" << args->b << std::endl; // 출력

    delete args; // 할당했던 힙 데이터 반환
    return 0;
}

int main()
{
    ThreadArgs* args = new ThreadArgs{ 10, 20 }; // 새로운 스레드 변수 생성
    HANDLE h = CreateThread(NULL, 0, ThreadProc, args, 0, NULL); // 스레드 생성
    WaitForSingleObject(h, INFINITE); // 스레드 작업 결과 확인을 위해 대기
    CloseHandle(h); // 핸들 정리
    return 0;
}
```

## References

{% embed url="<https://learn.microsoft.com/ko-kr/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread>" %}


---

# 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://www.pentestwiki.com/defense-evasion/windows-api/createthread.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.
