gpt4 book ai didi

c# - DllImport 生成 System.DllNotFoundException

转载 作者:太空狗 更新时间:2023-10-29 22:28:49 28 4
gpt4 key购买 nike

我在尝试从我的应用程序(用 C# 框架 4.0 编写)中使用非托管代码 dll 时遇到了一些困难。我正在使用 dll 导入如下

[DllImport(@"C:\MGW_SDK.dll", EntryPoint = "fInicializaSDK")]
public static extern int fInicializaSDK();

奇怪的是,当从我的开发环境 (Windows XP) 调用时,它工作得很好,但是当在生产服务器 (Windows7) 上时,它会生成以下异常:

System.DllNotFoundException: Unable to load DLL ' C:\MGW_SDK.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) at Comtpaq.AdminPack.SDKWrapper.fInicializaSDK() at Comtpaq.AdminPack.AdminPaqRepository.InitializeSDK() in C:\Work\AgroIn\AdminPaqRepository.cs:line 30

我已经运行了 Dependency Walker ,它显示以下依赖项存在冲突:C:\windows\system32\SHLWAPI.DLL

我已经尝试将那个 dll 复制到我的部署目录

MGW_SDK.dll 位于c:\(如路径所示)

我已经安装了 C 运行时库

我还注意到一些有趣的东西:在异常情况下,开发路径显示为 C:\Work\AgroIn\AdminPaqRepository.cs:line 30

这里还有什么问题?

编辑

我仅使用 MGW_SDK.dll 中使用的 extern/dllimport 方法之一构建了一个小型控制台应用程序,并使用没问题(没有任何 DllNotFoundException),所以这似乎与我正在构建的应用程序(WCF 服务)有关。此服务托管在 WAS 上,但我仍然找不到问题。

最佳答案

嗯,这是一个非常棘手的问题。它与 dll 依赖关系无关,也与 dll 位置无关。这与我如何调用 DllImport 无关。这是权限问题。

基本上这个问题是通过删除然后重新添加对dll所在文件夹的执行权限来解决的。参见 this article (回溯机)。

Solution


Oracle 9.2 Client software requires that you give the Authenticated User privilege to the Oracle Home by following these steps:

  1. Log on to Windows as a user with Administrator privileges.

  2. Launch Windows Explorer from the Start Menu and and navigate to the ORACLE_HOME folder. This is typically the "Ora92" folder under the "Oracle" folder (i.e. D:\Oracle\Ora92).

  3. Right-click on the ORACLE_HOME folder and choose the "Properties" option from the drop down list. A "Properties" window should appear.

  4. Click on the "Security" tab of the "Properties" window.

  5. Click on "Authenticated Users" item in the "Name" list (on Windows XP the "Name" list is called "Group or user names").

  6. Uncheck the "Read and Execute" box in the "Permissions" list under the "Allow" column (on Windows XP the "Permissions" list is called "Permissions for Authenticated Users").

  7. Re-check the "Read and Execute" box under the "Allow" column (this is the box you just unchecked).

  8. Click the "Advanced" button and in the "Permission Entries" list make sure you see the "Authenticated Users" listed there with:

      Permission = Read & Execute 
    Apply To = This folder, subfolders and files

    If this is NOT the case, edit that line and make sure the "Apply onto" drop-down box is set to "This folder, subfolders and files". This should already be set properly but it is important that you verify this.

  9. Click the "Ok" button until you close out all of the security properties windows. The cursor may present the hour glass for a few seconds as it applies the permissions you just changed to all subfolders and files.

  10. Reboot your computer to assure that these changes have taken effect (IMPORTANT).

Re-execute the application and it should now work.

关于c# - DllImport 生成 System.DllNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3818482/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com