Dlls in subfolder?

my app is dependent from several dlls. but i don't want all those dlls to be in the root directory, i'd like to put them into a special folder.. what do i have to do then so that my app searches for the dlls in this subfolder and not in the root? thx!
[261 byte] By [Vexator] at [2007-11-19 6:11:57]
# 1 Re: Dlls in subfolder?
You can use the win32 API; FindFirstFile:

Sample

void PluginLoader::LoadDllsFromDir( LPTSTR lpszDllPath )
{
WIN32_FIND_DATA findData = {0};
HANDLE hFind = 0;
int iCount = 0;
TCHAR szCurrent[_MAX_PATH];
string strDirectory;

// Remember current directory for restoring later
::GetCurrentDirectory( sizeof( szCurrent ) / sizeof( TCHAR ), szCurrent );

// Change to DLL directory (absolute path!)
::SetCurrentDirectory( lpszDllPath );

// Start searching for DLL's
hFind = ::FindFirstFile( "*.dll", &findData );
if( hFind != INVALID_HANDLE_VALUE )
{
do
{
// TODO: Do something with found files as you encounter them:
findData.cFileName;
}
while( FindNextFile( hFind, &findData ) );

// Stop searching
::FindClose( hFind );
}

// Restore current directory and exit
::SetCurrentDirectory( szCurrent );
}

Hope this helps.
loweyson at 2007-11-11 0:32:15 >
# 2 Re: Dlls in subfolder?
thx.. but i have to admit that i have no clue what to do now..
Vexator at 2007-11-11 0:33:10 >
# 3 Re: Dlls in subfolder?
When you say "app searches for them in sub-folder" do you mean that the app should load the dlls from those location ?

What kind of loading do you use, do you use LoadLibrary to explicitly load them or are they implicitly linked to the app ( which means windows looks for those dlls when the app is launched )?
kirants at 2007-11-11 0:34:09 >
# 4 Re: Dlls in subfolder?
they are implicitly linked... so windows saerches for them in the same directory where my application is. but as i have six of those apps and 15 dlls ( a graphics engine) it'd be a lot better if i could collect all those dlls in an extra folder. so how to tell windows to search in this sub folder for the dlls's?
Vexator at 2007-11-11 0:35:15 >
# 5 Re: Dlls in subfolder?
DLL search order is like this:

1. The directory from which the application loaded.
2. The current directory.
3. The system directory. Use the GetSystemDirectory function to get the path of this directory.
4. The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.

Windows Me/98/95: This directory does not exist.

5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6. The directories that are listed in the PATH environment variable.

So, you are left essentially with changing the PATH var.
kirants at 2007-11-11 0:36:14 >
# 6 Re: Dlls in subfolder?
mh that sucks.. :( but thx.
Vexator at 2007-11-11 0:37:15 >
# 7 Re: Dlls in subfolder?
Why not just put them in a folder that Windows searches anyway (e.g. the System folder) ?
John E at 2007-11-11 0:38:19 >
# 8 Re: Dlls in subfolder?
Why not just put them in a folder that Windows searches anyway (e.g. the System folder) ?
EEEwwww, no! There are already too many apps (including Windows itself) that drop DLL's into the Windows and System folders!

Personally, I think that an application should keep all it's DLL's to itself.

One method to have DLL's in a sub folder is to write a little "wrapper" application that appends your folder to the PATH var; then immediately launches your application. This way, you don't have to permanently change the user's PATH var. :cool:

Viggy
MrViggy at 2007-11-11 0:39:15 >
# 9 Re: Dlls in subfolder?
To add to my previous post...

Just write an app that calls CreateProcess (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/createprocess.asp) on your real app. One of the params is a pointer to an environment. You can read the PATH in the current env; modify it; pass it to CreateProcess to launch your real app.

Viggy
MrViggy at 2007-11-11 0:40:23 >
# 10 Re: Dlls in subfolder?
I would refrain from polluting the system folders too. I would let it be used for non-app related stuff like system dlls, drivers and such.
kirants at 2007-11-11 0:41:16 >
# 11 Re: Dlls in subfolder?
Hi Viggy & Kirants

Cram that system folder full of DLL's - that's my philosophy! :D

Admittedly tho' - my DLL's do get deleted whenever my apps get uninstalled...!
John E at 2007-11-11 0:42:25 >
# 12 Re: Dlls in subfolder?
Hi John,

Yeah, you know I once thought as you do. :)

Until my primary disk partition (which only contained my Windows install) ran out of space because of all the DLL's installed! :sick: :sick:

Viggy
MrViggy at 2007-11-11 0:43:25 >
# 13 Re: Dlls in subfolder?
MSDN:

Registering Application Path Information

The system supports "per application" paths. If you register a path, Windows sets the PATH environment variable to be the registered path when it starts your application. You set your application's path in the AppPaths subkey under the HKEY_LOCAL_MACHINE key. Create a new key using your application's executable file name as its name. Set this key's default value to the path of your executable file. The system uses this entry to locate your application if it fails to find it in the current path for example, if the user chooses the Run command on the Start menu and includes only the file name of the application, or if a shortcut icon doesn't include a path setting. To identify the location of .dll files placed in a separate folder, you can also include another value entry called Path and set its value to the path of your .dll files, as follows:

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths

Application Executable Filename = path
Path = path

The system will automatically update the path and default entries if the user moves or renames the application's executable file using the system shell user interface.
Vi2 at 2007-11-11 0:44:21 >