Run matlab and excute file.m from website - c#

i m building a website, and i want to run MATLAB file on the server and display the result on web page,
please if u have any idea how to run matlab from web site tell me
thank u soo much .

This is explained in detail in this guide: http://www.mathworks.com/help/toolbox/compiler/example_guide/brh232k.html
Basically you need to compile your .m file using the Matlab Builder NE and deploy it as a
webservice or normal ASPX file. See here for an overview of the deployment scheme.

This might be a terrible hack but perhaps it helps. I have read that you can make an executable from your MATLAB file. If that is so. The following has worked for me when I need a Web Application to execute an executable and show the results. Be warned, the account your web application runs under will need permissions to run the executable.
Once you have made your MATLAB file into an executable you can create a process, redirect its standard output and place that output in a web element (in this example I used a label).
//Get the path to the executable you wish to run from a setting in web.config
var executablePath = ConfigurationManager.AppSettings["MATLAB_EXECUTABLE_PATH"];
//Create a process to execute the executable. Redirecting the output.
var proc = new Process();
proc.StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
ErrorDialog = false,
FileName = executablePath,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,
UseShellExecute = false //Very important do not leave this out.
};
proc.Start(); //Execute the executable.
lblOutput.Text = proc.StandardOutput.ReadToEnd(); //use the results
lblErrorMessages.Text = proc.StandardError.ReadToEnd(); //Show any error output from the executable.

Related

Unable to run SAS script through C#

I have A requirement to run SAS script through Web application using asp.net and c#.
I have used ProcessStartInfo to execute SAS script. This works fine locally with solution.Once i hosted the application in IIS, it is not working and returning exit code with 111. Please help me to solve this issue.
ProcessStartInfo info = new ProcessStartInfo("path of SAS EXE","file path");
int exitCode = 0;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
Process p = Process.Start(info);
p.WaitForExit();
exitCode = p.ExitCode;
Have you considered not doing it using process? SAS has a technology called Integration Technologies which you most likely have. It interfaces using a COM interface. You can then execute SAS that way and pass what is needed vs using a command line call.
Here is sample code:
SAS.Workspace ws = new Workspace();
LanguageService ls = ws.LanguageService;
StoredProcessService sp = ls.StoredProcessService;
sp.Repository = #"file:" + #"x:\temp";
sp.Execute("test.sas", string.Empty);
string log = ls.FlushLog(1000);
If you need to do it via the process start, here is code:
Also, if you are passing commands to SAS, I don't see any. You have to pass commands to SAS from command line (program name at a minimum). info.Arguments is a start. Also, redirect the std output to a file. Look at info.RedirectStandardOutput and info.RedirectStandardError. However, i don't believe that is the issue. I think you are encountering a security issue. Look at Event Viewer and see if it pops up. IISS requires security to execute in a directory.
Finally, why are you using IIS? Unless you have a legacy requirement, IIS should not be used. Switch to Kestrel and ASP.NET Core. I will be presenting a paper at SGF on the use of SAS in this way. Download the paper and code as soon as they are available (next week?)

Process start but don't show the window

I'm trying to open an external executable. I can easily open external executables by using the Process class from System.Diagnostics:
Process p = new Process()
p.StartInfo.FileName = processName;
p.Start();
This works fine with most programs, like browsers and notepad, creating a process and showing its graphical interface. However, when I try to open my desired program, the process is started (can see it in the task manager, it even takes a whole CPU core for processing) but the GUI window doesn't show. What could possibly happen to a process from Process.Start to not show its GUI?
For reference, the program I want to execute is ADOM release 60, which runs 100% fine when I open it directly in the Explorer shell or in the Powershell. This is reproducible in both console and WindowsForms applications.
Here are some other settings that did not help:
p.StartInfo.CreateNoWindow = true; // or false
p.StartInfo.ErrorDialog = false;
p.StartInfo.WindowStyle = /* any of possible values */;
p.StartInfo.UseShellExecute = true; // or false
You need to set the WorkingDirectory to the root folder where the executable is located.
var executablePath = .....;
var p = new Process();
p.StartInfo = new ProcessStartInfo(executablePath);
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(executablePath);
p.Start();
The default value of WorkingDirectory is %SYSTEMROOT%\System32. Many legacy applications have hard coded relative paths that resolve to full paths with said directory and will fail miserably when trying to read (or create) files where they are not supposed to.

Failing to run C# process as different user

I'm trying to run djoin.exe tool with System.Diagnostics.Process from a C# service using a different user (not as the service user).
The process returns code -1073741502.
In the event log I can see:
Application popup: djoin.exe - Application Error : The application was
unable to start correctly (0xc0000142). Click OK to close the
application.
No stderr or stdout.
Here is the process configurations I used:
ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = "/Provision /Domain domain.com /Machine PC12 /SaveFile NUL /printblob",
WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
FileName = "djoin.exe"
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow =true,
Domain = "domain.com",
UserName = "other-user",
Password = "***"
};
if (username!=null)
{
startInfo.Domain = domain;
startInfo.UserName = username;
startInfo.Password = ToSecureString(password);
}
p = new Process { StartInfo = startInfo };
p.Start();
When using the RUNAS command, everything works fine.
What is the problem?
Seems like it is a permissions issue. This can either be at the folder level of where the exe is located, or to do with the user that the process is running under.
To diagnose this, you can first go to the folder where the exe is located. Then right click and set the permissions to "everyone" with full control. Then try to run again and see if you get the same message.
Also when you run Visual studio, at the start, right click and run as administrator. I take it from your comment that this works OK, leading me to believe it is in fact permission related. e.g. Are the different users in the same domain? Once you work out the permissions of the folder where the applcation lives, create an account with permission on that folder and then have whatever process schedules/runs the exe to execute under that account.
Update - the comments above prompted another idea, you could use system.diagnostics to write eventlog entries at each point of the code, to help determine what is going wrong. Another tool that may be of use if WinDBG to get more info about what is throwing that exception.

Calling an exe file from web application

I have an exe file of a console application that takes parameter as:
A.exe --i=123 --o=abc
The out is a generic tsv file created at a predefined output folder.
For some reason I need to develop a web application which can call A.exe (such that input parameters are passed as request parameters) and return the content of the file as a web response.
Something like:
www.myserver.com/A?i=123?o=abc
Can someone suggests few pointers to get started?
You can try using Process as follows,
Process proc = new Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = Path.Combine(exePath, #"A.exe");
proc.StartInfo.Arguments = String.Format(#"-i = ""{0}"" -o = ""{1}""", "123", "abc");
proc.Start()
Note : Set proper permission to the application pool to run this application.
Make a dll of your exe and call it from asp.net handler .ashx
It is a better option.

launch console application from asp.net page on shared hosting

i need to launch simple console application from my asp.net webpage, that creates a file using given parameters. hereis my code:
string filePath = Server.MapPath(#"~/Patcher/TestPatcher.exe");
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(filePath , "-sn:123456789123456 -upd -fn:test.hd");
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(info);
p.WaitForExit(25000);
// Display the output
string str = p.StandardOutput.ReadToEnd();
problem is that program doesnt create any file, but it does launch - i got some output in "str". any suggestions? and btw, that website must work on shared hosting.
Yeah. I think you have a security issue there... you can't just run an exe from a sandboxed environment (which the browser is).
Besides the code snippet you provided, is trying to run code on the webserver, not on from the asp.net page.

Categories

Resources