Re: New essay: whereis utility

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
news:5ost02dd259h4onf5n485v3k6e7671mvjc@xxxxxxxxxx
| Ah, but you are simulating what you THINK is happening. In fact, that is
not what
| ACTUALLY happens. _getcwd gives the current working directory, but the
search strategy
| involving the current working directory changes with various releases and
service packs.
| You also don't account for the "known DLLs" feature if you are asking
where a DLL is
| found. So if you ask where "kernel32.dll" is, for example, your code
gives an erroneous
| answer. See the latest posting of my article,, where I demonstrate that
your code does
| not work correctly in XP Pro SP 2.
|
| You may have been "using it for years" but in fact it doesn't work
correctly. And that's
| the point of my post.
| joe
|
| On Tue, 7 Mar 2006 21:29:51 -0800, "Ed Weir \(ComCast\)" <Anon@xxxxxxxx>
wrote:
|
| >"Joseph M. Newcomer" <newcomer@xxxxxxxxxxxx> wrote in message
| >news:tpls02989k8k4o7lbijnn66252mgr1v965@xxxxxxxxxx
| >| The whereis utility is often useful. The problem in Windows is that
there
| >are many
| >| different search algorithms, based on the platform, Windows release,
| >Service Packs
| >| installed, etc., so any program that works like the Unix program, that
is,
| >which simulates
| >| the search path, is likely to fail, particularly in light of the
possible
| >future changes
| >| that may occur. This program uses the LoadLibraryEx call to search the
| >load path.
| >|
| >| www.flounder.com/whereis.htm
| >|
| >| (It took almost more time to write it up and create the zipfile than to
| >write the program
| >| itself...)
| >| joe
| >| Joseph M. Newcomer [MVP]
| >| email: newcomer@xxxxxxxxxxxx
| >| Web: http://www.flounder.com
| >| MVP Tips: http://www.flounder.com/mvp_tips.htm
| >
| >Thanks, but I've been using this for years, without any need for updates:
| >#include <Afx.h>
| >#include <io.h>
| >#include <direct.h>
| >
| >typedef struct _finddata_t FINDDATA, *PFINDDATA;
| >
| >int main(int argc, TCHAR *argv[])
| >{
| > CStringArray rgStrPaths;
| > CStringArray rgStrPExts;
| > CString strFileName;
| > LPTSTR pszEnvPath = getenv("PATH"), pszVar;
| > LPTSTR pszPathExt = getenv("PATHEXT"); // Order or precedence
| >
| > if(2 > argc)
| > {
| > printf("usage: which filename (.COM;.EXE;.BAT;.CMD)\n\tshows which
| >executable is found in path\n");
| > return ERROR_SUCCESS;
| > }
| >
| > if(NULL == pszEnvPath) // If getenv fails (rare if ever) - user has no
path
| > {
| > // I'm going to leak this, because this tool terminates soon anyway.
| > pszEnvPath = _getcwd(NULL, sizeof(TCHAR) * MAX_PATH);
| > if(NULL == pszEnvPath)
| > {
| > printf("No path\n");
| > return ERROR_PATH_NOT_FOUND;
| > }
| > else
| > rgStrPaths.Add(pszEnvPath);
| > }
| >
| > if(NULL == pszPathExt)
| > { // Not available in environment?
| > pszPathExt = ".COM;.EXE;.BAT;.CMD"; // Set default precedence
| >// printf("Default precedence: %s\n", pszPathExt);
| > }
| >// else
| >// printf("Precedence: %s\n", pszPathExt);
| >
| > // Get the CWD to search first
| > if(0 == rgStrPaths.GetSize()) // No cwd yet?
| > {
| > strFileName.Format("%s\\", _getcwd(NULL, sizeof(TCHAR) * MAX_PATH));
| > rgStrPaths.Add(strFileName);
| > }
| >
| > pszVar = strtok(pszEnvPath, ";");
| > while(NULL != pszVar)
| > {
| > strFileName = pszVar;
| >
| > if(strFileName.Right(1) != '\\')
| > rgStrPaths.Add(strFileName + "\\");
| > else
| > rgStrPaths.Add(strFileName);
| >
| > pszVar = strtok(NULL, ";");
| > }
| >
| > // Guaranteed to have one or more path variables to search, load
precedence
| >of executable extensions
| > pszVar = strtok(pszPathExt, ";");
| > while(NULL != pszVar)
| > {
| > rgStrPExts.Add(pszVar);
| > pszVar = strtok(NULL, ";");
| > }
| >
| > FINDDATA fd;
| > int i;
| >
| > for(i = 0; i < rgStrPaths.GetSize(); i++) // Paths loop
| > {
| > // See if the user gave us an extension
| > if(NULL != strchr(argv[1], '.'))
| > {
| > // Extension is user supplied:
| > strFileName.Format("%s%s", (LPCTSTR) rgStrPaths.GetAt(i), argv[1]);
| > if(-1 != _findfirst((LPCTSTR) strFileName, &fd))
| > {
| > // Found it, tell the user where and what (could have used a
wildcard)
| > strFileName.Format("%s%s", (LPCTSTR) rgStrPaths.GetAt(i), fd.name);
| > printf("%s\n\n", (LPCTSTR) strFileName);
| > return ERROR_SUCCESS;
| > }
| > }
| > else
| > {
| > // No user extension: look through precedence
| > for(int x = 0; x < rgStrPExts.GetSize(); x++)
| > {
| > strFileName.Format("%s%s%s",
| > (LPCTSTR) rgStrPaths.GetAt(i),
| > argv[1],
| > (LPCTSTR) rgStrPExts.GetAt(x) );
| >
| > if(-1 != _findfirst((LPCTSTR) strFileName, &fd))
| > {
| > // Found it, tell the user where and what (could have used a
wildcard)
| > strFileName.Format("%s%s",
| > (LPCTSTR) rgStrPaths.GetAt(i),
| > fd.name );
| >
| > printf("%s\n\n", (LPCTSTR) strFileName);
| > return ERROR_SUCCESS;
| > }
| > }
| > }
| > }
| >
| > printf("%s not found in path\n\n", argv[1]);
| > return ERROR_SUCCESS;
| >}
| >
| >
| >I've also done tail (I called it ScrollFile) if you would like to see
that
| >too,
| >- Ed.
| Joseph M. Newcomer [MVP]
| email: newcomer@xxxxxxxxxxxx
| Web: http://www.flounder.com
| MVP Tips: http://www.flounder.com/mvp_tips.htm

