System.DllNotFoundException

Apr 29, 2012 at 6:38 PM
Edited May 1, 2012 at 8:52 AM

Hello all.

Ok, lets start with some background info:

Windows 7 64bit/x64 (Genuine, i purchased it) - All updates till now are installed.

I was working with StatconnDCOM to begin with, so R was firstly introduced to my computer via "RAndFriends" (that includes R 2.14.0, packages, RExcel, SWord, StatconnDCOM), but then i deleted R and downloaded the official R-alone version 2.15.0 and i installed it.

I always use custom paths, so i install programs (like those) on C:\Progs, and games on C:\Games.

[i cant think of any other info relevant, but please feel free to ask]

 

1) When using old R versions and current Stable R.NET (v1.4), everything's ok (but i cant expect my users to roll-back their R just to use my program..)

2) When i use the Current R (2.15.0) with stable R.NET 1.4, it has a problem with "Rlapack.dll"

3) When i got the unstable "rdotnet_6d2c3f161801" with the Current R (2.15.0):

     3.1) i have an error 'Public Shared Function SetDllDirectory(dllDirectory As String) As Boolean' is obsolete: 'Set environment variable 'PATH' instead.'

         So i went to "System Properties -> Advanced -> Environment Variables" and edited both the User and the System environment variable "named "path" and added ";C:\Progs\R\R-2.15.0\bin\i386". But i still got the error

    3.2) when i run the program, i get this error: "System.DllNotFoundException: Dll was not found. at RDotNet.NativeLibrary.UnmanagedDll..ctor(String dllName) in C:\Downloads\rdotnet_6d2c3f161801\RDotNet.NativeLibrary\UnmanagedDll.cs:line 40

at RdotNet.REngine..ctor(String id, String dll)

