简体   繁体   English

DllImport生成System.DllNotFoundException

[英]DllImport generates System.DllNotFoundException

I'm having some difficulties while trying to consume an unmanaged-code dll from my application (written in C# framework 4.0). 我在尝试从我的应用程序中使用非托管代码dll时遇到了一些困难(用C#framework 4.0编写)。 I'm using the dll import as follows 我正在使用dll导入如下

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

The weird thing is that when called from my development environment (Windows XP) it works just fine, but when on the production server (Windows7) it generates the following exception: 奇怪的是,从我的开发环境(Windows XP)调用时,它工作得很好,但是当在生产服务器(Windows7)上时,它会生成以下异常:

System.DllNotFoundException : Unable to load DLL ' C:\\MGW_SDK.dll' : The specified module could not be found. System.DllNotFoundException :无法加载' C:\\MGW_SDK.dll' :找不到指定的模块。 (Exception from HRESULT: 0x8007007E) at Comtpaq.AdminPack.SDKWrapper.fInicializaSDK() at Comtpaq.AdminPack.AdminPaqRepository.InitializeSDK() in C:\\Work\\AgroIn\\AdminPaqRepository.cs :line 30 于:(0x8007007E从HRESULT异常) Comtpaq.AdminPack.SDKWrapper.fInicializaSDK()Comtpaq.AdminPack.AdminPaqRepository.InitializeSDK()C:\\Work\\AgroIn\\AdminPaqRepository.cs :线30

I already ran Dependency Walker , which shows the following dependency as conflicted: C:\\windows\\system32\\SHLWAPI.DLL 我已经运行了Dependency Walker ,它将以下依赖关系显示为冲突: C:\\windows\\system32\\SHLWAPI.DLL

I already tried copying that dll to my deployment directory 我已经尝试将该dll复制到我的部署目录

The MGW_SDK.dll is on c:\\ (as the path says) MGW_SDK.dllc:\\ (如路径所示)

I already installed C Runtime Library 我已经安装了C运行时库

I also noticed some interesting stuff: on the exception, the development path is showing C:\\Work\\AgroIn\\AdminPaqRepository.cs:line 30 我还注意到一些有趣的东西:在例外情况下,开发路径显示C:\\Work\\AgroIn\\AdminPaqRepository.cs:line 30

What else could be the problem here? 还有什么可能是这里的问题?

Edit 编辑

I built a small console application with just one of the extern / dllimport methods that I use from MGW_SDK.dll and ran it with no problem (no DllNotFoundException whatsoever), so this seems to be related to the application that I'm building (a WCF Service). 我使用MGW_SDK.dll一个extern / dllimport方法构建了一个小型控制台应用程序并运行它没有问题(没有任何DllNotFoundException ),所以这似乎与我正在构建的应用程序有关(a WCF服务)。 This service is hosted on the WAS, but I'm still unable to find the issue. 此服务托管在WAS上,但我仍然无法找到问题。

Well, this was a very tricky problem. 嗯,这是一个非常棘手的问题。 It was not related to dll dependencies and not related to the dll location either. 它与dll依赖关系无关,也与dll位置无关。 It was not about how I was calling the DllImport . 这不是关于我如何调用DllImport It was a permissions problem. 这是一个权限问题。

Basically this issue is solved by taking out and then adding back the permissions for execute on the folder where the dlls are. 基本上这个问题是通过在dll所在的文件夹中取出然后再添加执行权限来解决的。 See this article (Wayback machine). 看到这篇文章 (Wayback机器)。

Solution


Oracle 9.2 Client software requires that you give the Authenticated User privilege to the Oracle Home by following these steps: Oracle 9.2客户端软件要求您通过以下步骤为Oracle Home提供Authenticated User特权:

  1. Log on to Windows as a user with Administrator privileges. 以具有管理员权限的用户身份登录Windows。

  2. Launch Windows Explorer from the Start Menu and and navigate to the ORACLE_HOME folder. 从“开始”菜单启动Windows资源管理器,然后导航到ORACLE_HOME文件夹。 This is typically the "Ora92" folder under the "Oracle" folder (ie D:\\Oracle\\Ora92 ). 这通常是“Oracle”文件夹下的“Ora92”文件夹(即D:\\Oracle\\Ora92 )。

  3. Right-click on the ORACLE_HOME folder and choose the "Properties" option from the drop down list. 右键单击ORACLE_HOME文件夹,然后从下拉列表中选择“属性”选项。 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"). 单击“名称”列表中的“经过身份验证的用户”项(在Windows XP上,“名称”列表称为“组或用户名”)。

  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"). 取消选中“允许”列下“权限”列表中的“读取和执行”框(在Windows XP上,“权限”列表称为“经过身份验证的用户的权限”)。

  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. 重新执行应用程序,它现在应该工作。

The root of C is one of the places UAC will not let you write. C的根是UAC不会让你写的地方之一。 Sometimes unmanifested apps that ask to read from a protected area end up reading from the virtualized equivalent - and your DLL will not be there. 有时,要求从保护区未列于载货舱单的应用程序最终从虚拟化等同阅读-和你的DLL不会在那里。 Try moving the DLL to a different folder (not the root of C, not anything under Program Files) and see if that solves it. 尝试将DLL移动到另一个文件夹(不是C的根目录,而不是程序文件下的任何内容),看看是否能解决它。 If it does, you can leave it there or (better) put a manifest on your app to prevent virtualization. 如果是这样,您可以将其留在那里或(更好)在您的应用上放置清单以防止虚拟化。

Have you tried using Assembly Binding Log Viewer ? 您是否尝试过使用程序集绑定日志查看器

装配绑定日志查看器

Don't know if this will work for sure but have you tried registering MGW_SDK.DLL using regsrv32 or other installation package on Win7 box? 不知道这是否可行,但您是否尝试使用regsrv32或其他安装包在Win7盒上注册MGW_SDK.DLL?

Also try to find out if the dependencies of MGW_SDK.DLL are also available on Win7 machine (either registered on system or in your application folder will work) 还试着找出MGW_SDK.DLL的依赖关系是否也可以在Win7机器上使用(在系统上或在你的应用程序文件夹中注册都可以)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM