简体   繁体   English

在何处放置非托管DLL以用于导入C#程序?

[英]Where to place an unmanaged DLL for use importing into a C# program?

This must be a really stupid question, but I'm still very green when it comes to C#. 这肯定是一个非常愚蠢的问题,但是在C#方面我还是很环保。

Anyway, I've got a DLL and I'm importing it with a line like this: 无论如何,我有一个DLL,并使用如下代码行将其导入:

[DllImport(@"MyCoolDll")]

I've lifted this straight from the demo app provided by the vendor, but it keep complaining that it can't find the DLL. 我直接从供应商提供的演示应用程序中删除了此文件,但它一直抱怨找不到DLL。 The actual error (from Visual Studio 2010) is like this: 实际错误(来自Visual Studio 2010)是这样的:

Unable to load DLL 'MyCoolDll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

I've tried placing the compiled DLL in the bin/debug and bin/release folders. 我试过将已编译的DLL放在bin / debug和bin / release文件夹中。 I've even tried copying it to system32, but nothing seems to work. 我什至尝试将其复制到system32,但似乎没有任何效果。

Any ideas? 有任何想法吗?

Your DLL may have dependencies that also need to be loaded. 您的DLL可能具有依赖项,这些依赖项也需要加载。 Did you check for that? 你检查了吗?

I know you have to give the full file name. 我知道您必须提供完整的文件名。 So 所以

[DllImport(@"MyCoolDll.dll")]

It should work from the bin\\debug or bin\\release folders. 它应该在bin \\ debug或bin \\ release文件夹中工作。

Update 更新资料

This is where I learned how to import unmanaged dlls. 是我学习如何导入非托管dll的地方。 If it was a test app that is working correctly, check it's bin\\debug folder to see what is different from yours. 如果它是可以正常运行的测试应用程序,请检查它的bin \\ debug文件夹以查看与您的文件夹有何不同。 Possibly an extra dll being referenced? 可能引用了一个额外的dll? Also check all the references within the sample app to make sure you are not missing any. 还要检查示例应用程序中的所有引用,以确保您没有丢失任何引用。

The key for me was to look at the paths listed in the Visual Studio Build console output, to see where the binaries were being placed. 对我而言,关键是查看Visual Studio Build控制台输出中列出的路径,以查看二进制文件的放置位置。 Once I found that, I knew where to copy the unmanaged dll and it fixed the "Unable to load DLL" error. 一旦发现该错误,便知道在何处复制非托管dll,并修复了“无法加载DLL”错误。

As far as I know you have to provide extension: 据我所知,您必须提供扩展名:

[DllImport(@"MyCoolDll.dll")]

I usually keep these dlls locally with the program binaries (so in bin\\Debug for development) 我通常将这些dll与程序二进制文件一起保存在本地(因此在bin \\ Debug中进行开发)

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

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