at RDotNet.REngine.CreateInstance(String id, String dll(

at RAttempFour.Form1.Form1_Load(Object sender, EventArgs e) on C:\blablablah\form1.vb:line18)

-------------------------------------------------------------------------------------------------------------------------------

The code i'm trying to run is the example posted on R.NET's page:

Public Class Form1
 
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgsHandles MyBase.Load
        Try
            setText(Registry.GetValue("HKEY_LOCAL_MACHINE\Software\R-core\R""InstallPath""C:") & "\bin\i386")
 
            'REngine.SetDllDirectory(Registry.GetValue("HKEY_LOCAL_MACHINE\Software\R-core\R", "InstallPath", "C:") & "\bin\i386")


            'Using engine As REngine = REngine.CreateInstance("RDotNet", {"-q"})    'This stopped working after stable R.Net 1.4
            Dim engine As REngine = REngine.CreateInstance("R""-q")
            ' quiet mode
 
 
            ' .NET Framework array to R vector.
            Dim group1 As NumericVector = engine.CreateNumericVector(New Double() {30.02, 29.99, 30.11, 29.97, 30.01, 29.99})
            engine.SetSymbol("group1", group1)
 
            ' Direct parsing from R script.
            Dim group2 As NumericVector = engine.Evaluate("group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)").AsNumeric()
 
            ' Test difference of mean and get the P-value.
            Dim testResult As GenericVector = engine.Evaluate("t.test(group1, group2)").AsList()
            Dim p As Double = testResult("p.value").AsNumeric().First()
 
            'mine
            Dim p2 As Double = engine.Evaluate("ks.test(group1, group2)").AsNumeric.FirstOrDefault
            '/
 
            Dim Result As String = String.Format("Group1: [{0}]" & vbCrLf & "Group2: [{1}]" & vbCrLf & "P-value = {2:0.000}" & vbCrLf & "P-value = {3:0.000}"String.Join(", ", group1), String.Join(", ", group2), p, p2)
 
            setText(Result)
 
 
            'engine.EagerEvaluate("source(""" & "C:\Users\N1h1l1sT\Dropbox\Visual Basic 2010\Projects\StatREC\StatREC\bin\z\new1.txt".Replace("\", "\\") & """)")
 
 
 
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
 
    Private Sub setText(str As String)
        If str.ToLower = "-clear" Then
            TextBox1.Text = String.Empty
        Else
            TextBox1.Text &= vbCrLf & vbCrLf & str
        End If
 
        TextBox1.SelectionStart = TextBox1.Text.Length
    End Sub
 
End Class

 

Developer
May 1, 2012 at 2:39 AM

I cannot reproduce your issue. I upgraded to R2.15.0 from R2.14.2 on a Win7 32 bits + R.NET1.4 and Win7 64 bits with the latest R.NET code. In both cases the sample code on the rdotnet home page works. Actually, even skipping the call to SetDllDirectory also works, presumably because the path to R.dll e.g. "C:\bin\R\R\bin\i386" is in the PATH.

That said, I know a colleague who had to move around RLapack.dll to get it to work. I am not advising this, but there are indeed some issues arising on some computers, yet to be reproduced. RLapack.dll does not seem to be an explicit dependency from R.dll from what I can see, but I may be wrong.

Meanwhile:

- You could try to add the path to the R dll to the PATH env variable (see below for instructions), e.g. probably C:\Program Files\R-2.15.0\bin\i386 or C:\Program Files\R-2.15.0\bin\x64 for you.

- If the issue persists can you determine as small a code as possible reproducing your issue and share it? Can you also share more information as to your setup (architecture 32 or 64, etc., in fact even if you are even using Windows, which sounds the case).

Regarding your question as to what 'Set environment variable 'PATH' instead.' means, you can follow the instructions in http://support.microsoft.com/kb/310519 (WinXP, but basically identical for more recent O/S).

May 1, 2012 at 8:54 AM

I've edited my original post to reflect all needed info. please check it out.

 

We have to make this to work - its the absolute basic Oo

Developer
May 1, 2012 at 1:48 PM

Hi,

I quickly made a console app in VB, and got your code to work with the stable release. It worked as expected on a Win7 32 bits. I cannot access my x64 bit box right now, and I'd have liked to check something. I notice your OS is 64 bits, but that you specify the 32 bits libraries in R. Is the target architecture of your VB program set to x86? if it is set to AnyCPU or x64, the R.dll in the i386 folder will fail to load, as the process is 64bits.

I would have liked to test this before, but I may not be able to do this in the coming days. I hope my guess about the issue is correct.

CHeers

May 1, 2012 at 1:52 PM
Edited May 1, 2012 at 2:01 PM

oh, i havent changed the settings of my vb project to x64, and i believe the default is anyCPU. could that be it? that's so folly :P

I'll test it right again and edit this post to present the results. (i.e. ill test both loading the 64bit binaries, and if it fails, to try x86 Visual Studio Settings)

 

Edit:

Both fail.

Actually the default wasnt AnyCPU, it was x86. So i correctly loaded the x86 (i386) binary files. but it won;t work...

i dont get it... and i dont have any ideas to give them a try.. i'm downright stuck :(

May 1, 2012 at 5:44 PM

As Dr. Sheldon Cooper would say: I'm attached to another object by an inclined plane wrapped helically around an axis.........

May 21, 2012 at 6:37 PM

Anyone guys? please.. i've been struggling with this error for far too long...

 

i need some fresh ideas. why isnt it working? i can make it x64, i can make it x86 - whatever suits you - just give me some information why isn't it working and how to gix it.

Jun 27, 2012 at 10:48 AM

In my case, the problem had been solved by using function, "engine.Initialize()" after CreateInstance...
I compiled with R-2.15.1 & R.NET (rdotnet_a833e72cbf58).

Good luck!

Jul 11, 2012 at 6:05 AM

I have the same problem

2) When i use the Current R (2.15.0) with stable R.NET 1.4, it has a problem with "Rlapack.dll"

now i change the r.net version R.NET (rdotnet_a833e72cbf58),but it still have the same problem.

not all the computer,In my computer it running ok.but in my co-worker's computer,it's fail.

Dec 17, 2013 at 11:06 AM
Did you ever work this out? I have the same error using R 3.0.2 but only when the code is running on my CI server (TeamCity)... when I run the code from within Visual Studio there's no problem.

