views:

3095

answers:

6

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.

+5  A: 

Are the C and C++ DLLs in the same directory as the C# assembly that's executing?

You may have to change your project output settings so that the C# assembly and the other DLLs all end up in the same folder.

I've often used the Dependency Walker in cases like this; it's a sanity check that shows that all the dependencies can actually be found.

Once your app is running, you may also want to try out Process Monitor on the code you are running, to see which DLLs are being referenced, and where they are located.

Daniel LeCheminant
+1 for Dependency Walker; great tool
Randolpho
I hadn't thought of using good old depends.exe on .Net stuff (I'm an old-school Win32/MFC and other C++ guy). Good point. It confirmed the missing DLL.
Andy Dent
@Andy: Well, that's good ;] Depends.exe ftw!
Daniel LeCheminant
+1 once again, was just going to suggest Dependency Walker.
Daniel Earwicker
+1  A: 

This is an interesting dilemma. I've never heard of a problem loading native .DLLs from C++/CLI after a call into it from C# before. I can only assume the problem is as @Daniel L suggested, and that your .DLL simply isn't in a path the assembly loader can find.

If Daniel's suggestion doesn't work out, I suggest you try statically linking the native C code to the C++/CLI program, if you can. That would certainly solve the problem, as the .DLL would then be entirely absorbed into the C++/CLI .DLL.

Randolpho
Andy Dent
Ah, well. Good thing Daniel was right then, eh?
Randolpho
+1  A: 

Make sure the target system has the correct MS Visual C runtime, and that you are not accidentally building the C dll with a debug runtime.

leppie
+1 good point, the debug CRT stuff has gotten me before...
Daniel LeCheminant
+3  A: 

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.

Andy Dent
Yeah I know it's cheeky to mark your own answer as accepted but nobody else came close to the real answer for the tests, which took a good hour of digging. I'm still stunned I have to use such an old-fashioned technique to copy the DLL!
Andy Dent
Andy, you should still check into the point's I raised, I've got a feeling that your getting lucky with initialization order/timing, and may become un-lucky in the future w/o a firm grip on this one.
RandomNickName42
A: 

The reason why this happens is because you either are loading DLLMAIN from managed code, before the CRT has an opportunity to be initialized. You may not have any managed code, be executed DIRECTLY or INDERECTLY from an effect of DllMain notifications. (See: Expert C++/CLI: .Net for Visual C++ Programmers, chapter 11++).

Or you have no native entrypoint defined wahtsoever, yet you have linked to MSVCRT. The CLR is automatically initialized for you with /clr, this detail causes a lot of confusion and must be taken into account. A mixed mode DLL actually delay loads the CLR through the use of hot-patching all of the managed entry point vtables in your classes.

A number of class initialization issues surround this topic, loader lock and delay loading CLR are a bit trickey sometimes. Try to declare global's static and do not use #pragma managed/unmanaged, isolate your code with /clr per-file.

If you can not isolate your code from the managed code, and are having trouble, (after taking some of these steps), you can also look towards hosting the CLR yourself and perhaps going through the effort of creating a domain manager, that would ensure your fully "in-the-loop" of runtime events and bootstrapping.

This is exactally why, it has nothting todo with your search path, or initialization. Unfortunately the Fusion log viewer does not help that much (which is the usual place to look for .NET CLR assembly binding issues not dependency walker).

Linking statically has nothing todo with this either. You can NOT statically link a C++/CLI application which is mixed mode.

  1. Place your DLLMAIN function into a file by itself.
  2. Ensure that this file does NOT have /CLR set in the build options (file build options)
  3. Make sure your linking with /MD or /MDd, and all your dependencies which you LINK use the exact same CRT.
  4. Evaluate your linker's settings for /DEFAULTLIB and /INCLUDE to identify any possiable reference issues, you can declare a prototype in your code and use /INCLUDE to override default library link resolution.

Good luck, also check that book it's very good.

RandomNickName42
thanks for raising these issues - they have been taken seriously and added to our tasks for reviewing the code
Andy Dent
A: 

Hi All. Had the same problem switching to 64-bit Vista. Our application was calling Win32 DLLs which was confusing the target build for the application. To resolve it we did the following:

  1. Go to project properties;
  2. Select Build tab;
  3. Change 'Platform target:' option to x86;
  4. Rebuild the application.

When I re-ran the application it worked.