C# System.Diagnostics.Process doesn't work properly - c#

I am using System.Diagnostics.Process in a Windows service application. The application that I call uses DropBox API and Google Drive API. When I login on Google Drive or DropBox it is supposed to open the Web browser and allow access to Drop or Drive, but it doesn't open it. Other functions works properly (create folder on local computer, read files, write logfiles etc).
When I open this application manually with double click, the login process works properly, the web browser is shown and I can allow access.
Something similar happens with other application using Saraff Twain. If I open it manually it works properly, I can scan and save files (on this process some alerts or message box are shown, like a "scanning", "no paper", "no scanner" messages) but when the windows service call it no messages are shown, it scans and save files but without messages.
If I call this application from another windows form or console application the applications works properly.
I don't know what is the problem with the Windows service.
Thanks for your help.
Here is the code that I used in the Windows service app (here it fails), and Windows forms app (here it works fine). I have tried calling CMD.exe and application path as argument, and directly the path on file name.
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C" + " \"" + execName+"\" " + argument;
//p.StartInfo.Verb = "runas";
// writeLogLine(argument + " " + execName);
p.Start();
output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
writeLogLine("out" + output);

A Windows Service is not meant for user interaction as it should be capable of running without any users logged in.
Windows Services are isolated from the users' desktops as a security measure this is why any interaction with a desktop is bound to fail.
Either replace the Windows Service with a regular desktop application or change the Windows Service in such a way that it doesn't need desktop access.

Related

Process.Start() working but no window is popping up

