Exception from HRESULT: 0x80040013: when Integrating R with C#: - c#

I am trying to interface R with C# web application.I tried many solutions being provided but couldn't get it through. Error I am getting is: Exception from HRESULT: 0x80040013
Steps I followed were:
Installing R 2.15.1 for windows xp (32 bit)
Installed R_Scilab_DCOM3.0-1B5
Loaded rscproxy_1.3-1 package in the R/R.15.1/library
Set the System Variable path to C:/Program Files/R/R.15.1/bin
Added 3 COM references-STATCONNECTORCLNTLib,StatConnectorCommonLib,STATCONNECTORSRVLib
Code:
private StatConnectorClass _statconnector = new STATCONNECTORSRVLib.StatConnectorClass();
_statconnector.Init("R");--> Line shows the Exception from HRESULT: 0x80040013

I have used the COM method to interface to c# some years ago, but I gave up because on each version change there were problems similar to that you mentioned. In addition, the licensing scheme of R(D)COM is a bit obscure to me, and the .NET/COM/R tour has too many corners.
My current choice is Rserve, which is well maintained and has never let me down. I had written my own test bed for c#, but I consider it obsolete because RserveCLI is more complete.
R.NET in theory is a better concept, but for strange reasons it is considered "stable" by the author, which is highly euphemistic because of the many bugs mainly in memory management. Too bad it never took off.

I believe R.NET is a much better way to access R functionality from C#. It all runs in-process and so doesn't require an external R process to be running.
If you install the NuGet package, you will get version 1.5. This incorporates some memory management fixes I made that address most of the crashes people have reported. As Dieter points out, we use R.NET extensively via the F# RProvider, and it is extremely stable with the latest version. The RProvider also comes with a wrapper generator that generates wrapper functions for R functions which can be used from C#. So for example you could call the plot function from C# as R.plot(...params...). And you get Intellisense.

Related

Use Prolog with C# (Unity)

I would like to know if there is an "easy" way to call Prolog (preferably SWI-Prolog) code from C# code (in a Unity context).
I have already tried to use the SWI-Prolog - C# interface (source here) however it is not well maintained and I can't make it work with the last version of SWI-Prolog 64 bits (8.2.4 as of today).
I have also seen that it is possible to send information using an SWI-Prolog socket and a C# socket (or Unity Socket) but I can't make it work, and I am not sure using socket is the best idea for the performance.
The only solution working for me was using another "custom" prolog engine : CSharpProlog. This allows me to integrate Prolog code in CSharp files. Yet I would like to use SWI-Prolog if possible, do anyone knows how I can do that ?
From the link you provided, is not apparent that the source is available here.
Just try to compile it, to fit your target machine architecture.
I'm trying to generate the interface on Linux, with MonoDevelop, but it's not immediate, will retry later. Meanwhile, you could try on Windows. Probably will be simpler...
edit
Now I had the time to attempt to compile from source in Windows 10 64 bits, and so far it seems to run pretty well. I'm using VisualStudio Community 2017, and after opened the solution (contrib-swiplcs\SwiPlCs_git.sln), have defined _PL_X64 in SwiPLcs properties - compilation.
HTH
edit
Have uploaded this repo that shows a simplified usage of SWIPlCs.
Note there is a directory (swipl_cs) where I have simply copied (unchanged) the indispensable source files from SwiPlCs.
There is a single Prolog file, that computes N-Queens problem solutions (just the first, to keep things as simple as possible) and displays the steps performed by CLP(FD) - thanks to Markus Triska for the original implementation.

How do I open OLEDB Prompt for 32-bit from x64

Good day everyone,
I am writing a C# application that will allow users to dynamically set the database they want to connect to (I'll work a bit with the database data and such, but that's not important). The important part is that I'm allowing my users to connect to data-stores from OLEDB using the code below.
ADODB.Connection connection;
MSDASC.DataLinks instance = new MSDASC.DataLinksClass();
if( (connection = instance.PromptNew() as ADODB.Connection) == null ) return;
This will open the very same Dialog that windows opens for *.udl files, and that's exactly what I want.
However, I hit a interesting problem to which your brightness could come in handy: some customer WILL have to browse for x86 drivers, and the vast majority will certainly use x64.
I know you can open x86 UDL files with the following command-line:
"C:\Windows\syswow64\rundll32.exe" "C:\Program Files (x86)\Common Files\System\Ole DB\oledb32.dll",OpenDSLFile "C:\myConnectionFile.udl"
When the default (64 bit) command is:
"C:\Program Files\Common Files\System\Ole DB\oledb32.dll",OpenDSLFile "C:\myConnectionFile.udl"
In other words: windows' allowing users to create entries in both fashion. I would like to do the same in my app, using the API.
I have considered the option of creating a temp UDL file and opening from the command-line above, which made my conversation with my technical lead rather unpleasant, so that's not an option.
All suggestions are welcome. I will not dismiss unsafe coding nor the thought of building wrapper in C++ if we get to that (although my C++ is inconveniently rusty nowadays).
Thank you all in advance and happy coding...
Good day fellow developers,
After a tedious and lengthy research process I have around the answer I was looking for.
In order to use OLEDB providers for both 32 and 64 bit platform from one single C# 64bit app I'll need to create an Out-of-Process Wrapper to the 32bit call, and make the call over IPC (Internal Process Calls). Because the amount of functionalities I'm exposing is moderate, the hindrance was just re-creating some method calls on the wrapper.
This blog helped me put the parts together, and now I'm able to determine what type of OLEDB connection I'll allow my user create, and I'm also able to perform all operations I need regardless of the Provider Architecture.
I hope this will benefit other people who might be having the issue.
If time (and NDA) allows, I'll get the code here for people to copy and try it later.
These links were also very useful on my research
http://blog.mattmags.com/2007/06/30/accessing-32-bit-dlls-from-64-bit-code/
Registering the DLL Server for Surrogate Activation
https: // msdn.microsoft.com/en-us/library/windows/desktop/ms686606(v=vs.85).aspx)
Writing Serviced Component
https: // msdn.microsoft.com/en-us/library/3x7357ez(VS.80).aspx)
How to: Create Serviced Component
https: // msdn.microsoft.com/en-us/library/ty17dz7h(VS.80).aspx)
Create Out-Of-Process COM in C#/.Net?http: // stackoverflow.com/questions/446417/create-out-of-process-com-in-c-net
Thanks everyone
D

Method dominator message in CAT.NET

I have installed CAT.NET to analyze a binary.
This is a VS 2013, EF project with output as Class Library.
During the analysis it reports this:
Disabling method dominator caching due to excessive memory usage.
There is no information regarding that message in the output html.
What does this mean and where can I find more info.
Thanks.
I had the same problem with this tool. I opened the task manager during a dll scan and saw that the memory was increasing quickly, after that the tool showed the message, the memory usage decreased and the scan finished in a few seconds. It seems like the tool has a memory usage prevent system and I didn't find any information about it.
What I found was that this tool wasn't seem to be in code maintaining (the tool was released more than 3 years ago) and the version 2.0 never was released, in fact the 2.0 beta version was removed from the download repository. In the next link, there is a discussion, including Microsoft's developers opinions and the reason why they removed the file, but there isn't opinion about the new release http://social.msdn.microsoft.com/Forums/Lync/en-US/31663b6d-254e-48ce-8345-3355cb8b6023/whats-the-status-of-catnet-20?forum=catnet.
I ignore the reason why you use this tool, personally I chose it to prevent sql injection attacks. I did a very simple example to prove it:
internal static List<Category> GetCategoryNames(CategoryRequest request)
{
string sql = #"select classId, name from class where name like %"+ request.Name + "%";
return SqlUtils.ExecuteList(sql, "dbname", CategoryNameMapper);
}
and the tool didn't show me any risk in the report. I found another guy with the same problem http://social.msdn.microsoft.com/Forums/en-US/c99de49d-c541-40df-bb85-de5951261e0c/using-catnet-to-check-for-security-vulnarabilities-better-tool-available?forum=clr
That's why I decided to look around another options, if I find another tools with similar issues, I let you know.
Regards,
Omar.

Integrating matlab functions into c# project

