# GetProcAddress

GetProcAddress는 저장된 DLL에서 함수 또는 변수의 주소를 얻으며 동적 API 로딩에 사용됩니다.

```cpp
FARPROC GetProcAddress(
  [in] HMODULE hModule,
  [in] LPCSTR  lpProcName
);
```

| 인자         | 설명                     |
| ---------- | ---------------------- |
| hModule    | DLL 핸들 모듈              |
| lpProcName | 함수 이름 문자열 / Ordinal 번호 |

## Example

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

int main()
{
    // kernel32.dll의 함수들 동적 로딩
    HMODULE hKernel32 = GetModuleHandleA("kernel32.dll");
    if (!hKernel32) {
        std::cout << "Failed to get kernel32 handle" << std::endl;
        return 1;
    }

    // CreateFileA 함수 주소 획득
    FARPROC pCreateFile = GetProcAddress(hKernel32, "CreateFileA");

    // CreateFileA를 못 찾았을 때 예외처리
    if (!pCreateFile) {
        std::cout << "GetProcAddress failed" << std::endl;
        return 1;
    }
    std::cout << "CreateFileA address: 0x" << std::hex << pCreateFile << std::endl;

    // 함수 포인터로 캐스팅하여 사용
    typedef HANDLE(WINAPI* pCreateFileA)(
        LPCSTR,
        DWORD, 
        DWORD, 
        LPSECURITY_ATTRIBUTES,
        DWORD, 
        DWORD, 
        HANDLE
        );
    pCreateFileA CreateFileFunc = (pCreateFileA)pCreateFile;

    // 동적으로 로드한 함수 호출 -> CreateFileFunc은 CreateFileA의 함수 포인터
    HANDLE hFile = CreateFileFunc(
        "test.txt",
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL
    );
    if (hFile != INVALID_HANDLE_VALUE) {
        std::cout << "File created successfully using dynamic call" << std::endl;
        CloseHandle(hFile);
    }

    return 0;
}
```

## References

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

{% embed url="<https://aahc.tistory.com/17>" %}

{% embed url="<https://velog.io/@soonsoo3595/C-%EC%BA%90%EC%8A%A4%ED%8C%85%EC%97%90-%EB%8C%80%ED%95%B4%EC%84%9C>" %}


---

# 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/getprocaddress.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.
