Locked Reading input file in a dll

  • Monday, April 23, 2012 9:45 PM
     
     
    I am reading an input file from the working directory in my code. I built a dll from the code and wrote a wrapper around it. Where should i place the input file so that its read in the code?

All Replies

  • Monday, April 23, 2012 10:37 PM
     
     Answered

    The working directory that the DLL uses is the same as the application. So it should be fine in the same location as where you place it when the executable finds it.

    Just remember though, it is very easy to override the working directory from the outside. Visual Studio does it all the time when using the debugger. Normally when an application starts, the working directory is set to the same directory that the executable is in. When the Visual Studio debugger starts, there is an option to set the working directory and this is set to $(ProjectDir) by default. Even if you run an executable from Explorer, you can create a shortcut to it and override the working directory. You can use a stub executable to run it as well since CreateProcess has a parameter to set the working directory, same with ShellExecute and ShellExecuteEx. It is also pretty easy to override the working directory of an application by just injecting a DLL too.

    If you are worried about the location of your file then you shouldn't use a relative path to the file, you need to provide an absolute path, that is the only way you can guarantee that your file will be found. You can get the directory of your executable or even your DLL easily by calling GetModuleFileName with NULL for the HMODULE parameter to get the full executable path, or passing in the HMODULE you got for from your DllMain for the DLL path. You can then strip off the executable name from the end and use that path as a base for your file opening.

    Well, this is possibly a bit advanced right now, but it is a good idea to be at least aware of the problems of the working directory.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • Tuesday, April 24, 2012 12:03 PM
     
     Answered
    I am reading an input file from the working directory in my code. I built a dll from the code and wrote a wrapper around it. Where should i place the input file so that its read in the code?
    What I always do is call GetModuleFileName(NULL, ...) and strip the file name to get the directory of the module, and then use SetCurrentDirectory() to make sure the working directory is the same as that of the module. Now you can use relative path (simplest is just to put the file in the same directory as the module itself).
     

    David Wilkinson | Visual C++ MVP