I have a nice .net assembly of core matlab functions, created of course with the matlab compiler. For functions that accept numbers or arrays of numbers, this is fine; I can write code in c# without having to revert to matlab (well, the RCM has to be installed; that’s fine).
For functions that must reference other functions, however, the only way I can find so far to get a c# programme going is to compile both functions into the assembly. To explain better, let’s say I have a library in which I’ve stored the ode45 routine. If I want to solve a specific equation, let’s say something simple like dy/dx = -y, then I have to create a matlab script file which may be written as follows:
function dydx = diffeq(x, y)
dydx = -y
[obviously the analytical solution exists, but for the sake of this example let’s say I want to solve it this way]
Now in order to solve this equation, I would have to add this function as a method in my class to be compiled into the .net assembly. This of course ruins the generality of my library; I want application-specific equations in a different library to my core math function library. That is, the ODE45 method should reside in a “more core” library than the library in which the “diffeq” method would reside.
More than that, I would much prefer to create the “diffeq” method in a c# class that I can edit directly in e.g. VS2012. I would like to edit the equation directly rather than having to enter matlab each time and recompile an assembly.
To solve this problem, I have gone to the extent of decompiling the assembly which contains both the ode45 code and my differential equation method; it turns out the assembly is nothing but an interface to the MCR; the diffeq methods in the assembly return something like the following:
return mcr.EvaluateFunction(numArgsOut, “diffeq”, new object[0]);
We note that the function/method “diffeq” is not part of the MCR; MCR does not change. However, I can’t find the equation anywhere in the assembly.
Which begs the question “Dude, where’s my function?”
There is a ‘resources’ component of the assembly in which we find [classname].ctf, and in that we’ll find some machine code. This looks encrypted, but the equation might be hidden in there. If so, that would be a deliberate attempt to prevent when I am attempting, and kudos to MathWorks for making it impossible for me to avoid having to enter the matlab application!
However, there doesn’t seem to be anything in licensing to prevent what I want to do; I think it would be great if mathworks would allow as open an approach as that, but in the interrim, does anyone know how to do this?
The "MATLAB Compiler" has a somewhat misleading name. It is more of a deployment solution than a compiler in the actual sense (see note below). It is mainly intended to distribute MATLAB applications to end-users without requiring a full MATLAB installation on their part (only the royalty-free MCR runtime needs to be installed).
The MCR is in fact a stripped-down version of the MATLAB engine along with accompanying libraries.
When you use MATLAB Compiler to generate a binary package, the result is a target-specific wrapper (be it a standalone application, C/C++ shared library, Java package, or a .NET assembly) that calls the MCR runtime. The binary generated includes an embedded CTF archive containing all the original MATLAB content (your M-files and other dependencies) but in an encrypted form. When first executed, the CTF archive is extracted to a temp folder, and the M-files (still encrypted) are then interpreted by the MCR at runtime like typical MATLAB code.
There is an option in deploytool (mcc -C) to tell the compiler not to embed the CTF archive inside the binary as a resource, instead to place it as a seperate file next to the generated binary (this CTF archive can be inspected as a regular ZIP-file, but the source files inside are still encrypted of course).
See the following documentation page for more information:
Application Deployment Products and the Compiler Apps
PS: The truth is MATLAB Compiler started out as a product to convert MATLAB code into full C/C++ code which used the now discontinued "MATLAB C/C++ Math Library" (no runtime requirement, you just compile the generated C++ code and link to certain shared libraries; the result is a true compiled executable not a wrapper). This functionality completely changed around the time MATLAB 7 was released (the reason being that the old way only supported a subset of the MATLAB language, while using the current MCR mechanism enables deploying almost any code). Years later, MATLAB added a new product to replace the once-removed functionality of code translation, namely the MATLAB Coder.

Repeatable builds from same C# source code on different machines

I am trying to produce a tool which is smart enough to programmtically examine release version binaries produced by identical C# code compiled on two seperate machinces at different times and conclude that the code was identical while being able to pick up any code changes if present in the c# code used to produce these binaries.
I have tried using a number of approaches but in order to keep this short i'll just stick to the latest attempt.
I run ildasm with the /text option on the binaries and replace the GUIDs for anonymous fields etc in text, but when the binaries come from different pcs i find that the text produced by ILDASM /text option is reordered. The binaries originating from the same code but compiled by same setup on different machines also appear heavily reordered. Any suggestion how one may be able to control this reordering of IL would be much appreciated ?
Cheers
PS: Any alternative strategies of reliably accomplishing this are also most welcome.
Waiting for Eric Lippert to wake up :) - community wiki out of #mikez 's comment:
When a principal developer (Eric Lippert) on the compiler team speaks, you should listen: http://ericlippert.com/2012/05/31/past-performance-is-no-guarantee-of-future-results/ contains detailed explanation and strong recommendation for not doing it (likely in response to this precise question):
Is compiling the same C# program twice guaranteed to produce the same binary output?
No.
I found that a solution in accordance to what Eric Lippert's mentioned in his post what his client ended up settling for can be reached by setting the processor affinity for the compilation process to 01. After this the executables/ dlls produced are almost identical in excpetion to som mvid and guids used. Running ILDASM on these binaries with the text mode and building a simple hashing tool to strip away this random stuff provides such a solution. I am just providing this for the sake of completion and to help others who may face this problem.

Categories