469,625 Members | 1,086 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Append to PATH Environment Variable

I have an application that uses PInvoke to call into native DLLs. My
DLLimport attribute only has the name of the DLL that I need to import (i.e.
I leave it up to the OS to search the PATH for the DLL.) This all works
great when the user's PATH variable is setup correctly.

Unfortunately, there are times when the user's PATH is too full or whatever
to have the correct search path for my application.

What I was wondering is if there is a way to set the PATH variable to have
the correct search directory after my application starts up (yet before any
call to DLLImport). This way I can guarantee that the search is correct for
the running instace of my application.

I tried this at the beginning of my application to no avail:

System.Diagnostics.ProcessStartInfo info =
System.Diagnostics.Process.GetCurrentProcess().Sta rtInfo;

string origpath = info.EnvironmentVariables["path"];
string newpath = "C:\\mypath;" + origpath;
info.EnvironmentVariables["path"] = newpath;

****

I know I could create a separate executable that sets the Environment
variables and calls StartProcess to launch my application. I was hoping that
my application could set this path infromation without requiring another
executable to launch my application.

Thanks!
Nov 16 '05 #1
1 8781
Chris,

You could always call the SetEnvironmentVariable function through the
P/Invoke layer.

Also, if you can wait for .NET 2.0, you are able to take a function
pointer and assign it to a delegate to call. This will allow you to load
the dll (LoadLibrary, LoadLibraryEx), get the procedure address
(GetProcAddress), and then get a delegate that you can call which will call
the function at that address.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris B" <Ch****@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
I have an application that uses PInvoke to call into native DLLs. My
DLLimport attribute only has the name of the DLL that I need to import
(i.e.
I leave it up to the OS to search the PATH for the DLL.) This all works
great when the user's PATH variable is setup correctly.

Unfortunately, there are times when the user's PATH is too full or
whatever
to have the correct search path for my application.

What I was wondering is if there is a way to set the PATH variable to have
the correct search directory after my application starts up (yet before
any
call to DLLImport). This way I can guarantee that the search is correct
for
the running instace of my application.

I tried this at the beginning of my application to no avail:

System.Diagnostics.ProcessStartInfo info =
System.Diagnostics.Process.GetCurrentProcess().Sta rtInfo;

string origpath = info.EnvironmentVariables["path"];
string newpath = "C:\\mypath;" + origpath;
info.EnvironmentVariables["path"] = newpath;

****

I know I could create a separate executable that sets the Environment
variables and calls StartProcess to launch my application. I was hoping
that
my application could set this path infromation without requiring another
executable to launch my application.

Thanks!

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jeff Wagner | last post: by
1 post views Thread by jwpioneer | last post: by
1 post views Thread by Bonj | last post: by
10 posts views Thread by mirandacascade | last post: by
3 posts views Thread by HMS Surprise | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.