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?

Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
  • Are you using roaming profiles? – David Brabant Jun 18 '12 at 09:11
  • Could be. Normally not, but we've had people with incorrect (roaming profiles) before. Checking into it. – Christophe Geers Jun 18 '12 at 09:14
  • OK. The users that reported the problem don't have roaming profiles. – Christophe Geers Jun 18 '12 at 11:19
  • Would you still recommend MozNET in a VB project running .NET 3.5? It seems like a ton of links are broken on their website, including the documentation. I just tried to register on their forum after Google connected me to a post here http://7s.unilith.org but it seems like the last legit post was in 2012? Not sure what to make of MozNet right now... – whyoz Mar 11 '15 at 23:48
  • No I wouldn't use it. It is only maintained by one guy IIRC. We have removed MozNET since then. We switched to Chromium with the aid of CefSharp since then (https://github.com/cefsharp/CefSharp). – Christophe Geers Mar 12 '15 at 09:19

2 Answers2

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);
Christophe Geers
  • 8,564
  • 3
  • 37
  • 53
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.

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42