Everything I start from a worker role has no interface, why? - c#

I am having a very bad moment executing a process from internet explorer.. I am supposed to call internet explorer from a worker role process to perform some operations..but internet explorer is executing with no visible interface...
This is my code :
psi.FileName = "D:\\Program Files\\Internet Explorer\\iexplore.exe";
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.UseShellExecute = false;
psi.WindowStyle = ProcessWindowStyle.Maximized;
psi.RedirectStandardOutput = false;
psi.RedirectStandardInput = false;
psi.RedirectStandardError = true;
psi.Arguments = fileLocation;
exeProcess = Process.Start(psi);
started = true;
exeProcess.PriorityClass = ProcessPriorityClass.RealTime;
Can you please help me? I need to execute internet explorer with a visible interface.

Running process that normally requires completely functioning Windows desktop is unlikely to run correctly from service as there is no UI/windows associated with services. You should be able to reproduce the same behavior by trying to instantiate such application from a service (i.e. IIS) on local machine running under some service account.
If you need to render web pages on server it would be better to either use external service or specifically designed "headless browser" like PhantomJS.

Related

Dial VPN connection from a Windows C# service

I am trying to develop a Windows service using C# and Visual Studio 2017. I would like to dial up a pre configured VPN connection from within the service.
If I were to place the following line of code in a C# Windows Desktop app it will quickly flash a CMD prompt and dial the VPN:
System.Diagnostics.Process.Start("rasdial.exe", "ConnectionName user password");
But it does not work in the service. I have brought it to this point. It does not throw an error, it does not dial a connection. It just seems to do nothing as i step through it.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo("rasdial.exe", "ConnectionName user password");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.Start()
Not sure how to fix this or what the next steps would be. An API would be nice if there was one.
I have done some Googling about this but cannot discover anything so i thought i would take it to the experts.
Thanks.
Stephen Simpson
Ken White pointed out that service accounts don't have the same privileges as normal user accounts. I solved the problem by running the service under a specific user.

Execute batch file in client page

I have the below command which run batch file, I need to run this batch when I open the ASPX page in the machine browser to affect the machine and not affect server :
ProcessStartInfo psi = new ProcessStartInfo(this.WhiteLabel.Text);
psi.RedirectStandardOutput = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = true;
Process.Start(psi);
This is not possible because of security concerns. The only possible way might be to create an ActiveX library that the user would acknowledge and accept on their browser to run. What are you trying to run on the client? Maybe there is another approach?
This has been asked before see here...
How to execute an application on the client from a website?

How to call buggy .dll in new process?

I have a c# windows service application that is crashing without throwing an exception when processing certain files using a third-party .dll. What I decided to do was create a new console application which replicates a small portion of the windows service code, particularly the part the causes the service to crash. What I want to do is call the new .exe program from the windows service, and if it crashes, I throw an exception myself.
So, I need to call this .exe program (not in the background as I can't allow the windows service to continue until I know the file to be processed is safe), and then determine if it exited successfully or not. How do I go about doing this? The examples I've seen run the .exe in a background process which is not what I want.
Thanks.
Look at this SO answer how to run console application from windows service. Just add WaitForExit, like this :
ProcessStartInfo info = new ProcessStartInfo(#"c:\myprogram.exe");
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.CreateNoWindow = true;
info.ErrorDialog = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(info);
process.WaitForExit();
In console application you can set exit code if you exit with Environment.Exit(statusCode) or return int value from main function of console applicaiton. Or you can write to output and then in your service examine exit code (process.ExitCode) or output stream so you can determine is process was exited successfully.

Add ports with netsh in WCF as domain admin without admin privileges

I have a service with WCF in a WPF application (self-hosted) and I have the typical error "Your process does not have access rights to this namespace".
The users can’t have admin privileges so using a .manifest is not a solution.
The ports are dynamic, the application calculate a free port every time is running, so the application must insert the listen port by netsh several times
I use a ProcessStartInfo with domain administrator, but to start the process the user needs admin privileges.
Run the application as administrator neither is a solution, so I need that a normal user can run the application and the program add the port by netsh as domain administrator.
My Process is something like this:
ProcessStartInfo psi = new ProcessStartInfo("netsh", parameter);
SecureString ss = new SecureString();
for (int i = 0; i < adminPass.Length; i++)
ss.AppendChar(adminPass[i]);
psi.Password = ss;
psi.UserName = Admin;
psi.Domain = Domain;
psi.Verb = "runas";
psi.RedirectStandardOutput = false;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
Process.Start(psi);
Thanks a lot
Take a look at the accepted answer for this Stack Overflow question for a possible solution to your problem. The approach outlined in the answer is to break out the admin-requiring code out into a Windows service which performs the elevated privilege operations under an appropriate (separate) account when invoked.

Windows SDK - C# - Debugging process exiting with error code -1073741502

SHORT VERSION
How do you figure out which DLL is failing to load (and potentially why) when a process exits with error code -1073741502?
LONG VERSION
I'm trying to write a pretxnchangegroup hook for Mercurial, and as a part of that hook I need to get the output of running the command:
hg log
The code that I'm using to start and run the hg.exe process is as follows:
string Command = "log";
Process p = new Process();
ProcessStartInfo psi = p.StartInfo;
p.StartInfo.FileName = #"C:\Program Files (x86)\Mercurial\hg.exe";
psi.CreateNoWindow = true;
psi.LoadUserProfile = true;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.WorkingDirectory = Environment.CurrentDirectory;
p.StartInfo.Arguments = Command;
// Pass-through environment variables
psi.UserName = Properties.Settings.Default.HG_User;
psi.Domain = Properties.Settings.Default.HG_Domain;
psi.Password = new System.Security.SecureString();
foreach (char c in Properties.Settings.Default.HG_Pass)
{
psi.Password.AppendChar(c);
}
p.Start();
p.WaitForExit();
The problem is that the process keeps exiting with error code -1073741502, without outputting anything on Standard Output or Standard Error. After some research online, I discovered that this error code has something to do with the application failing to initialize properly (couldn't find DLL's, maybe?), but I have no idea how to go about figuring out how to fix it.
Keep in mind that this hook is being called for when I'm pushing to the repository over the web (so, IIS is calling the Mercurial CGI via Python, which has this program configured as a hook).
In a totally different web application, I'm able to run HG commands just fine, and I'm also able to run this by doing
runas /user:<same account as in the code> /noprofile cmd.exe and then manually typing in the hg command line.
Also, if I set UseShellExecute = true, then it executes just fine, but then I can't get the Standard Output. I'm really tempted to just make a web service call to the web app which is able to execute this command successfully, but that'd be a really ugly solution.
Any ideas why this thing isn't executing?
I was able to resolve this by disabling UAC so it sounds like a permissions problem even though I do not know the exact details.

Categories