Cunning Linguist
Cunning Linguist

Reputation: 1

C++ to C#, C# to C++ with external DLLs, DLL not found exception

Background:

  1. C++ (without libraries/DLL(3rd party) and C++ codes calling them) as Dll called by C# is working.
  2. C++ (with libraries/Dlls(3rd party) and C++ codes using them) as Dll called by C++ is working.
  3. C++ (with libraries/Dlls(3rd party) and C++ codes using them) as Dll called by C# is not working.

I encountered Dll not found exception for the 3rd case. I was just commenting/ uncommenting my codes for inclusion or exclusion of Dlls and libraries for my C++ Dll hence the Dll supposedly can be found

Summary of problem:

1st case has shown that my Dll path is correct and the dll can be found and the setup is proper such that I can use C++ dll through C#.

2nd case has shown that my Dll itself is having no error.

3rd case is contradicting 1st and 2nd cases.

Please enlighten me, a million thanks in advance!!

P.S.: I didn't include code because I am not sure if the problem is obvious (to you professionals). I can make edits to include minimal example code if it is not.

Update 1:

Referring to the suggested solution, I have tried putting every dependencies under 1 folder however it did not work. Most probably I have done it in the wrong way.

Following is the setup of my Dll (I don't have enough reputation to post image here hence this is the link).

http://postimg.org/image/v4cau3b2d/

Could you please advise on how to structure the "Dependency folder" so that C# can find it?

Update 2:

Found that the libs can be located however failed when instantiating one of the class (3rd Party),

class TESS_API TessBaseAPI {
 public:
  TessBaseAPI();// failed here, why?
}

Apparently we need to do this!

Status: Used an alternative (Wrapped tesseract by some generous and talented guy, i.e. the 3rd party class can be used directly in C#). Proceeded in life. Will try to solve this when there's time.

Upvotes: 0

Views: 1065

Answers (3)

Redwan
Redwan

Reputation: 758

You also can get DllNotFoundException when с++ dependency of your с++ dependency cannot be loaded. So in third case (if it was run on separate machine), you may have not installed some dependencies.

Upvotes: 0

gbjbaanb
gbjbaanb

Reputation: 52689

I can only guess at the issue. I would initially say that the problem is a dependent dll that is used by your 3rd party lib, one that is being supplied by your C++ calling program that is not present when called by your C# program.

eg. If your dll calls the 3rd party dll, and it in turn calls a dll such as the c runtime library, or some other dll, then this dependent dll might not be present in the environment built for C#. The "dll not found" exception may refer to the dependency, not your dll.

One way to check this is to copy all the dlls used in step 2 into the C# directory and run it again (or use Dependency Viewer, if you can find where Microsoft has hidden it in later versions of VS).

I think the most likely cause will be something remarkably trivial and obvious... you'll need to get someone else to just check over your runtime environment, chances are they'll point out a really stupid mistake immediately. (or does that only happen to me!)

Edit: quick addendum, are you running your C# program in "All CPU" mode on a 64 bit machine, and the 3rd party libs are 32-bit? (or some variation on this theme). That could cause the problem too.

Upvotes: 1

Jcl
Jcl

Reputation: 28272

My suspicions from what I take from your question is that, your own DLL which uses third-party dynamically linked libraries, is not finding those third-party DLLs.

If it is so, you'd need to check how is the DLL accesing those third-party DLLs... if it is by a call to LoadLibrary, then you need to check wether the third-party library is in a correct path where it can be located:

The second way (generally using no path at all but the filename) is the most common way to use LoadLibrary, if that's the case, then just as a test, I'd try running the process as administrator and copying the required libraries somewhere on the PATH environment variable (\Windows\System32\ might be a good choice just for testing - remember to delete them afterwards if they are not meant to go there).

If this works, then it's more likely a problem with copying all the required libraries to the executable folder.

Also, take notice that running the executable from within Visual Studio (either debugging via F5 or running via Ctrl+F5) doesn't make the executable folder the current working directory. If you have copied all the required DLLs to the bin output folder, try to run your executable directly from there using Explorer and see if that works.

Upvotes: 1

Related Questions