SetEnvironmentVariable not changing PATH variable

3

I'm using the MozNet plugin from Se7enSoft. This is a WebBrowser control for FireFox 3.6. It uses XulRunner.

The first thing I have to do is to execute an Initialize(...) method.

var binDirectory = Path.GetDirectoryName(
    Assembly.GetExecutingAssembly().Location);
var xulRuntimeDirectory = Path.Combine(binDirectory, "xul");
Se7enSoft.MozNet.Xpcom.Initialize(xulRuntimeDirectory, null);

I have to pass it the directory into which we've installed XulRunner. The Initialize method of this plugin internally uses the following DLLImport.

[DllImport("xpcom", CharSet = CharSet.Ansi, 
           EntryPoint = "NS_CStringContainerFinish",   
           CallingConvention = CallingConvention.Cdecl)]
internal static extern int Moz_CStringContainerFinish(ACString container);

The NS_CStringContainerFinish method from the XulRunner's xpcom.dll is required.

Just before this method is called the very first time, the MozNet plugin temporarily changes the PATH environment variable.

Environment.SetEnvironmentVariable("path", 
    Environment.GetEnvironmentVariable("path") + ";" + 
    binDirectory, EnvironmentVariableTarget.Process);

The XulRunner's location is temporarily added to the PATH environment variable to make sure it can resolve the xpcom.dll (and others).

However it still cannot find it. I'm receiving the following exception.

Unable to load DLL 'xpcom': Cannot find method. 
    (Exception from    HRESULT: 0x8007007F)
at Se7enSoft.MozNet.Native.MozNativeMethods.Moz_CStringContainerInit(
   ACString container)
at Se7enSoft.MozNet.Xpcom.XpCom_Init()
at Se7enSoft.MozNet.Xpcom.Initialize(String mozPath, String profPath)

This problem only occurs on 3 PC's (Windows 2000 & XP). Works fine for hundreds of others.

I can reproduce the problem if I debug and step over the Environment.SetEnvironmentVariable(...) method.

Are there any issues with SetEnvironmentVariable that might prevent it from changing the PATH environment variable?

c#
xulrunner

2 Answers

5

Found the cause.

The DLLImport statement automatically locates the xpcom.dll file using the dynamic link library search order.

[DllImport("xpcom", CharSet = CharSet.Ansi, 
           EntryPoint = "NS_CStringContainerFinish",   
           CallingConvention = CallingConvention.Cdecl)]
internal static extern int Moz_CStringContainerFinish(ACString container);

In short, it searches:

  1. The directory in which your application is installed.
  2. System directory
  3. 16-bit system directory
  4. Windows directory
  5. Current directory
  6. Directories in PATH environment variable.

Turned out the 3 PCs which had the issue, had a different xpcom.dll lingering around. This was found first and BOOM...exceptions galore.

A directory (UNC path) listed in the PATH environment variable pointed to a couple DLLs which were dependencies of xpcom.dll, namely:

  • nspr4.dll
  • nss3.dll
  • plc4.dll
  • plds4.dll

We fixed it by changing the PATH variable for our process only. Made sure our path was searched before any other by adding it at the beginning of the PATH environment variable.

var pluginDirectory = @"C:\....\xulrunner\");
var path = Environment.GetEnvironmentVariable("path");
Environment.SetEnvironmentVariable(
    "path", 
    pluginDirectory + ";" + path, 
    EnvironmentVariableTarget.Process);
answered on Stack Overflow Jun 18, 2012 by Christophe Geers • edited Jun 19, 2012 by Christophe Geers
0

Yes, there is a problem using set environment Variable,it just sets the contents of the specified environment variable for the current process.It will not have impact on the variable.

Please read following example for explanation.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.85).aspx

This function has no effect on the system environment variables or the environment variables of other processes.

answered on Stack Overflow Jun 18, 2012 by Rajesh Subramanian

User contributions licensed under CC BY-SA 3.0