Creating texture which is in project folder - c++

I'm having trouble creating a texture which is in my DLL project's directory.
I'm doing
D3DXCreateTextureFromFile(Device, "Sprites/ExpandBlack.png", &BlackTexture);
but that doesn't seem to work. However if I do the whole path like:
D3DXCreateTextureFromFile(Device, "C:\\Users\\Home\\Documents\Visual Studio 2017\\Projects\\NO\\NO\\Sprites\\ExpandBlack.png", &BlackTexture);
it does work.
I also tried doing ../Sprites/ExpandBlack.png, ..\\Sprites\\ExpandBlack.png etc.
Any kind of help is appreciated.
This is where Sprites is located and it has to be "compiled" with the dll.

../Sprites/ExpandBlack.png, ..\\Sprites\\ExpandBlack.png and Sprites/ExpandBlack.png all refer to the same relative path.
Relative paths append to the process's working directory. DLLs use the same working directory.
To get a DLLs path see Get DLL path at runtime

Relative paths are relative to the current directory of the process. If it works when using a full path but doesn't work when using a relative path then obviously your relative path is not leading to where it should be leading. Most likely because the current directory of the process that's supposed to access that file is not what it should be.
Since we're talking about a DLL here, I suppose the problem is that when running the program that's actually using the DLL, that program is launched from a different location than the one the DLL project file is in. Most likely, because the project is located in a different directory than the DLL project. Visual Studio will by default use the directory in which the project file is located as the working directory for the process. You can change the working directory that Visual Studio will use in the project properties under "Debugging". Most likely, you will want your sprites located relative to the application that's using them rather than some DLL that the application is using. If these sprites are actually tied to the library and required by the library to function, on the other hand, then you may want to consider embedding the files into the library, e.g., as resources, or at least place these files relative to the library and access them based on the location of the DLL and not the current directory of the process that's using the DLL. To get the path of your DLL, see the answer linked in user2176127's answer…

Related

SDL2.dll was not found

I'm trying to set up SDL2 in C++ Visual Studio but when I run the code(just some starter code I copied) it pops up with an error box box that talks about "SDL2.dll cannot be found" I tried switching to x64 but that was no help. I can see that the dll is right next to the lib files but it just won't work.
Your problem is the lib folder is not a place that your OS will search for dependent dlls by default. To fix this you would have to help your OS find the dll. There are several methods you can use to tell your OS where to look. One is adding an entry to your PATH environment variable that contains the full path to the folder containing the dll.
This site can help with setting the PATH: https://www.computerhope.com/issues/ch000549.htm
As second method is to put the dll in the same folder as the executable.
By default your OS probably is using the safe search option described here:
The directory from which the application loaded.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.***

C++ Moving DLL out of root directory into a subfolder, visual studio

I'm a beginner with building software using C++. In my project I link to DLLs and I keep them stored in the root folder. I do this because I want the project to be portable from one machine to another, and I also want the release builds to have dependence from installing things into system32 and what not.
The problem is all the DLLs in the root folder is messy, so I want to organize them into subfolders. But I can't do that because putting the DLL in a root subfolder instead of the root, you get errors. I think because the DLL is copied to the output at the wrong location, not where the exe is, but in a subfolder, just like the source structure. Am I right about that?
What is a solution that will allow me to have the project be still be copy-pastable/portable between machines?
Windows searches for DLLs in predefined locations (see Dynamic-Link Library Search Order). Subdirectories of the directory where the application resides are not part of the search order.
To implement your requirement, you will need to explicitly add the directories to the searched locations. This process consists of two steps:
Call AddDllDirectory for each directory you want searched, in addition to the default search locations on application startup.
By default, DLL imports are resolved prior to starting a process' primary thread. To allow your application to change the DLL search path, import resolution needs to be postponed. The easiest way to do this is by using the /DELAYLOAD (Delay Load Import) linker option (see Linker Support for Delay-Loaded DLLs for additional information).
While it is possible, to segregate DLLs into subdirectories, it is best to keep them all alongside the executable image.
If you put the DLLs into subfolders instead of keeping them in the same directory as the executable, you would either have to modify PATH environment variable in Windows to point to every subfolder, or use the DLLs in LoadLibrary+GetProcAddress way instead of linking them to the executable via import libs.

Change current working directory VS13?