C:\ >which kernel32.dll
C:\WINDOWS\system32\kernel32.dll

C:\Program Files\MyApp >which kernel32.dll
C:\WINDOWS\system32\kernel32.dll

C:\Program Files\MyApp >ver

Microsoft Windows XP [Version 5.1.2600]

C:\ >dir kernel32.dll /s
Volume in drive C is C
Volume Serial Number is CC9E-DDDF

Directory of C:\i386

08/04/2004 02:00 AM 983,552 kernel32.dll
1 File(s) 983,552 bytes

Directory of C:\WINDOWS\ServicePackFiles\i386

08/04/2004 12:56 AM 983,552 kernel32.dll
1 File(s) 983,552 bytes

Directory of C:\WINDOWS\system32

08/04/2004 04:00 AM 983,552 kernel32.dll
1 File(s) 983,552 bytes

Directory of C:\WINDOWS\system32\dllcache

08/04/2004 04:00 AM 983,552 kernel32.dll
1 File(s) 983,552 bytes

Total Files Listed:
4 File(s) 3,934,208 bytes
0 Dir(s) 14,344,904,704 bytes free

C:\ >

Yep, still works fine.

Note: COM/DCOM trancends search paths, so is irrelevant to which. I
wouldn't want to which a COM/DCOM app anyway... which remains appropriate
in the domain for which it is intended. Whereis, on the other hand; perhaps
you mean, "dir \kernel32.dll /s"?

Cheers
- Ed.

.



Relevant Pages

  • Re: Newbie: module structure and import question
    ... Thx Rob. ... what py file will import it and where the working directory should be. ... Thx again, Rob. ... > I think your issue is your module search path. ...
    (comp.lang.python)
  • Re: relative-to-source file names (was: Small, understandable Forth)
    ... The system library is found in the search path. ... tarballs or setup.exe: Install time). ... The current working directory is straight forward - a relative path name ... fair amount of it would be components similar to MINOS. ...
    (comp.lang.forth)
  • Re: New user
    ... chmod +x ... The current working directory ... That's in the search path. ... loaded Suse Linux 10. ...
    (alt.os.linux.suse)
  • Re: .pth files in working directory
    ... Michael Ekstrand schrieb: ... Python puts the current working directory in the default search path. ...
    (comp.lang.python)