Disable Windows service at application launch - c#

Because of VLC conflict I have to turn off Windows Advanced Text Services at my application launch. Is there any special API for that? Will it work for user with default rights?

The question is titled "Disable Windows service..." but the answers all tell how to stop a service.
Most of what you will find on Google is that you can update the registry to disable a service using something like this:
RegistryKey key = Registry.LocalMachine.OpenSubKey(#"SYSTEM\CurrentControlSet\Services\[YourServiceName]", true);
key.SetValue("Start", 4);
I haven't tried that method but it appears as though it will work. I also came up with a different method that uses sc.exe to disable the service:
Process sc = Process.Start("sc.exe", "config [YourServiceName] start= disabled");

ServiceController _ServiceController = new ServiceController([NameService]);
if (!_ServiceController.ServiceHandle.IsInvalid)
{
_ServiceController.Stop();
_ServiceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMilliseconds(uConstante.CtTempoEsperaRespostaServico));
}

You can use WMI for that.
Look here, for example: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=114

You can use the ServiceController class for this. There are code samples in the linked documentation page.

just execute "net stop service-name" to stop a service or "net start service-name" to start a service.
type "net start" in the console (cmd.exe) in order to list all services.
You need admin privileges in order to enable/disable services.

Related

How to keep "Allow Service to interact with desktop" checked by default at the time of windows service installation

I have created a windows service which should open and run a ABC.exe application. I know it is a bad practice to do this dont downvote for this reason coz i dont have any other option at the moment. i have done research about this a lot and found out a work around as making it interactive.i used this link: https://code.msdn.microsoft.com/windowsapps/CSCreateProcessAsUserFromSe-b682134e
but the problem i am facing right now is user have to manually set Local System Account and check the "Allow Service to interact with Desktop" box. all i want to do right now is set it checked by default at the time of installation. so when user installs the service it should be already checked and the service should be in the running state(dont have to manually start the service-i am done with this part). i have seen some similar post here but they all show way to start the service from external app like creating another console app and using system.management.managementobject. i want to know if there is any way to add some code in the service itself and where to add it?
ps.: i am creating this service for windows vista+ systems.
please help as i am stuck on this problem for quite a while. Thanks in advance.
What i suggest you do is modify the value directly in registry
In the installer class, override the OnCommitted(System.Collections.IDictionary savedState) method.
In that method, modify the registry key that ensures this setting, like so:
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
base.OnCommitted(savedState);
string regKey = "SYSTEM\\CurrentControlSet\\Services\\MyServiceName";
var key = Registry.LocalMachine.OpenSubKey(regKey, RegistryKeyPermissionCheck.ReadWriteSubTree)
?? Registry.LocalMachine.CreateSubKey(regKey, RegistryKeyPermissionCheck.ReadWriteSubTree);
// might need to check the result, should be hex 110 or Decimal 272
key.SetValue("Type", 272, RegistryValueKind.DWord);
}

Run C# app on Startup?

I have created a simple weather application and I added the code below to let the user let it run on Startup:
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (startupCheck.Checked) {
rk.SetValue("WeTile", "\"" + Application.ExecutablePath.ToString() + "\"");
} else {
rk.DeleteValue("WeTile", false);
}
Now this runs fine on both my computers. But when I gave the app to my girlfriend. She said the app does not run on windows start up. I read it online that it could be because of the user permission or the location so I told her to move the app to c:/ and try checking the box again and then restarting. Now it works but on every startup she has the default windows message saying you want to run this app?
How do I get rid of this? What is the best way to add to windows startup that works with both windows 32/64 bit without any user permission disruptions?
It sounds like you may have run afoul of Windows' file blocking security function. Applications created on another computer are automatically blocked from executing unless the user specifically "unblocks" the file. Have your girlfriend right-click on the executable, select "Properties" and see if there is a button at the bottom of the dialog to unblock the file.
Once unblocked, you should no longer see the confirmation prompt at startup.
You could add it to the Windows startup folder, check if it's not there already and if not, add it (assuming this is what the user wants).
See How do I set a program to launch at startup

Cant Access Registry even with Elevated Permission

I am making a Service Program where it will change the start up type of certain service.
My Current code does not even let me open the registry for writing, even when I "Run As Admin".
Code:
ServiceKey = Registry.LocalMachine;
ServiceKey = ServiceKey.OpenSubKey(#"SYSTEM\CurrentControlSet\services\" + ServiceName, true);
ServiceKey.SetValue("Start", 2, RegistryValueKind.DWord);
However, I get this error: Requested Registry Access is not allowed.
Anyone know a solution ?
(PS. I know there are other way's I could be doing this, but it's bother me how It's not letting me Access the registry.) My program is also running as Any CPU.
The way to change a service's configuration is not to whack the registry. You use the service control manager. MSDN even has a sample program that changes a service's start type. I found this page by going to About Services, then clicking Service Configuration Programs, then Service Configuration.
Found the problem, on some registry, if you check the "Permission", you will notice that not even an admin has the permission to change the registry. You can change the permission your self but on my case, I will find a different approach to editing the service start up.

How to Start multiple windows Services from asp.net web application?

I want to restart a set of windows services which are running on my local computer.
I want to restart all these services at once when i click a button in my asp.net application ?
I would appreciate if any one can help me out .
As of now i am able to implement the scenario for restarting a single windows service by using
Service Controller Class.
Just put a loop in iterating through each service name. Create a ServiceController for each service name and restart it there.
List<string> serviceList = //however you get all of the services you want to start, put them in here.
foreach(string serviceName in serviceList)
{
ServiceController controller = new ServiceController(serviceName);
....
controller.Restart();
}
To use a batch file use:
System.Diagnostics.Process.Start(pathToBatchFile);
You can manage the process by using intellisense and a little curiosity. Also, here is an msdn article I found for you.
http://blogs.msdn.com/b/csharpfaq/archive/2004/06/01/146375.aspx
To hide the command prompt and have more control over the process, use the System.Diagnostics.ProcessStartInfo class and pass its object to the Process.Start method. You can even catch the output from the batch file inside your program.

System.Diagnostics.Process.Start() cannot start process when called from Windows service

I am trying to start an external process from a .NET Windows service. In the past I have used the Process.Start() overload that takes the executable path and a command line string. This works. But now I would like to start the process and have it run in the context of a particular user. So I call this version of Start()
public static Process Start(
string fileName,
string userName,
SecureString password,
string domain)
However, when I call the method, the application I am trying to run generates an unhandled exception:
The application failed to initialize properly (0xc0000142). Click on OK to terminate the application.
I have tried to start different applications and they all generate the same exception. I have run the code outside of the Windows service and the application starts correctly.
So is there a way to get this to work in a Windows service?
Maybe the user has to have, "logon as a service" security right. This is done with the "local security policy" application. And/or "logon as a batch job".
This is very similar to this question here. The answer is usually due to security issues with the desktop and window station in which the process is being run. See this article for an explanation and some sample code.
This is just a shot in the dark, but perhaps you can try to run the Windows Service in Interactive mode. If that works, though, this can't be done in Windows Vista (because of Session 0 Isolation).
Use Filemon and see if it is trying to open a config file and not finding it. I once had this error due to a malformed config.

Categories