简体   繁体   English

C#项目找不到C ++ .dll?

[英]c# project cannot find c++ .dll?

I have a working c++ dll that works in one c# project that I am calling via the interop service. 我有一个可工作的c ++ dll,可在通过互操作服务调用的一个c#项目中工作。 I have created another c# project and am trying to call the same .dll but keep getting a generic error message stating that the .dll cannot be found, both project are .net 2.0. 我创建了另一个c#项目,并尝试调用相同的.dll,但不断收到一条通用错误消息,指出无法找到.dll,两个项目均为.net 2.0。 What folder, and where do I specify in the project, should I put the .dll file in so that the project can find it? 我应将.dll文件放入哪个文件夹以及在项目中的哪里指定,以便项目可以找到它?

Think of it as a reminder for me... 认为这是对我的提醒...

In the previous project I did not have a reference to it, I just had it in the /bin folder and doing the same thing for this project does not work. 在上一个项目中,我没有引用它,只是将它放在/ bin文件夹中,对该项目执行相同的操作是行不通的。

Thanks R. 谢谢R。

确保有问题的DLL所依赖的所有DLL也都与使用DLL的exe位于同一目录中。

将C ++(本机)dll放入C#项目的输出目录中,bin \\ Debug或类似目录

Just as long as you put your working directory to the location of the DLL you should not have a problem. 只要将工作目录放在DLL的位置,就不会有问题。

Go to your project properties on the Debug tab and set the working directory to a directory that contains your DLL. 转到“调试”选项卡上的项目属性,然后将工作目录设置为包含DLL的目录。

我通常将DLL作为文件添加到项目中,并在较新版本中进行复制。

C# dll trying to access an unmanged dll by a relative path is governed by the same rules as any process loading a dll - that is the file has to be in the same directory as the executable or in one of paths defined in process' PATH variable. 尝试通过相对路径访问未管理的dll的C#dll与加载dll的任何进程都受相同的规则支配-也就是说,该文件必须与可执行文件位于同一目录中,或者位于进程的PATH变量中定义的路径之一中。

In order to have your Visual Ssolution do that automatically for you there are number of things you can do: 为了使您的Visual Ssolution自动为您执行此操作,您可以执行许多操作:

  1. Add the unmanaged project as C# project dependency. 将非托管项目添加为C#项目依赖项。 This will copy the DLL into the path of the C# executable. 这会将DLL复制到C#可执行文件的路径中。

  2. Change the output path of C++ project to that one of c# executable. 将C ++项目的输出路径更改为c#可执行文件之一。

  3. Build the C++ project and add the dll as a link to the C# project. 生成C ++项目,并将dll作为链接添加到C#项目。 Set in its properties to copy to the output directory. 设置其属性以复制到输出目录。

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

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