c++ - LoadLibrary fails in WinUI3 - Stack Overflow

I am trying to use the SDK in a WinUI 3 C++ application.When using the SDK, LoadLibrary inside the 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.

  • LoadLibraryA failed to load DLL. LoadLibraryA fails even with a full path.
  • the full path is specified, LoadLibraryExA loads the main DLL successfully.
  • Running the application as administrator does not resolve the problem.

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");

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.

  • LoadLibraryA failed to load DLL. LoadLibraryA fails even with a full path.
  • the full path is specified, LoadLibraryExA loads the main DLL successfully.
  • Running the application as administrator does not resolve the problem.

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");
Share Improve this question edited Mar 5 at 3:08 kinton asked Mar 3 at 4:04 kintonkinton 3151 silver badge7 bronze badges 15
  • 1 If it works with absolute path, you simply have to calculate that absolute path using relative path based on base executable path or data path. – Sergey A Kryukov Commented Mar 3 at 4:07
  • 1 1. Call GetLastError() and see what it returns. 2. LoadLibrary initializes the DLL and basically calls DllMain, which LoadLibraryEx does not without appropriate flags. Maybe you are trying to load a 32 bit DLL from a 64 bit process or the opposite and the code can't be executed for that reason. – QuentinC Commented Mar 3 at 5:02
  • 1 "LoadLibraryA fails even with a full path." Then the code that manipulates the environment is irrelevant. – n. m. could be an AI Commented Mar 3 at 5:40
  • 1 If you can reproduce the error with just your own code, please do so and amend the question with a minimal reproducible example. – n. m. could be an AI Commented Mar 3 at 6:36
  • 1 The error can be indirect (a dll looks for a dll which looks for a dll, etc.) Might want to call AddDllDirectory before anything else, and pass the directory where all SDK bins are, like C:\Program Files\IDS\ids_peak\generic_sdk\api\lib\x86_64 – Simon Mourier Commented Mar 3 at 7:05
 |  Show 10 more comments

1 Answer 1

Reset to default 2

I suggest you could refer to the thread: https://github/microsoft/WindowsAppSDK/discussions/5202

The reason is that the DLL search order is different for packaged apps. You could refer to the Doc: Standard search order for packaged apps

In winui3, the App Paths key isn't used when computing the DLL search path.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745110241a4611804.html

相关推荐

  • c++ - LoadLibrary fails in WinUI3 - Stack Overflow

    I am trying to use the SDK in a WinUI 3 C++ application.When using the SDK, LoadLibrary inside the SDK

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信