Trying to control open application, instead opening new application - c#

Was tough to make a title that made sense.
So I am working with SalesLogix CRM and trying to navigate to a contact in said CRM. Saleslogix allows you to use almost like a url that looks like slx:CONTACT//C6UJ9A006S96
That line will direct the CRM to the contact with that ID.
My problem is when I try to navigate to that url I open a new SalesLogix instance instead of using the current one.
Saleslogix is a desktop based software just for your information.
The strange part for me is that if I use Windows' 'Run...' from the start menu and put in slx:CONTACT//C6UJ9A006S96 then it will use the already open application, same with a normal command prompt, however if I use an Admin command prompt it will open a new instance of SalesLogix. I checked the task manager and it doesn't matter if I open it using the admin command prompt or just the desktop window, it will say it is running under my current user and not system or anything weird so it seems the user isn't the problem.
My code is:
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.ProcessName == "SalesLogix")
{
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = #"C:\Program Files (x86)\SalesLogix\SalesLogix.exe";
p.StartInfo.Arguments = "slx:CONTACT//C6UJ9A006S96";
p.Start();
}
}
Any help would be appreciated and if I didn't explain my problem clearly enough I would be happy to clear up any confusion.

Try running the URL directly. As in, don't execute the SalesLogix exe, just pass the URI as the filename and let the Windows protocol handler take care of it.

What i think you are doing here is taking the process that is already running, rewriting its parameters and starting it all new again. Did you try simply calling the new process with those parameters to see if it would attach to the existing system process ?

Related

How to open an application in the current user from a service?

