none
error - Unhandled Exception: System.IO.FileNotFoundException: File not found: commslib.dll RRS feed

  • Question

  • Hi,

    I have a got a sensor that I need to write a program for. The sensor manufacturer has provided the Active X controls that I can use for my project. 

    I first added a reference to the ocx file in visual studio project and I managed to run some command to get the version number, however, I need to go beyond that, I need to write code to point to the data folder and so on.


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using IMLtools;
    
    
    namespace ConsoleApp12
    {
        class Program
        {
    
            static void Main(string[] args)
            {
    
       
             var x = "";
                int check;
                
                string IMSfilespec = "c:\\windmill\\Gtt.IMS";
                var IMSname = "G&D.IMS";
                
    
                
                IMLtoolsv6 newsd = new IMLtoolsv6();
             
                newsd.IMLOpen();
                newsd.IMSLoadFile(IMSfilespec, IMSname);
                Console.WriteLine("Check this ");
                    
                  
               
               
              
    
    
    
            }
        }
    }
    

     This is the error

    Unhandled Exception: System.IO.FileNotFoundException: File not found: commslib.dll
       at IMLtools._IMLtoolsv6.getWindmillpath()
       at ConsoleApp12.Program.Main(String[] args) in C:\Users\gdmal\source\repos\ConsoleApp12\ConsoleApp12\Program.cs:line 29

    I tried specifying all the path and the filename as suggested by the document, yet I fail to get it right. The original manual is located here - https://www.windmill.co.uk/help/imltools.pdf

    My dlls are located here - C:\Windmill.

    Monday, December 2, 2019 8:42 AM

Answers

  • If your code has a dependency on a native DLL then the DLL has to be in a path that Windows will search for it otherwise it won't be loaded. Normally apps that have shared binaries add themselves to the system path. If that isn't an option then you'll need to put the DLLs somewhere where they will be found.

    The easiest solution (unless the app has a setup requirement or lots of dependent DLLs) is to copy the native DLL into your output directory. You can do this during the build process using a post build event.

    xcopy "pathtodll\commslib.dll" "$(TargetDir)" /Y /R

    To make the build repeatable so you can just fetch from your source repo (Git or whatever) then it is generally recommended to create a "binaries" or equivalent directory off your solution root. Then put the DLL(s) in there that you want to run against. You are responsible for manually keeping these binaries up to date. The binaries become part of your source code as well. The post build event then copies the DLL(s) from this directory to your output directory for your project. Since this happens at build time then when you run the code it'll be found in the local path.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by cally_kalpana Wednesday, December 4, 2019 1:33 AM
    Tuesday, December 3, 2019 3:39 AM
    Moderator

All replies

  • Have you verified this works with C# ?

    Did you add the DLL to the project references ?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, December 2, 2019 2:33 PM
    Moderator
  • "The sensor manufacturer has provided the Active X controls that I can use for my project. "

    In that case you aren't dealing with file paths. You should have added a reference to this component to your project by using References\COM then find the COM component that should be registered on the machine.

    However the error is related to not finding a DLL which would seem to indicate a bad ActiveX registration. I guess it could also be that the ActiveX control is registered for x86 and you're trying to run as x64 but that would likely generate a class registration error instead. 

    What is `commslib.dll`? Is this a Windows DLL or something provided by the control provider? For ActiveX Windows will locate binaries via the COM registration.


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, December 2, 2019 2:54 PM
    Moderator
  • Hi Karen

    I tried but I was getting this error - A reference to the commslib.dll could not be added.Please make sure that the file is accessible and that it is a valid assembly or COM component.

    Tuesday, December 3, 2019 1:17 AM
  • Hi Cooldad,

    It's not a windows dll but is provided by the control provider. Is there anyway that I can do a dllimport or something like that. I have seen some articles on it, but not sure how to go about doing it.

    Tuesday, December 3, 2019 1:30 AM
  • Hi,

    I actually think I solved it, I changed my build path to the folder where the dlls are located. Since the dlls are provided by the vendor ( are those dlls not recognized in the visual studio reference, called unmanaged code ). 

    What are my other options instead of changing my build path to the folder. I am sure there are risks with this. Please do advice.

    Tuesday, December 3, 2019 2:27 AM
  • If your code has a dependency on a native DLL then the DLL has to be in a path that Windows will search for it otherwise it won't be loaded. Normally apps that have shared binaries add themselves to the system path. If that isn't an option then you'll need to put the DLLs somewhere where they will be found.

    The easiest solution (unless the app has a setup requirement or lots of dependent DLLs) is to copy the native DLL into your output directory. You can do this during the build process using a post build event.

    xcopy "pathtodll\commslib.dll" "$(TargetDir)" /Y /R

    To make the build repeatable so you can just fetch from your source repo (Git or whatever) then it is generally recommended to create a "binaries" or equivalent directory off your solution root. Then put the DLL(s) in there that you want to run against. You are responsible for manually keeping these binaries up to date. The binaries become part of your source code as well. The post build event then copies the DLL(s) from this directory to your output directory for your project. Since this happens at build time then when you run the code it'll be found in the local path.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by cally_kalpana Wednesday, December 4, 2019 1:33 AM
    Tuesday, December 3, 2019 3:39 AM
    Moderator
  • Hi CooldadTx

    Thank you so much for the explanation. Well understood. 

    Wednesday, December 4, 2019 1:33 AM