开发者

Specifying a DLL reference

I'm having trouble setting the path to a DLL that is not in the same directory as the executable.

I have a reference to dllA.dll. At present, everything is just copied into the same directory and all is well; however, I need to move the executable to another directory while still referencing the DLL in the original directory.

So, it's setup like:

C:\Original\Dir

program.exe
dllA.dll
dllB.dll
dllC.dll

But I need to have it setup like:

C:\New\Dir

program.exe
dllB.dll
dllC.dl

Such that it is still able to reference dllA.dll in C:\Original\dir

I tried the following, but to no avail:

  • Set the "Copy Local" value to false for dllA.dll because I want it to be referenced in its original location.
  • Under "Tools > Options > Projects and Solutions > VC++ Directories" I have added the path to "C:\Original\Dir"
  • Added "C:\Original\Dir" to both the PATH and LIB environment variables

At runtime, it informs me that it cannot locate dllA.dll Maybe the above steps I took only matter at compile time?

I was able to find this C# : Specifying a location for Dll reference

But I was thinking that my above method should've worke开发者_运维技巧d.

Any ideas?


Your compile-time settings won't affect the run-time path. Try adding C:\Original\dir to the system-wide path, and you should see that it picks up the DLL correctly. If so, then your solutions appear to be: 1) modify the system path permanently. May or may not be feasible. 2) alter the environment path at run-time. 3) use relative paths when referring to the DLL. 4) record the path to the DLL at installation time, perhaps in the registry, so that your exe can load it explicitly.


While I may still foolishly believe this can be accomplished through specifying the proper path variables, I was able to overcome this issue by adding some entries to my app.config

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="dllA" publicKeyToken="blah" culture="neutral" />
    <codeBase version="blah" href="file:///C:/Original/Dir/dllA.dll" />
  </dependentAssembly>
</runtime>


Working from the above answer (Thanks Karl), it wasn't clear to me about the syntax for href, especially for relative paths:

 <dependentAssembly>
    <assemblyIdentity name="SVDInterface"  culture="neutral" />
    <codeBase version="1.0.3114.29282" href="./System/SVD/SVDInterface.dll" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Math Functions"  culture="neutral" />
    <codeBase version="1" href="./System/SVD/Math Functions.dll" />
  </dependentAssembly>

The version number for the 1st dll I got from the references tab. Having looked into lots of ways of specifying where a dll is, this was by far the easiest way - still took me all day to get it to work fully. The last nugget of information is that the App.config entries above get written out to yourappname.exe.config which you must copy with your app to its final destination otherwise it will never find your dlls.

Hopefully of use to someone.

Kristian

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