> For the complete documentation index, see [llms.txt](https://www.pentestwiki.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pentestwiki.com/defense-evasion/windows-api/getprocaddress.md).

# 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>" %}
