kinton
kinton

Reputation: 193

It worked with MFC, but not with WinUI 3: LoadLibraryA fails in SDK

I am trying to use the SDK in a WinUI 3 C++ application. When using the SDK, LoadLibrary inside the SDK fails. During debugging, I examined the process and found that calling LoadLibraryExA as a test successfully loaded the DLL. However, I don't understand why LoadLibraryA fails. What could be the possible reasons for this failure? How can I ensure that the SDK can properly load its DLL? It worked well with MFC, but it doesn't seem to work well with WinUI 3.

I suspect that _wputenv_s may not be setting the environment variable correctly, and the search path is not being added. However, I am unsure how to resolve this issue.

Any help would be appreciated.

IDS_PEAK_GENERIC_SDK_PATH : C:\Program Files\IDS\ids_peak\generic_sdk

.cpp

    // LoadLibraryExA succeeds
HMODULE module2 = LoadLibraryExA(
    "C:\\Program Files\\IDS\\ids_peak\\generic_sdk\\api\\lib\\x86_64\\ids_peak.dll",
    NULL,
    LOAD_WITH_ALTERED_SEARCH_PATH
);

size_t sz = 0;
    if (_wgetenv_s(&sz, NULL, 0, L"IDS_PEAK_GENERIC_SDK_PATH") == 0 && sz > 0)
    {
        std::vector<wchar_t> env_ids_peak(sz);
        if (_wgetenv_s(&sz, env_ids_peak.data(), sz, L"IDS_PEAK_GENERIC_SDK_PATH") == 0)
        {
            if (_wgetenv_s(&sz, NULL, 0, L"PATH") == 0 && sz > 0)
            {
                std::vector<wchar_t> env_path(sz);
                if (_wgetenv_s(&sz, env_path.data(), sz, L"PATH") == 0)
                {
                    std::wstring ids_peak_path(env_ids_peak.data());
                    ids_peak_path.append(L"\\api\\lib\\x86_64");
                    std::wstring path_var(env_path.data());
                    path_var.append(L";").append(ids_peak_path);
                    _wputenv_s(L"PATH", path_var.c_str());
                }
            }
        }
    }
    
    // LoadLibraryA fails
    HMODULE module = ::LoadLibraryA("ids_peak.dll");

.hpp

#define PEAK_DYNAMIC_LOADING
#define PEAK_IPL_DYNAMIC_LOADING
#include "peak/peak.hpp"
#include "peak_ipl/peak_ipl.hpp"
#include "peak/converters/peak_buffer_converter_ipl.hpp"

Upvotes: 0

Views: 73

Answers (0)

Related Questions