I thought it might be something to do with the environment PATH... I'd put C:\Program Files\R\R-3.0.2\bin\i386 in the system PATH on my machine and I figured there was some chance that maybe TeamCity was running things in 64 bit mode where Visual Studio was running in 32 bit... so I tried adding the following code, but still no dice:
// Make sure our engine can find the R installation on this machine
var envPath = Environment.GetEnvironmentVariable("PATH");
var rInstallPath = (string)Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\R-core\R", "InstallPath", @"C:\Program Files\R\R-3.0.2");
var rBinPath = Environment.Is64BitProcess ? @"\bin\x64" : @"\bin\i386";            
Environment.SetEnvironmentVariable("PATH", envPath + System.IO.Path.PathSeparator + System.IO.Path.Combine(rInstallPath,rBinPath));

// Create our static engine instance
engine = REngine.CreateInstance("RDotNet");

// From v1.5, REngine requires explicit initialization.
// You can set some parameters.
engine.Initialize();
Unfortunately the UnmanagedDll method doesn't say which dll it was unable to load/find... although I'm not sure if that would help anyway (perhaps with full path details it might).
Developer
Dec 17, 2013 at 12:30 PM
Previous posts date back to more than a year ago; I assume you (jamescrosswell) are using the latest 1.5.5. The error messages and other things have been improved on a branch with my user name, but have yet to be merged back to a release (which may happen after the new year). Meanwhile I can only propose you build a version from that branch to try to diagnose the issue further.
Dec 17, 2013 at 7:55 PM
Thanks, I am using 1.5.5 yeah (which I got via NuGet).

I downloaded the source and added a bit of logging. After playing around with this a bit, I realize that it's possible to pass in an explicit path to the R.dll library... which I'm now doing.

After that, I started to get some different errors, complaining that R couldn't load a forecast package that I need (or it's various dependencies). Essentially this is because the packages were installed in my R_LIBS_USER folder and not in the R_LIBS folder... when R.NET was run by TeamCity then (as the local System user) these packages weren't available. The solution was to copy/move the relevant libraries from R_LIBS_USER to R_LIBS.

Kind Regards,

James Crosswell
Founder | Tea Boy
www.mentaldesk.com
Dec 26, 2013 at 12:31 AM
Edited Dec 26, 2013 at 12:32 AM
I might have a similar situation to that of James.
Setup is: VS 2013, R 3.0.2, R.Net 1.5.5, Win 7 SP1 64bit.
The error is:
"An unhandled exception of type 'System.DllNotFoundException' occurred in RDotNet.NativeLibrary.dll
Additional information: Dll was not found."

This is just when I try to instantiate the engine:
using (REngine engine = REngine.CreateInstance("RDotNet"))

I am trying to point the system to use the x64 folder as VS is running a 32 bit process:
        var oldPath = System.Environment.GetEnvironmentVariable("PATH");
        var rPath = @"C:\Program Files\R\R-3.0.2\bin\x64";
        var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath);
        System.Environment.SetEnvironmentVariable("PATH", newPath);
is there anything else I should be doing?
Developer
Dec 26, 2013 at 12:41 AM
Edited Dec 26, 2013 at 12:45 AM
I am trying to point the system to use the x64 folder as VS is running a 32 bit process:
Not sure what you mean by that: VS itself is a 32 bits process (I think even for VS2013), but an application launched in debug mode from it can be 32 or 64 bits depending on the specified target platform in the project properties.

You should use Environment.Is64BitProcess as James gave in the sample code, to be more agnostic to the 'bitness' of the process

Other option to get more detailed error messages is to use the code in development: https://rdotnet.codeplex.com/SourceControl/latest and branch 'jperraud'
Dec 26, 2013 at 1:40 AM
And remember, if you are running C# as 32 bit you can't call the 64 bit R anyway. The default R installation is 64 bit, but C# runs in 32 bit mode by default (it's faster for most applications as the pointers are 1/2 the size).
Dec 29, 2013 at 6:25 PM
Thanks, that helped.
Turns out it is not possible to call a 64-bit dll (R.net) from a 32-bit process (Visual Studio)