#include <stdlib.h>
#include <libloaderapi.h>

LPCWSTR GetThisDllPath()
{
    static wchar_t s_wszMyPath[_MAX_PATH] = { 0, };

    if (!s_wszThisPath[0])
    {
        DWORD dwFlags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
                      | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
        HMODULE hModule;
    
        if (!::GetModuleHandleExW(dwFlags, (LPCWSTR)&GetThisDllPath, &hModule))
        {
            int rv = ::GetLastError();
            fprintf(stderr, "GetModuleHandleEx failed, error = %d\r\n", rv);
            return nullptr;
        }
        if (!::GetModuleFileNameW(hModule, s_wszMyPath, _MAX_PATH))
        {
            int rv = ::GetLastError();
            fprintf(stderr, "GetModuleFileName failed, error = %d\r\n", rv);
            return nullptr;
        }
    }
    
    return s_wszThisPath;
}

#include "pch.h"

wchar_t s_wszMyPath[_MAX_PATH];

BOOL APIENTRY DllMain(
    HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        ::GetModuleFileNameW(hModule, s_wszMyPath, _MAX_PATH);
        break;
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

HMOUDLE hModule = &__ImageBase;
wchar_t wszMyPath[_MAX_PATH];

::GetModuleFileNameW(hModule, wszMyPath, _MAX_PATH);

 

'C, C++' 카테고리의 다른 글

xlnt - XLSX 파일 다루기  (0) 2022.12.22
To install the MinGW-w64 toolchain  (0) 2022.10.28
문자열 구분자로 분리  (0) 2021.10.20
VSCode + vcpkg  (0) 2021.10.19
ticktock  (0) 2021.08.15

+ Recent posts