3

I recently wrote a small native c++ wrapper in C++/CLI. Its a simple program. I am able to compile it properly and able to add the dll refeernce in my C# project without any errors. I can also create the object from this CPP/CLI dll and access the functions at compile time without any errors.

Now When I run the program, The first function call which references the object from this dll throws an exception. The exception is as follows:

Could not load file or assembly 'ProtobufWrapper, Version=1.0.4381.26401, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

I am not sure what is wrong in my case.

4
  • Have you looked in the GAC on the host it is running on? Commented Dec 30, 2011 at 7:01
  • Does your C++/CLI project reference any other assemblies? Commented Dec 30, 2011 at 7:03
  • @M.Babcock: I think the error message would vary slightly, if the DLL was found but failed to load. Common reasons for load failure can include missing dependencies and wrong architecture (i.e. x86 DLL in an x64 process). But in this case, it probably isn't in the library search path. Commented Dec 30, 2011 at 7:09
  • Have you tried to add the reference from VS..? also check to see if you can GAC the dll.. what version of VS are you using.. if it's 2010 remember it has it's own separate GAC for 2.0 and 3.5 Commented Dec 30, 2011 at 7:19

1 Answer 1

2

Is the DLL file located in the same directory as the .Exe you're starting? Sometimes the file won't get copied and can't be found.

On the DLL reference click properties and check for build action. Set it to Copy.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for reply everyone. Realized that the DLL was not getting copied in the binary folder of main program. Reason is that the C# program where i am referring the C++/CLI library is just another library which is used in yet another C# main program. Forgot to add reference to my C++/CLI library in the main C# program. Working great now.
Is there any way to explicitly specify where to load the DLL? I want to avoid having to put them in the same directory. I tried to add the location of the C# dll to the PATH, but it did not help.
@Mark: Yes, handle the AppDomain.AssemblyResolve event. As a bonus, this lets you pick different C++/CLI mixed-mode DLLs depending on whether the process is 32-bit or 64-bit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.