Discussion:
SearchPath Fails
(too old to reply)
Mike Copeland
2009-10-23 23:58:52 UTC
Permalink
Using VC++ 6.0...
How do I get SearchPath to work? This code compiles, but it doesn't
find the file "skel1.ect" that exists in one of the directories in the
Path. (The code executes and does return the Path string, but the
"sResult" variable doesn't get populated.) Please advise. TIA

DWORD err = GetEnvironmentVariable("PATH", NULL, 0);
char* path = new char[err+1]; path[err] = 0;
GetEnvironmentVariable("PATH", path, err);
err = SearchPath(path, "skel1", ".ect", 0, NULL, NULL);
char* sResult = new char[err+1]; sResult[err] = 0;
err = SearchPath(path, "skel1", ".ect", err, sResult, NULL);
Igor Tandetnik
2009-10-24 02:07:11 UTC
Permalink
Post by Mike Copeland
Using VC++ 6.0...
How do I get SearchPath to work? This code compiles, but it doesn't
find the file "skel1.ect" that exists in one of the directories in the
Path. (The code executes and does return the Path string, but the
"sResult" variable doesn't get populated.) Please advise. TIA
DWORD err = GetEnvironmentVariable("PATH", NULL, 0);
char* path = new char[err+1]; path[err] = 0;
GetEnvironmentVariable("PATH", path, err);
err = SearchPath(path, "skel1", ".ect", 0, NULL, NULL);
char* sResult = new char[err+1]; sResult[err] = 0;
err = SearchPath(path, "skel1", ".ect", err, sResult, NULL);
Works for me. Do you actually have a file named skel1.ect somewhere on your path?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Mike Copeland
2009-10-25 00:51:36 UTC
Permalink
Post by Igor Tandetnik
Post by Mike Copeland
Using VC++ 6.0...
How do I get SearchPath to work? This code compiles, but it doesn't
find the file "skel1.ect" that exists in one of the directories in the
Path. (The code executes and does return the Path string, but the
"sResult" variable doesn't get populated.) Please advise. TIA
DWORD err =3D GetEnvironmentVariable("PATH", NULL, 0);
char* path =3D new char[err+1]; path[err] =3D 0;
GetEnvironmentVariable("PATH", path, err);
err =3D SearchPath(path, "skel1", ".ect", 0, NULL, NULL);
char* sResult =3D new char[err+1]; sResult[err] =3D 0;
err =3D SearchPath(path, "skel1", ".ect", err, sResult, NULL);
Works for me. Do you actually have a file named skel1.ect somewhere on your path?
Yes, I sure do. Several in different pathed directories.
So, it should work asis, right? 8<{{
Ulrich Eckhardt
2009-10-26 08:00:51 UTC
Permalink
Post by Mike Copeland
Using VC++ 6.0...
This is >10 years old, unsupported by the vendor and lacks in several
aspects. I'd suggest upgrading.
Post by Mike Copeland
How do I get SearchPath to work? This code compiles, but it doesn't
find the file "skel1.ect" that exists in one of the directories in the
Path. (The code executes and does return the Path string, but the
"sResult" variable doesn't get populated.) Please advise. TIA
DWORD err = GetEnvironmentVariable("PATH", NULL, 0);
char* path = new char[err+1]; path[err] = 0;
GetEnvironmentVariable("PATH", path, err);
err = SearchPath(path, "skel1", ".ect", 0, NULL, NULL);
char* sResult = new char[err+1]; sResult[err] = 0;
err = SearchPath(path, "skel1", ".ect", err, sResult, NULL);
1. Do not use new when you can avoid it. In this case, using a
std::vector<char> would have been better.
2. Don't put multiple statements on one line. It confuses the casual reader.
3. Do not reuse variables. This makes it difficult to see what value the
variable currently holds.
4. Check returnvalues, GetEnvironmentVariable() returns zero on failure. If
that happens, this code will still execute "correctly" but with an empty
PATH.
5. Verify that the results of GetEnvironmentVariable() are what you expect.
Alternatively, hard-code the path and try again. The point is that you have
two operations here and you claim the overall operation failed. Knowing
which of the two failed (or both) is a key to solving the overall problem.
Divide and conquer!

That said, are you aware of how environment variables work, i.e. how they
are propagated between processes? The part that causes most confusion is
that they are _NOT_ system-global variables!

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
xcal
2009-10-27 18:17:36 UTC
Permalink
hi Urich,
Post by Ulrich Eckhardt
1. Do not use new when you can avoid it. In this case, using a
std::vector<char> would have been better.
excuseme, why not ?

thanks in advance,
Carlos.
Ben Voigt [C++ MVP]
2009-10-27 19:14:49 UTC
Permalink
Post by xcal
hi Urich,
Post by Ulrich Eckhardt
1. Do not use new when you can avoid it. In this case, using a
std::vector<char> would have been better.
excuseme, why not ?
Because it doesn't leak memory.

With new, you have to remember to call delete on every potential branch out
of the function, including exceptions, early returns, etc. std::vector,
smart pointers, RAII, all of them use a destructor for cleanup which handles
all these scenarios automatically.
Post by xcal
thanks in advance,
Carlos.
Ben Voigt [C++ MVP]
2009-10-27 19:14:49 UTC
Permalink
Post by xcal
hi Urich,
Post by Ulrich Eckhardt
1. Do not use new when you can avoid it. In this case, using a
std::vector<char> would have been better.
excuseme, why not ?
Because it doesn't leak memory.

With new, you have to remember to call delete on every potential branch out
of the function, including exceptions, early returns, etc. std::vector,
smart pointers, RAII, all of them use a destructor for cleanup which handles
all these scenarios automatically.
Post by xcal
thanks in advance,
Carlos.
xcal
2009-10-27 18:17:36 UTC
Permalink
hi Urich,
Post by Ulrich Eckhardt
1. Do not use new when you can avoid it. In this case, using a
std::vector<char> would have been better.
excuseme, why not ?

thanks in advance,
Carlos.

Tamas Demjen
2009-10-26 20:32:24 UTC
Permalink
Post by Mike Copeland
How do I get SearchPath to work? This code compiles, but it doesn't
find the file "skel1.ect" that exists in one of the directories
Try calling GetLastError right after SearchPath, and tell us what value
it returns. 2, for example, means the file does not exist on the path.

Also note that some processes have a different PATH variable than
others. Typing PATH from a console may print something different than
what your app sees. Just guessing.

Your code works for me too, BTW.

Tom
Loading...