As stated in this post the working directory when I debug my SDL program is relative to the .vcproj instead of the .exe (which it should be IMO)
So I'm wondering if there's anyway I can change this, so when i press F5 the path will be relative to the .exe and not .vcproj?
The current (relatively easy) workaround I'm using, is simply opening up the folder and starting program from there, but I would much rather prefer simply pressing F5.
The naive answer to your question is that you can set the Working Directory option in the Debugging configuration properties to $(TargetDir). The default setting is $(ProjectDir) and by default the project directory is not where the executable file is output. However, I do not recommend you take this option, as I explain below.
You are attempting to solve this the wrong way. Your fundamental problem is that you are assuming that the working directory is the same as directory which contains the executable file. There's no reason for that to be so, and you should not rely one it. You know that the files are in the same directory as the executable and so you should look there, rather than the working directory which is only sometimes, coincidentally, the same as the executable directory.
So, instead of relying on the invoker of the process setting up the working directory to your liking, make your program independent of the working directory. You state that you wish to work with files whose location you know relative to the executable directory. So there is the answer. Construct full paths to your files, using the directory which contains the executable as the base.
If you need to find out the location of the executable, call GetModuleFileName(), and strip off the file name. What you have left is the directory which contains your executable. Combine that with the relative path of your files and your code is now independent from the working directory.

How can I automatically load DLLs from a subdirectory?

In Visual Studio, you create a .dll project and it creates a .dll and .lib files. You link statically to the .lib, and if the .dll is in the same folder as the .exe, everything works.
I suspect that everything would also work if the .dll was in System32 or any other PATH folder (confirm or correct, please).
But here's the question: I want my exe to find the .dll in ./DLLS/ folder, that is, if my exe is in ....../MyApp/MyApp.exe then it should look for the .dll in ...../MyApp/DLLS/MyDll.dll. I DO NOT want to include the DLLS folder in path. Is there any way I can do this?
Please note that I do not wish to use LoadLibrary explicitly, so I can't specify the path there.
Thanks in advance for any help.
Here is the default sequence which Win32 applications go through when looking for a DLL:
http://msdn.microsoft.com/en-us/library/7d83bc18(VS.80).aspx
So according to this, another approach might be to call SetCurrentDirectory or SetDllDirectory. But in order for this to work you have to use the Delay Loaded Library functionality (you specify that in Project Settings in Visual Studio). Delay loaded library means that the DLL is loaded only at the moment when the program needs it, not automatically at the programs' startup.
You could use SetDllDirectory for this. The loader will use the additional directory you specify when loading libraries. There can only be one additional directory, however, so you need to make sure that there aren't other calls to this at a later point in your application, otherwise the directory you specify will be ignored.
If that API does not allow relative directories (I'm not sure), you could always call GetModuleFileName with a NULL first parameter to get the file name of the currently executing program. With a little string manipulation, you can get the absolute path to your DLLs folder.

Infernal Libraries (aka DLL Hell)

In a Project of mine, I use a Delphi Application which dynamically loads a wrapper DLL (exporting C-Style functions) which in turn is statically link against a bunch of 3rd party DLLs.
It works fine on my test machines, but on my customers computer it failed to initialize with an error Message like "Couldn't find entrypoint _somefunction#4AKKZ in TMYlibrary.dll".
After some investigation with sysinternal's process monitor, I realized that Windows would look fror DLLs in windows/sytem32 first, so if a DLL named similar to my DLL was present in system32, windows would pick that one and try to find my function entry points in it - which would fail.
Do you know of a possiblity to change windows' DLL the searching behaviour?
Additional Information
[Update] The .exe file is located on the top level of the application's folder tree.
The Wrapper and the 3rd-party-DLLs ar e both located in the Subfolder /bin of my apps Folder
Dev platform is windows XP/7, using VS2008 for the dlll and Delphi 2010 for the application
I found another solution myself:
SetDllDirectory adds an additional search path to the list of locations to look at.
From http://msdn.microsoft.com/en-us/library/ms686203%28v=VS.85%29.aspx
After calling SetDllDirectory, the DLL search path is:
The directory from which the application loaded.
The directory specified by the lpPathName parameter.
The system directory. Use the GetSystemDirectory function to get the
path of this directory. The name of
this directory is System32.
The 16-bit system directory. There is no function that obtains the
path of this directory, but it is
searched. The name of this directory
is System.
The Windows directory. Use the GetWindowsDirectory function to get
the path of this directory.
The directories that are listed in the PATH environment variable.
(maybe i should do my googling before I post on SO ;)
Ship the DLL in your program's folder. (same as the exe file).
Then Windows should try your version first.