DLL file cannot be located when application setup is generated using the in-built Publish feature of Visual Studio

Viewed 127

I have successfully built an application in C# to interface a highspeed measurement sensor. The application uses a third party DLL file used to interface the external hardware, supplied by the hardware vendor. A sample code snippet on how to interface the classes in the DLL file is shown below;

[DllImport("LJV7_IF.dll")]
    internal static extern int LJV7IF_Initialize();

    [DllImport("LJV7_IF.dll")]
    internal static extern int LJV7IF_Finalize();

    [DllImport("LJV7_IF.dll")]
    internal static extern uint LJV7IF_GetVersion();

    [DllImport("LJV7_IF.dll")]
    internal static extern int LJV7IF_UsbOpen(int lDeviceId);

    [DllImport("LJV7_IF.dll")]
    internal static extern int LJV7IF_EthernetOpen(int lDeviceId, ref LJV7IF_ETHERNET_CONFIG ethernetConfig);

Enphasis is laid on the use of "DLLImport" function. In order to access the DLL file, I created in folder called Library in the solution path an then added the command

copy $(SolutionDir)Library\LJV7_IF.dll $(TargetDir)LJV7_IF.dll

Build Event.png

such that the DLL can be copied to the bin folder after successful build. As a result the application currently works.

When the application is Published using the in-built Publish option in Visual Studio

Screenshot of Publish page.png

the generated setup installs but the DLL "LJV7_IF.dll" is not copied to the application output. When the installed application is run, the DLL file cannot be located. This is the error message I get,

Error output.png.

The Error message specifies that the application cannot locate the DLL file.

QUESTION: Is there an alternative way to reference the DLL file such that when the executable file is created, the DLL is correctly located?

3 Answers

The solution presented by @ Hans Passant works for this problem and has been solved. As a recap, Quote!!

"Just add it to the project with Project > Add Existing Item. Ensure its Build Action property is "Content" (it is by default) so the installer project knows it needs to be deployed as well. And if you set it Copy to Output Directory property to "Copy if newer" then you don't need the postbuild event anymore. Or do it manually by clicking the Application Files button." Curtesy of – Hans Passant

Can you try this instead of a manual copy?

Open the project properties, choose the configuration (step 1) you want to change (the one used when publishing).

Change the output path as desired (step 2).

project properties

For Visual Studio 2019 I found two approaches for my application to be able to access/use a third-party vendor's DLLs:

  1. Add the directory in which the third-party vendor's DLLs are installed to the Windows System Path environment variable (my preferred choice since it most closely replicates how my application is likely to be installed on the client's system)

  2. Copy the third-party DLLs to the directory in which my application was built as a Visual Studio 2019 Post-Build Event.

Related