Append to PATH Environment Variable

G

Guest

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().StartInfo;

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!
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top