C#到C ++ / CLI到C DLL System.IO.FileNotFoundException [英] C# to C++/CLI to C DLL System.IO.FileNotFoundException

查看:667
本文介绍了C#到C ++ / CLI到C DLL System.IO.FileNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行调用C ++ / CLI的C#代码时会收到 System.IO.FileNotFoundException:找不到指定的模块组装,其又调用纯C DLL。



BackingStore是纯C。
CPPDemoViewModel是C ++ / CLI调用BackingStore它有一个对BackingStore的引用。



我尝试了最简单的情况 - 添加一个新的C#单元测试项目,只是试图创建一个在CPPDemoViewModel中定义的对象。我添加了一个从C#项目到CPPDemoViewModel的引用。



一个C ++ / CLI测试项目只需添加参考CPPDemoViewModel就可以了。



我使用Visual Studio 2008 SP1与.Net 3.5 SP1。我建立在Vista x64上,但一直小心,以确保我的平台目标设置为x86。



这感觉像一个愚蠢,明显我失踪,但它会更加愚蠢的浪费时间试图在私人解决它,所以我在这里尴尬自己!



这是一个测试项目移植一个巨大的



编辑
之后,我将保存在一个DLL中的遗留C代码检查目录,我可以确认BackingStore.dll没有被复制。



我有一个典型的多项目解决方案创建的标准唯一的项目文件夹。

 
WPFViewModelInCPP
BackingStore
CPPViewModel
CPPViewModelTestInCS
bin
Debug
Debug

更高级别的Debug似乎是C和C ++ / CLI项目使用的公用文件夹, >

WPFViewModelInCPP\Debug包含BackingStore.dll,CPPDemoViewModel.dll,CPPViewModelTest.dll及其相关的.ilk和.pdb文件



WPFViewModelInCPP\CPPViewModelTestInCS\bin\Debug包含CPPDemoViewModel和CPPViewModelTestInCS .dll和.pdb文件,但 BackingStore。



CPPDemoViewModel具有属性复制本地我假定是负责复制其DLL时,如果引用。我无法将C#项目的引用添加到纯C DLL - 它只是说无法添加对后备商店的引用。



我不知道我只有一个问题或两个。



我可以使用老式的复制构建步骤将BackingStore.dll复制到任何给定C#项目的目录,虽然我希望新的.net模型不需要。



DependencyWalker告诉我,丢失的文件是GPSVC.dll 已建议表示安全设置问题。

edit2
如果BackingStore.dll的手动副本与可执行文件相邻, GUI现在工作正常。 C#测试项目仍然有一些问题,我怀疑是由于测试项目的运行时环境,但我现在可以没有这个。

解决方案

除了更改输出设置之外,GUI的答案是添加了预构建步骤

  $(ProjectDir).. \Debug\BackingStore。* $(TargetDir)

测试项目是将缺少的DLL添加到testrunco​​nfig的部署选项卡。您可以通过直接编辑默认的 LocalTestRun.testrunco​​nfig (显示在解决方案项下的解决方案)或右键单击解决方案和添加一个新的测试运行配置,然后将显示在主测试



感谢您对这个问题的测试配置,导致我的答案。


I'm getting System.IO.FileNotFoundException: The specified module could not be found when running C# code that calls a C++/CLI assembly which in turn calls a pure C DLL. It happens as soon as an object is instantiated that calls the pure C DLL functions.

BackingStore is pure C. CPPDemoViewModel is C++/CLI calling BackingStore it has a reference to BackingStore.

I tried the simplest possible case - add a new C# unit test project that just tries to create an object defined in CPPDemoViewModel . I added a reference from the C# project to CPPDemoViewModel .

A C++/CLI test project works fine with just the added ref to CPPDemoViewModel so it's something about going between the languages.

I'm using Visual Studio 2008 SP1 with .Net 3.5 SP1. I'm building on Vista x64 but have been careful to make sure my Platform target is set to x86.

This feels like something stupid and obvious I'm missing but it would be even more stupid of me to waste time trying to solve it in private so I'm out here embarrassing myself!

This is a test for a project porting a huge amount of legacy C code which I'm keeping in a DLL with a ViewModel implemented in C++/CLI.

edit After checking directories, I can confirm that the BackingStore.dll has not been copied.

I have the standard unique project folders created with a typical multi-project solution.

WPFViewModelInCPP
  BackingStore
  CPPViewModel
  CPPViewModelTestInCS
    bin
      Debug
  Debug

The higher-level Debug appears to be a common folder used by the C and C++/CLI projects, to my surprise.

WPFViewModelInCPP\Debug contains BackingStore.dll, CPPDemoViewModel.dll, CPPViewModelTest.dll and their associated .ilk and .pdb files

WPFViewModelInCPP\CPPViewModelTestInCS\bin\Debug contains CPPDemoViewModel and CPPViewModelTestInCS .dll and .pdb files but not BackingStore. However, manually copying BackingStore into that directory did not fix the error.

CPPDemoViewModel has the property Copy Local set which I assume is responsible for copying its DLL when if is referenced. I can't add a reference from a C# project to a pure C DLL - it just says A Reference to Backing Store could not be added.

I'm not sure if I have just one problem or two.

I can use an old-fashioned copying build step to copy the BackingStore.dll into any given C# project's directories, although I'd hoped the new .net model didn't require that.

DependencyWalker is telling me that the missing file is GPSVC.dll which has been suggested indicates security setting issues. I suspect this is a red herring.

edit2 With a manual copy of BackingStore.dll to be adjacent to the executable, the GUI now works fine. The C# Test Project still has problems which I suspect is due to the runtime environment of a test project but I can live without that for now.

解决方案

The answer for the GUI, other than changing output settings, was the addition of a Pre-Build Step

copy $(ProjectDir)..\Debug\BackingStore.* $(TargetDir)

The answer for the Test projects was to add the missing DLL to the Deployment tab of the testrunconfig. You can either do so by directly editing the default LocalTestRun.testrunconfig (appears in Solution under Solution Items) or right-click the Solution and Add a new test run config, which will then appear under the main Test menu.

Thanks for the answers on this SO question on test configurations for leading me to the answer.

这篇关于C#到C ++ / CLI到C DLL System.IO.FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