I have a web API project that has a call that allows the user to basically start a separate application on the server.
Basically my web API is a gateway to remotely call this application from an MVC project.
Problem:
The problem I am facing is that the Process.Start() method is working perfectly (as in I can see the process starting on the server's task manager) but no window is popping up? I can run the application directly and see it start in its own window.
Web API Code:
public void ReconnectEPLAN()
{
if (CheckEplanConnection() == false)
{
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = #"C:\Program Files\EPLAN\Pro Panel\2.8.3\Bin\W3u.exe"; //works but no ui poopup
process.StartInfo.CreateNoWindow = false;
process.Start();
}
}
What can I do to force the started process's app window to appear as well?
On your server, IIS runs as a service (unlike IIS Express, which runs in the user space).
Since Windows Vista, services can no longer interact with the user's desktop directly.
See:
How to run console application from Windows Service?
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/f8f91e8f-5954-43a7-8bc4-80ed2ff1e3b1/quotallow-service-to-interact-with-desktopquot-does-not-work-on-vista?forum=windowssdk
https://www.codeproject.com/Questions/1239551/Why-does-process-start-goes-to-background-when-sta
Services cannot interact directly with the user at all: this is because services can run on a machine that doesn't have a user logged in at all, so there is no way to interact with them.

Converting .xls to .xlsx using excelcnv.exe or ofc.exe in C#

I am trying to run a 'EXE' using c# code to convert .xls to .xlsx. It is running successfully and working as expected if I run using a console application or cmd.
But when I am running it in server side (code hosted in IIS i.e code running using w3wp.exe worker process) it unable to convert files to .xlsx
First, I used Excelcnv.exe to convert
if (System.IO.File.Exists(pstrIPDTemplatePath))
{
string pstrOpenXMLIPDTemplatePath = pstrIPDTemplatePath.Replace(".xls", ".xlsx");
string processArguments = "-oice \"" + pstrIPDTemplatePath + "\" \"" + pstrOpenXMLIPDTemplatePath + "\"";
ProcessStartInfo Excelcnv = new ProcessStartInfo();
Excelcnv.FileName = mstrExcelCnvExePath;
Excelcnv.Arguments = processArguments;
Process.Start(Excelcnv);
}
It converted few files successfully but for few files it is creating excelcnv.exe*32 process in task manager, but it is not converting excel file. I even used event handlers for output, but the result is null. Process is going to indefinite state.
But if I am Running it through console application it converted the file successfully it also popped up a dialog after running it to convert multiple files that Excel did not launch correctly and asking whether to start in Safe Mode.
To avoid this safe mode issue, I tried to use ofc.exe
So, I used ofc.exe to convert as below:
Process prc = new Process();
prc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
prc.StartInfo.FileName = #"OFC.exe";
prc.StartInfo.Arguments = #"ofc.ini";
prc.StartInfo.UseShellExecute = false;
prc.StartInfo.RedirectStandardOutput = true;
prc.Start();
Same case with this code. It is working successfully and converting as expected when running via console application.
It is unable to convert to .xlsx but creating the process in task manager and log file is empty when running using w3wp process hosted in IIS.
Even after giving IIS_IUSRS permission to 'EXE' location, it is not working but its working in console application.
Any ideas why this is happening, and how to get around it?
Try to add your w3wp running user to 'Administrator' group. It will work.
Otherwise use this which is mentioned here

Process.Start doesn't work in IIS

I'm trying to print a PDF manually through Process.Start, but it isn't working in IIS. I copied the same code in a windows form application and that worked. I already tried giving the rights to 'Network Service' user (my application pool has Network Service permission). I've also followed the steps here:
IIS7 does not start my Exe file by Process Start
string file = #"C:\test.pdf";
string printer = "TestPrinter";
string processFilename = Microsoft.Win32.Registry.LocalMachine
.OpenSubKey("Software")
.OpenSubKey("Microsoft")
.OpenSubKey("Windows")
.OpenSubKey("CurrentVersion")
.OpenSubKey("App Paths")
.OpenSubKey("AcroRd32.exe")
.GetValue(String.Empty).ToString();
var info = new ProcessStartInfo();
info.FileName = processFilename;
info.Arguments = string.Format("/h /t \"{0}\" \"{1}\"", file, printer);
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
Process p = Process.Start(info);
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
int counter = 0;
while (!p.HasExited)
{
System.Threading.Thread.Sleep(1000);
counter += 1;
if (counter == 5) break;
}
if (!p.HasExited)
{
p.CloseMainWindow();
p.Kill();
}
Well, it took me three days and multiple ways and tools to test printing only to confirm that it's not a programming problem. It is a permission problem. When I was searching for tips, this particular problem seemed to be ongoing for more than a decade and it always happens to certain web server setups. So, here is the solution for anyone who stumbled upon the same situation as I had and save the three days of headache.
The setup:
Have a web application or service running on IIS server and you need to print some documents.
Have a network printer set up for this purpose, such as printing to a particular departmental printer within the office network.
The printing works when you are testing on localhost with your development machine.
The printing silently drops and nothing happens when deployed to the actual web server.
The reason is that a network printer is accessed in the form of \networkserver\printername and the web application account, IIS_IUSER, is not a domain account and, therefore, doesn’t have access to any network server.
Solutions:
Add the IIS_IUSER account to the domain BUT this is a very bad idea because you would need to give Internet users of your web app to have access to some network drive. Therefore, this is a no-no.
Add the printer using TCP/IP instead of setting it up as a network drive. By IP address, all local users, including the IIS_IUSER account will have access to the printer by default.
With an IP printer, no matter you use ProcessStartInfo or a third-party tool to print, it will work. Happy programming!

How to fix DirectoryNotFoundException while using Process in Web App

I'm using Pdf2Text in an ASP.NET web app. The web interface allows PDF files to be uploaded and converted to text. To convert to text, I use the C# function below, which relies on running the Pdf2Text program via the Process library.
void ExtractOCR(string input, string output)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = true;
startInfo.FileName = Server.MapPath("ocr/Pdf2Text.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = input + " " + output;
Process exeProcess;
using (exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
I've double-checked that the input and output paths are all valid. However, when I run the web app, I get the following error.
I've tried the Just-In-Time debugger but it won't even run for some reason. The Pdf2Text is a precompiled file, I don't have it's source code. I believe this is the file's download site, but not 100% sure. I've checked online to find solutions to similar errors but none has worked.
Thank you #GraDea for suggesting to look into the pool's permissions into the web app directory, this was the solution.
The web app was located at a custom location (not the default ASP server directory of inetpub\wwwroot). After the suggestion, I tried adding the pool user to the web app's custom location, but that didn't seem to work. Next, I moved the web app to the inetpub\wwwroot location and added the pool user to the folder, restarted the site via IIS and everything is now back to normal.
For future reference to anyone, easiest fix to a similar problem is to make sure your web app is in the default IIS server directory, and that your site pool's username is added to the application folder's security permissions. I've not tried the fix for a custom location because it's not so important for me, but I'm guessing it will most likely also involve adding the necessary IIS default pool users (e.g. IIS_IUSRS) and the site pool's user.

How to start the bat-file at Asp.net web server side?

I need to call a console application to load data into another desktop application on the remote server that located within the corporate domain.
Users will enter the web page and upload data to asp.net web server, which after transformation should call that console application. Users are located remotely and do not have any other access except the web server.
I decided to lower the security web application context and let the asp.net working process to start the console application on the current IIS 6.0 web server
What I have done:
I changed the security account for the application pool for Local System;
I added ASPNET Account and IIS_WPG IIS Process Account to Administrators group;
I added “Allow service to interact with desctop” for “IIS Admin Service” and “World Wide Web Publishing Service” processes and restarted the machine;
I tried to start BAT-file at server side through the test page code-behind, but failed:
protected void btnStart_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = #”C:\run.bat”;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();
}
The error was access denied.
Please help me to find any workable idea how to start the bat-file at web server side.
Thanks
Try setting UseShellExecute to true instead of false. After all, batch files run in a shell - so you need a shell to execute it. (Another option is to run cmd.exe and pass the name of the batch file in as an argument, e.g. "cmd.exe /k c:\run.bat")
You might also want to try creating a simple .NET app which just (say) creates a file with a timestamp in. That way you can test the "can I start another process" bit separately from the "can I get the batch file to work" bit.
Put that particular batch file in your application itself.
string str_Path = Server.MapPath(".") + "\\run.bat";
ProcessStartInfo processInfo = new ProcessStartInfo(str_Path);
processInfo.UseShellExecute = false;
Process batchProcess = new Process();
batchProcess.StartInfo = processInfo;
batchProcess.Start();
Take a look at this example: Run Interactive Command Shell or Batch Files From ASP.NET
It uses little different approach. They suggest running cmd.exe and executing command line by line.

Categories

Resources