I have a remote computer that is always opened with a user, because I am using it as video player in which I have installed Kodi.
In this computer, I have a gRPC service installed, because I would like to can open some applications, close them and do a basic management from a mobile.
This gRPC service is hosted in a ASP Core application, that is installed as windows service. This services is running as SYSTEM user.
I am doing a test trying to open the notepad application, and it is opened, but as system user, so I can't see it in the user that is opened.
My question is, is it possible to open the notepad, or any other application, from this service and open in in the user that has the session opened to can see the notepad?
Perhaps one solution could be run the grpc service with this user, but I prefer to use the system user because some actions will need their permissions.
To open the notepad I am using this code:
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = string.Empty;
start.FileName = "notepad";
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
using (Process? proc = Process.Start(start))
{
if (proc == null)
{
throw new ArgumentException("Handle error");
}
}
Thanks.
According to documentation, the ProcessStartInfo class has settable properties for UserName and Password, which seems to me like the obvious solution to your problem (I Haven't tested that myself, though).
You can even use the PasswordInClearText property though I think using a SecureString would probably be safer.

Starting a process

As part of SharePoint automation testing, I am trying to open Internet Explorer as another user by using the System.Diagnostics.Process . Here is the following C# code
System.Diagnostics.Process p = new System.Diagnostics.Process();
// Domain and User Name:
p.StartInfo.Domain = "MYDOMAIN";
p.StartInfo.UserName = "myusername";
// Command to execute and arguments:
p.StartInfo.FileName = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";
p.StartInfo.Arguments = "http://url/AllItems.aspx";
// Build the SecureString password...
System.String rawPassword = "thepassword";
System.Security.SecureString encPassword = new System.Security.SecureString();
foreach (System.Char c in rawPassword)
{
encPassword.AppendChar(c);
}
p.StartInfo.Password = encPassword;
// The UseShellExecute flag must be turned off in order to supply a password:
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;
p.Start();
When I run this automated test Visual Studio returns informing me that the test was successful, however Internet Explorer does not open.
Is there something in my code I am missing in order for a window to appear? There is no iexplore process running prior to the test being run.
putting double quotes around the file path (since it contains spaces) may help:
p.StartInfo.FileName = "\"C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe\""
^^ ^^
In addition, if your intention is to start this from a service process or dll running in a service such as "SharePoint", then this code will probably not launch the process in the desktop desired. You'll need to set the desktop to "winsta0\\default" in the startup info.
To run a process the worker process should have high privileges and this is not an ideal case in any web application. If your purpose is to use IE for unit testing then I would consider using something like WaitIN. If your purpose is for application logic to access a URL and do something then consider using HttpWebRequest. If you still need a process to be started then create a Windows Service and then expose a web call so in Share Point you can just make a call and your Windows Service can run on local account or some other high privilege account.
Hope this helps and please provide the scenario why you want to start the IE and that can give you a better answer in the forum.

Printing by executing a process in a Windows Service

I have a Windows Service that needs to start a process to send a file to the printer (I found that solution there https://stackoverflow.com/a/4875755/1228738) . I do this using the Process.Start().
My problem is that nothing happens.
The service is actually installed on my developer machine (win7, x64). I tried installing it as LOCAL SYSTEM, NETWORK SERVICE, LOCAL SERVICE with the same result every time.
I tried those way of starting my process :
Process p = new Process();
p.StartInfo.FileName = "C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe";
p.StartInfo.Arguments = "-p myFile.pdf";
p.Start();
and
Process.Start("C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe", "-p myFile.pdf");
and also
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe";
startInfo.Arguments = "-p myFile.pdf";
Process.Start(startInfo);
When I execute the same code in a winform application, everything works fine, the file is sent to the printer. But in the Windows Service, nothing happens.
I saw that post https://stackoverflow.com/a/6271309/1228738, which explains why I would not see the UI, that's fine I don't have any UI anyway. But as said in the comment section, a process with no user input should be OK. The process that I start don't need any user input.
The only thing I can think of right now, is that because of session isolation (https://stackoverflow.com/a/5063750/1228738), the service can't find any installed printers... Can that be the case ? If so, any suggestion how to work around that ? And if not, any idea of what's wrong ?
Thanks!
EDIT #1
I tried running the service with my user account, and it's working, so I guess my fears are confirmed... the users LOCAL SYSTEM and NETWORK SERVICE have no installed printers.
So I'll refine my question a little bit. Is there a way for those account to access printers installed on the computer ?
EDIT #2
We finally decided that a user will be created for running that service and in that user accounts we'll install the printer on which to print.
I guess this question can be closed now.
Thank you all for your help.
I had this issue too, this trick solved it
Go to services ---> Double click the required service ---> proceed to logon tab
Supply the Log-in credentials from which printer was installed.
Run your service, then check the printer queue.
Reason: Local system account does not have those printer installed !
See screen shot below.
Check out this MSDN Page: http://support.microsoft.com/kb/324565
According to this page, you cannot print from ASP.NET pages or Windows services using .NET.
The solution here is tho share your local printer and call Foxit with
-/t yourfile.pdf \\localhost\YourSharedPrinter
That way your service does not need an UserProfile and no DefaultPrinter.

Gaining control over process started by an URI in C#

I'm making a program which opens a configured application after with the passed paramters through an url with shell execute. I achieved this with the following:
ProcessStartInfo procinfo = new ProcessStartInfo(URI);
procinfo.UseShellExecute = true;
Process App = Process.Start(procinfo);
I want to kill this process later after some minutes through this project that I could do by App.Kill() but the problem is that the Process.Start() always returns null if I pass the URI. How could I reach that process?
If the address of the executable file to start is a URL, the process
is not started and null is returned.
http://msdn.microsoft.com/en-us/library/53ezey2s.aspx
Even if you specify a browser, the process could be a simple handler that sends a message to an existing process (or opens another process) and closes itself immediately.
But you may explicitly launch the browser you want if you know how its process works.
Process p = Process.Start(browserExePath, url);

How to run a server-side application via ASP.Net(C# or VB)

I wanted to build a front end web page so that I can start and stop various programs on my server remotely. I know that Shell will open something locally, but how can I make an ASP.Net page which activates programs server-side? Googling got me the "Shell" method, but I don't think that works server-side. Any ideas?
Take a look at the System.Diagnostics.Process class. It can allow you to start and stop an executable on the server.
You would have to impersonate an account that has sufficient privileged to run the application, though. You can use the UserName and Password properties of the System.Diagnostics.ProcessStartInfo object that you pass to Process.Start.
Edit
For an example, you could do the following:
var startInfo = new ProcessStartInfo("C:\MyServerApp.exe", "/A /B /C");
startInfo.UserName = "LocalUser";
// It's a bad idea to hard code the password in the app, this is just an example
startInfo.Password = "SomePass";
var process = Process.Start(startInfo);
// Not a good idea to wait in a web app, but you can until the process completes
process.WaitForExit();

Categories