Installing a Windows Service Programmatically 
November 15th, 2005, 10:57 AM
| | |
Hi all:
I read an article from http://www.c-sharpcorner.com/Code/20...mmatically.asp
about how to install a windows service programmatically.
Based ont the code sample, it provides the feature to install service
under LocalSystem account.
What I need is to install service under some other certian account. By
further studying the code, and MSDN
( http://msdn.microsoft.com/library/de...-us/dllproc/ba
se/createservice.asp) ,
public static extern IntPtr CreateService(IntPtr SC_HANDLE,string
lpSvcName,string lpDisplayName,
int dwDesiredAccess,int dwServiceType,int dwStartType,int
dwErrorControl,string lpPathName,
string lpLoadOrderGroup,int lpdwTagId,string lpDependencies,string
lpServiceStartName,string lpPassword);
is the core API to create a service. And the last two parameters should be
name and password of account under wihcih service should run. But when I
tried to pass values to these two parameters bu calling:
sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
servicePath, null, 0, null, _UserName, _Password);
This piece of code is just a bit different from original, but sv_handle
return is 0, which means failure.
Anybody has idea how to install a service under some certain account?
Thanks for any suggestion.
--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy | 
November 15th, 2005, 10:57 AM
| | | | re: Installing a Windows Service Programmatically
Hardy,
If the return value is IntPtr.Zero, then throw a new instance of the
Win32Exception class. It will detail what the error from the call to
GetLastError is and it will give you more information about what went wrong
with the call.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com
"Hardy Wang" <hardy_wang@marketrend.com> wrote in message
news:Oo$9vktdDHA.2804@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hi all:
> I read an article from
>[/color] http://www.c-sharpcorner.com/Code/20...mmatically.asp[color=blue]
> about how to install a windows service programmatically.
> Based ont the code sample, it provides the feature to install service
> under LocalSystem account.
> What I need is to install service under some other certian account. By
> further studying the code, and MSDN
>[/color]
( http://msdn.microsoft.com/library/de...-us/dllproc/ba[color=blue]
> se/createservice.asp) ,
> public static extern IntPtr CreateService(IntPtr SC_HANDLE,string
> lpSvcName,string lpDisplayName,
> int dwDesiredAccess,int dwServiceType,int dwStartType,int
> dwErrorControl,string lpPathName,
> string lpLoadOrderGroup,int lpdwTagId,string lpDependencies,string
> lpServiceStartName,string lpPassword);
> is the core API to create a service. And the last two parameters should be
> name and password of account under wihcih service should run. But when I
> tried to pass values to these two parameters bu calling:
> sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
> SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> servicePath, null, 0, null, _UserName, _Password);
> This piece of code is just a bit different from original, but sv_handle
> return is 0, which means failure.
>
> Anybody has idea how to install a service under some certain account?
>
>
> Thanks for any suggestion.
>
>
> --
> WWW: http://hardywang.1accesshost.com
> ICQ: 3359839
> yours Hardy
>
>[/color] | 
November 15th, 2005, 10:57 AM
| | | | re: Installing a Windows Service Programmatically
I adding some code like:
sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
servicePath, null, 0, null, _UserName, _Password);
if (sv_handle.ToInt32() == 0) {
CloseServiceHandle(sc_handle);
int err = GetLastError();
return false;
}
I tried to capture the error by reading return from GetLastError(), I got
997, where to find the definition of error code?
--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
"Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com> wrote
in message news:eWiePotdDHA.1532@TK2MSFTNGP10.phx.gbl...[color=blue]
> Hardy,
>
> If the return value is IntPtr.Zero, then throw a new instance of the
> Win32Exception class. It will detail what the error from the call to
> GetLastError is and it will give you more information about what went[/color]
wrong[color=blue]
> with the call.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - nicholas.paldino@exisconsulting.com
>
> "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> news:Oo$9vktdDHA.2804@TK2MSFTNGP11.phx.gbl...[color=green]
> > Hi all:
> > I read an article from
> >[/color]
>[/color] http://www.c-sharpcorner.com/Code/20...mmatically.asp[color=blue][color=green]
> > about how to install a windows service programmatically.
> > Based ont the code sample, it provides the feature to install[/color][/color]
service[color=blue][color=green]
> > under LocalSystem account.
> > What I need is to install service under some other certian account.[/color][/color]
By[color=blue][color=green]
> > further studying the code, and MSDN
> >[/color]
>[/color]
( http://msdn.microsoft.com/library/de...-us/dllproc/ba[color=blue][color=green]
> > se/createservice.asp) ,
> > public static extern IntPtr CreateService(IntPtr SC_HANDLE,string
> > lpSvcName,string lpDisplayName,
> > int dwDesiredAccess,int dwServiceType,int dwStartType,int
> > dwErrorControl,string lpPathName,
> > string lpLoadOrderGroup,int lpdwTagId,string[/color][/color]
lpDependencies,string[color=blue][color=green]
> > lpServiceStartName,string lpPassword);
> > is the core API to create a service. And the last two parameters should[/color][/color]
be[color=blue][color=green]
> > name and password of account under wihcih service should run. But when I
> > tried to pass values to these two parameters bu calling:
> > sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
> > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > servicePath, null, 0, null, _UserName, _Password);
> > This piece of code is just a bit different from original, but sv_handle
> > return is 0, which means failure.
> >
> > Anybody has idea how to install a service under some certain[/color][/color]
account?[color=blue][color=green]
> >
> >
> > Thanks for any suggestion.
> >
> >
> > --
> > WWW: http://hardywang.1accesshost.com
> > ICQ: 3359839
> > yours Hardy
> >
> >[/color]
>
>[/color] | 
November 15th, 2005, 10:57 AM
| | | | re: Installing a Windows Service Programmatically
Hardy,
You should never call GetLastError yourself, as it is not guaranteed to
be accurate. The reason for this is that the runtime makes API calls
between your API call and it might set the error state on its own. Because
of this, you should call the static GetLastWin32Error method on the Marshal
class.
However, there is a class that does all of this, the Win32Exception
class in the System.ComponentModel namespace. You should make your call
like this:
// Call CreateService.
IntPtr sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL, servicePath, null, 0, null, _UserName, _Password);
// Throw an exception if there is no handle.
try
{
if (sv_handle == IntPtr.Zero)
// Throw an exception.
throw new Win32Exception();
}
finally
{
CloseServiceHandle(sc_handle);
}
This should give you the description of the error. You can also look in
WINERROR.H to find the error the code represents.
--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com
"Hardy Wang" <hardy_wang@marketrend.com> wrote in message
news:%23MNLettdDHA.1460@TK2MSFTNGP10.phx.gbl...[color=blue]
> I adding some code like:
> sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
> SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> servicePath, null, 0, null, _UserName, _Password);
>
> if (sv_handle.ToInt32() == 0) {
> CloseServiceHandle(sc_handle);
> int err = GetLastError();
> return false;
> }
>
> I tried to capture the error by reading return from GetLastError(), I got
> 997, where to find the definition of error code?
>
> --
> WWW: http://hardywang.1accesshost.com
> ICQ: 3359839
> yours Hardy
> "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com>[/color]
wrote[color=blue]
> in message news:eWiePotdDHA.1532@TK2MSFTNGP10.phx.gbl...[color=green]
> > Hardy,
> >
> > If the return value is IntPtr.Zero, then throw a new instance of the
> > Win32Exception class. It will detail what the error from the call to
> > GetLastError is and it will give you more information about what went[/color]
> wrong[color=green]
> > with the call.
> >
> > Hope this helps.
> >
> >
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - nicholas.paldino@exisconsulting.com
> >
> > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > news:Oo$9vktdDHA.2804@TK2MSFTNGP11.phx.gbl...[color=darkred]
> > > Hi all:
> > > I read an article from
> > >[/color]
> >[/color]
>[/color] http://www.c-sharpcorner.com/Code/20...mmatically.asp[color=blue][color=green][color=darkred]
> > > about how to install a windows service programmatically.
> > > Based ont the code sample, it provides the feature to install[/color][/color]
> service[color=green][color=darkred]
> > > under LocalSystem account.
> > > What I need is to install service under some other certian[/color][/color][/color]
account.[color=blue]
> By[color=green][color=darkred]
> > > further studying the code, and MSDN
> > >[/color]
> >[/color]
>[/color]
( http://msdn.microsoft.com/library/de...-us/dllproc/ba[color=blue][color=green][color=darkred]
> > > se/createservice.asp) ,
> > > public static extern IntPtr CreateService(IntPtr SC_HANDLE,string
> > > lpSvcName,string lpDisplayName,
> > > int dwDesiredAccess,int dwServiceType,int dwStartType,int
> > > dwErrorControl,string lpPathName,
> > > string lpLoadOrderGroup,int lpdwTagId,string[/color][/color]
> lpDependencies,string[color=green][color=darkred]
> > > lpServiceStartName,string lpPassword);
> > > is the core API to create a service. And the last two parameters[/color][/color][/color]
should[color=blue]
> be[color=green][color=darkred]
> > > name and password of account under wihcih service should run. But when[/color][/color][/color]
I[color=blue][color=green][color=darkred]
> > > tried to pass values to these two parameters bu calling:
> > > sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
> > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > > servicePath, null, 0, null, _UserName, _Password);
> > > This piece of code is just a bit different from original, but[/color][/color][/color]
sv_handle[color=blue][color=green][color=darkred]
> > > return is 0, which means failure.
> > >
> > > Anybody has idea how to install a service under some certain[/color][/color]
> account?[color=green][color=darkred]
> > >
> > >
> > > Thanks for any suggestion.
> > >
> > >
> > > --
> > > WWW: http://hardywang.1accesshost.com
> > > ICQ: 3359839
> > > yours Hardy
> > >
> > >[/color]
> >
> >[/color]
>
>[/color] | 
November 15th, 2005, 10:57 AM
| | | | re: Installing a Windows Service Programmatically
I tried your codes, got error message "Control ID not found".
--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
"Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com> wrote
in message news:e$jjjytdDHA.1448@TK2MSFTNGP12.phx.gbl...[color=blue]
> Hardy,
>
> You should never call GetLastError yourself, as it is not guaranteed[/color]
to[color=blue]
> be accurate. The reason for this is that the runtime makes API calls
> between your API call and it might set the error state on its own.[/color]
Because[color=blue]
> of this, you should call the static GetLastWin32Error method on the[/color]
Marshal[color=blue]
> class.
>
> However, there is a class that does all of this, the Win32Exception
> class in the System.ComponentModel namespace. You should make your call
> like this:
>
> // Call CreateService.
> IntPtr sv_handle = CreateService(sc_handle, serviceName,[/color]
serviceDisplayName,[color=blue]
> SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
> SERVICE_ERROR_NORMAL, servicePath, null, 0, null, _UserName, _Password);
>
> // Throw an exception if there is no handle.
> try
> {
> if (sv_handle == IntPtr.Zero)
> // Throw an exception.
> throw new Win32Exception();
> }
> finally
> {
> CloseServiceHandle(sc_handle);
> }
>
> This should give you the description of the error. You can also look[/color]
in[color=blue]
> WINERROR.H to find the error the code represents.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - nicholas.paldino@exisconsulting.com
>
> "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> news:%23MNLettdDHA.1460@TK2MSFTNGP10.phx.gbl...[color=green]
> > I adding some code like:
> > sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
> > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > servicePath, null, 0, null, _UserName, _Password);
> >
> > if (sv_handle.ToInt32() == 0) {
> > CloseServiceHandle(sc_handle);
> > int err = GetLastError();
> > return false;
> > }
> >
> > I tried to capture the error by reading return from GetLastError(), I[/color][/color]
got[color=blue][color=green]
> > 997, where to find the definition of error code?
> >
> > --
> > WWW: http://hardywang.1accesshost.com
> > ICQ: 3359839
> > yours Hardy
> > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com>[/color]
> wrote[color=green]
> > in message news:eWiePotdDHA.1532@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > Hardy,
> > >
> > > If the return value is IntPtr.Zero, then throw a new instance of[/color][/color][/color]
the[color=blue][color=green][color=darkred]
> > > Win32Exception class. It will detail what the error from the call to
> > > GetLastError is and it will give you more information about what went[/color]
> > wrong[color=darkred]
> > > with the call.
> > >
> > > Hope this helps.
> > >
> > >
> > > --
> > > - Nicholas Paldino [.NET/C# MVP]
> > > - nicholas.paldino@exisconsulting.com
> > >
> > > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > > news:Oo$9vktdDHA.2804@TK2MSFTNGP11.phx.gbl...
> > > > Hi all:
> > > > I read an article from
> > > >
> > >[/color]
> >[/color]
>[/color] http://www.c-sharpcorner.com/Code/20...mmatically.asp[color=blue][color=green][color=darkred]
> > > > about how to install a windows service programmatically.
> > > > Based ont the code sample, it provides the feature to install[/color]
> > service[color=darkred]
> > > > under LocalSystem account.
> > > > What I need is to install service under some other certian[/color][/color]
> account.[color=green]
> > By[color=darkred]
> > > > further studying the code, and MSDN
> > > >
> > >[/color]
> >[/color]
>[/color]
( http://msdn.microsoft.com/library/de...-us/dllproc/ba[color=blue][color=green][color=darkred]
> > > > se/createservice.asp) ,
> > > > public static extern IntPtr CreateService(IntPtr[/color][/color][/color]
SC_HANDLE,string[color=blue][color=green][color=darkred]
> > > > lpSvcName,string lpDisplayName,
> > > > int dwDesiredAccess,int dwServiceType,int dwStartType,int
> > > > dwErrorControl,string lpPathName,
> > > > string lpLoadOrderGroup,int lpdwTagId,string[/color]
> > lpDependencies,string[color=darkred]
> > > > lpServiceStartName,string lpPassword);
> > > > is the core API to create a service. And the last two parameters[/color][/color]
> should[color=green]
> > be[color=darkred]
> > > > name and password of account under wihcih service should run. But[/color][/color][/color]
when[color=blue]
> I[color=green][color=darkred]
> > > > tried to pass values to these two parameters bu calling:
> > > > sv_handle = CreateService(sc_handle, serviceName,[/color][/color][/color]
serviceDisplayName,[color=blue][color=green][color=darkred]
> > > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > > > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > > > servicePath, null, 0, null, _UserName, _Password);
> > > > This piece of code is just a bit different from original, but[/color][/color]
> sv_handle[color=green][color=darkred]
> > > > return is 0, which means failure.
> > > >
> > > > Anybody has idea how to install a service under some certain[/color]
> > account?[color=darkred]
> > > >
> > > >
> > > > Thanks for any suggestion.
> > > >
> > > >
> > > > --
> > > > WWW: http://hardywang.1accesshost.com
> > > > ICQ: 3359839
> > > > yours Hardy
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color] | 
November 15th, 2005, 10:57 AM
| | | | re: Installing a Windows Service Programmatically
Hardy,
Are you sure that this is the error that the call to CreateService gave?
If you replace the "throw" part with a call to GetLastWin32Error, what does
that return?
--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com
"Hardy Wang" <hardy_wang@marketrend.com> wrote in message
news:uuPxP2tdDHA.1712@tk2msftngp13.phx.gbl...[color=blue]
> I tried your codes, got error message "Control ID not found".
>
> --
> WWW: http://hardywang.1accesshost.com
> ICQ: 3359839
> yours Hardy
> "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com>[/color]
wrote[color=blue]
> in message news:e$jjjytdDHA.1448@TK2MSFTNGP12.phx.gbl...[color=green]
> > Hardy,
> >
> > You should never call GetLastError yourself, as it is not guaranteed[/color]
> to[color=green]
> > be accurate. The reason for this is that the runtime makes API calls
> > between your API call and it might set the error state on its own.[/color]
> Because[color=green]
> > of this, you should call the static GetLastWin32Error method on the[/color]
> Marshal[color=green]
> > class.
> >
> > However, there is a class that does all of this, the Win32Exception
> > class in the System.ComponentModel namespace. You should make your call
> > like this:
> >
> > // Call CreateService.
> > IntPtr sv_handle = CreateService(sc_handle, serviceName,[/color]
> serviceDisplayName,[color=green]
> > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
> > SERVICE_ERROR_NORMAL, servicePath, null, 0, null, _UserName, _Password);
> >
> > // Throw an exception if there is no handle.
> > try
> > {
> > if (sv_handle == IntPtr.Zero)
> > // Throw an exception.
> > throw new Win32Exception();
> > }
> > finally
> > {
> > CloseServiceHandle(sc_handle);
> > }
> >
> > This should give you the description of the error. You can also[/color][/color]
look[color=blue]
> in[color=green]
> > WINERROR.H to find the error the code represents.
> >
> >
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - nicholas.paldino@exisconsulting.com
> >
> > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > news:%23MNLettdDHA.1460@TK2MSFTNGP10.phx.gbl...[color=darkred]
> > > I adding some code like:
> > > sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
> > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > > servicePath, null, 0, null, _UserName, _Password);
> > >
> > > if (sv_handle.ToInt32() == 0) {
> > > CloseServiceHandle(sc_handle);
> > > int err = GetLastError();
> > > return false;
> > > }
> > >
> > > I tried to capture the error by reading return from GetLastError(), I[/color][/color]
> got[color=green][color=darkred]
> > > 997, where to find the definition of error code?
> > >
> > > --
> > > WWW: http://hardywang.1accesshost.com
> > > ICQ: 3359839
> > > yours Hardy
> > > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com>[/color]
> > wrote[color=darkred]
> > > in message news:eWiePotdDHA.1532@TK2MSFTNGP10.phx.gbl...
> > > > Hardy,
> > > >
> > > > If the return value is IntPtr.Zero, then throw a new instance of[/color][/color]
> the[color=green][color=darkred]
> > > > Win32Exception class. It will detail what the error from the call[/color][/color][/color]
to[color=blue][color=green][color=darkred]
> > > > GetLastError is and it will give you more information about what[/color][/color][/color]
went[color=blue][color=green][color=darkred]
> > > wrong
> > > > with the call.
> > > >
> > > > Hope this helps.
> > > >
> > > >
> > > > --
> > > > - Nicholas Paldino [.NET/C# MVP]
> > > > - nicholas.paldino@exisconsulting.com
> > > >
> > > > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > > > news:Oo$9vktdDHA.2804@TK2MSFTNGP11.phx.gbl...
> > > > > Hi all:
> > > > > I read an article from
> > > > >
> > > >
> > >[/color]
> >[/color]
>[/color] http://www.c-sharpcorner.com/Code/20...mmatically.asp[color=blue][color=green][color=darkred]
> > > > > about how to install a windows service programmatically.
> > > > > Based ont the code sample, it provides the feature to install
> > > service
> > > > > under LocalSystem account.
> > > > > What I need is to install service under some other certian[/color]
> > account.[color=darkred]
> > > By
> > > > > further studying the code, and MSDN
> > > > >
> > > >
> > >[/color]
> >[/color]
>[/color]
( http://msdn.microsoft.com/library/de...-us/dllproc/ba[color=blue][color=green][color=darkred]
> > > > > se/createservice.asp) ,
> > > > > public static extern IntPtr CreateService(IntPtr[/color][/color]
> SC_HANDLE,string[color=green][color=darkred]
> > > > > lpSvcName,string lpDisplayName,
> > > > > int dwDesiredAccess,int dwServiceType,int dwStartType,int
> > > > > dwErrorControl,string lpPathName,
> > > > > string lpLoadOrderGroup,int lpdwTagId,string
> > > lpDependencies,string
> > > > > lpServiceStartName,string lpPassword);
> > > > > is the core API to create a service. And the last two parameters[/color]
> > should[color=darkred]
> > > be
> > > > > name and password of account under wihcih service should run. But[/color][/color]
> when[color=green]
> > I[color=darkred]
> > > > > tried to pass values to these two parameters bu calling:
> > > > > sv_handle = CreateService(sc_handle, serviceName,[/color][/color]
> serviceDisplayName,[color=green][color=darkred]
> > > > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > > > > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > > > > servicePath, null, 0, null, _UserName, _Password);
> > > > > This piece of code is just a bit different from original, but[/color]
> > sv_handle[color=darkred]
> > > > > return is 0, which means failure.
> > > > >
> > > > > Anybody has idea how to install a service under some certain
> > > account?
> > > > >
> > > > >
> > > > > Thanks for any suggestion.
> > > > >
> > > > >
> > > > > --
> > > > > WWW: http://hardywang.1accesshost.com
> > > > > ICQ: 3359839
> > > > > yours Hardy
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color] | 
November 15th, 2005, 10:57 AM
| | | | re: Installing a Windows Service Programmatically
Yes, I am sure, I used a catch to capure exception thrown by code, and got
that message.
By calling "int err = Marshal.GetLastWin32Error();" I have error number
1421, don't know what does it mean.
--
WWW: http://hardywang.1accesshost.com
ICQ: 3359839
yours Hardy
"Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com> wrote
in message news:eR3gh%23tdDHA.1752@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hardy,
>
> Are you sure that this is the error that the call to CreateService[/color]
gave?[color=blue]
> If you replace the "throw" part with a call to GetLastWin32Error, what[/color]
does[color=blue]
> that return?
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - nicholas.paldino@exisconsulting.com
>
> "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> news:uuPxP2tdDHA.1712@tk2msftngp13.phx.gbl...[color=green]
> > I tried your codes, got error message "Control ID not found".
> >
> > --
> > WWW: http://hardywang.1accesshost.com
> > ICQ: 3359839
> > yours Hardy
> > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com>[/color]
> wrote[color=green]
> > in message news:e$jjjytdDHA.1448@TK2MSFTNGP12.phx.gbl...[color=darkred]
> > > Hardy,
> > >
> > > You should never call GetLastError yourself, as it is not[/color][/color][/color]
guaranteed[color=blue][color=green]
> > to[color=darkred]
> > > be accurate. The reason for this is that the runtime makes API calls
> > > between your API call and it might set the error state on its own.[/color]
> > Because[color=darkred]
> > > of this, you should call the static GetLastWin32Error method on the[/color]
> > Marshal[color=darkred]
> > > class.
> > >
> > > However, there is a class that does all of this, the[/color][/color][/color]
Win32Exception[color=blue][color=green][color=darkred]
> > > class in the System.ComponentModel namespace. You should make your[/color][/color][/color]
call[color=blue][color=green][color=darkred]
> > > like this:
> > >
> > > // Call CreateService.
> > > IntPtr sv_handle = CreateService(sc_handle, serviceName,[/color]
> > serviceDisplayName,[color=darkred]
> > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
> > > SERVICE_ERROR_NORMAL, servicePath, null, 0, null, _UserName,[/color][/color][/color]
_Password);[color=blue][color=green][color=darkred]
> > >
> > > // Throw an exception if there is no handle.
> > > try
> > > {
> > > if (sv_handle == IntPtr.Zero)
> > > // Throw an exception.
> > > throw new Win32Exception();
> > > }
> > > finally
> > > {
> > > CloseServiceHandle(sc_handle);
> > > }
> > >
> > > This should give you the description of the error. You can also[/color][/color]
> look[color=green]
> > in[color=darkred]
> > > WINERROR.H to find the error the code represents.
> > >
> > >
> > > --
> > > - Nicholas Paldino [.NET/C# MVP]
> > > - nicholas.paldino@exisconsulting.com
> > >
> > > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > > news:%23MNLettdDHA.1460@TK2MSFTNGP10.phx.gbl...
> > > > I adding some code like:
> > > > sv_handle = CreateService(sc_handle, serviceName,[/color][/color][/color]
serviceDisplayName,[color=blue][color=green][color=darkred]
> > > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > > > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > > > servicePath, null, 0, null, _UserName, _Password);
> > > >
> > > > if (sv_handle.ToInt32() == 0) {
> > > > CloseServiceHandle(sc_handle);
> > > > int err = GetLastError();
> > > > return false;
> > > > }
> > > >
> > > > I tried to capture the error by reading return from GetLastError(),[/color][/color][/color]
I[color=blue][color=green]
> > got[color=darkred]
> > > > 997, where to find the definition of error code?
> > > >
> > > > --
> > > > WWW: http://hardywang.1accesshost.com
> > > > ICQ: 3359839
> > > > yours Hardy
> > > > "Nicholas Paldino [.NET/C# MVP]"[/color][/color][/color]
<nicholas.paldino@exisconsulting.com>[color=blue][color=green][color=darkred]
> > > wrote
> > > > in message news:eWiePotdDHA.1532@TK2MSFTNGP10.phx.gbl...
> > > > > Hardy,
> > > > >
> > > > > If the return value is IntPtr.Zero, then throw a new instance[/color][/color][/color]
of[color=blue][color=green]
> > the[color=darkred]
> > > > > Win32Exception class. It will detail what the error from the call[/color][/color]
> to[color=green][color=darkred]
> > > > > GetLastError is and it will give you more information about what[/color][/color]
> went[color=green][color=darkred]
> > > > wrong
> > > > > with the call.
> > > > >
> > > > > Hope this helps.
> > > > >
> > > > >
> > > > > --
> > > > > - Nicholas Paldino [.NET/C# MVP]
> > > > > - nicholas.paldino@exisconsulting.com
> > > > >
> > > > > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > > > > news:Oo$9vktdDHA.2804@TK2MSFTNGP11.phx.gbl...
> > > > > > Hi all:
> > > > > > I read an article from
> > > > > >
> > > > >
> > > >
> > >[/color]
> >[/color]
>[/color] http://www.c-sharpcorner.com/Code/20...mmatically.asp[color=blue][color=green][color=darkred]
> > > > > > about how to install a windows service programmatically.
> > > > > > Based ont the code sample, it provides the feature to[/color][/color][/color]
install[color=blue][color=green][color=darkred]
> > > > service
> > > > > > under LocalSystem account.
> > > > > > What I need is to install service under some other certian
> > > account.
> > > > By
> > > > > > further studying the code, and MSDN
> > > > > >
> > > > >
> > > >
> > >[/color]
> >[/color]
>[/color]
( http://msdn.microsoft.com/library/de...-us/dllproc/ba[color=blue][color=green][color=darkred]
> > > > > > se/createservice.asp) ,
> > > > > > public static extern IntPtr CreateService(IntPtr[/color]
> > SC_HANDLE,string[color=darkred]
> > > > > > lpSvcName,string lpDisplayName,
> > > > > > int dwDesiredAccess,int dwServiceType,int[/color][/color][/color]
dwStartType,int[color=blue][color=green][color=darkred]
> > > > > > dwErrorControl,string lpPathName,
> > > > > > string lpLoadOrderGroup,int lpdwTagId,string
> > > > lpDependencies,string
> > > > > > lpServiceStartName,string lpPassword);
> > > > > > is the core API to create a service. And the last two parameters
> > > should
> > > > be
> > > > > > name and password of account under wihcih service should run.[/color][/color][/color]
But[color=blue][color=green]
> > when[color=darkred]
> > > I
> > > > > > tried to pass values to these two parameters bu calling:
> > > > > > sv_handle = CreateService(sc_handle, serviceName,[/color]
> > serviceDisplayName,[color=darkred]
> > > > > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > > > > > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > > > > > servicePath, null, 0, null, _UserName, _Password);
> > > > > > This piece of code is just a bit different from original, but
> > > sv_handle
> > > > > > return is 0, which means failure.
> > > > > >
> > > > > > Anybody has idea how to install a service under some certain
> > > > account?
> > > > > >
> > > > > >
> > > > > > Thanks for any suggestion.
> > > > > >
> > > > > >
> > > > > > --
> > > > > > WWW: http://hardywang.1accesshost.com
> > > > > > ICQ: 3359839
> > > > > > yours Hardy
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color] | 
November 15th, 2005, 10:57 AM
| | | | re: Installing a Windows Service Programmatically
Hardy,
This is truly odd. 1421 is what the exception and the call to
GetLastWin32Error returned. However, I don't know why CreateService is
returning it. What are you doing in the UI when this call is made?
--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com
"Hardy Wang" <hardy_wang@marketrend.com> wrote in message
news:e7iUSCudDHA.1580@tk2msftngp13.phx.gbl...[color=blue]
> Yes, I am sure, I used a catch to capure exception thrown by code, and got
> that message.
> By calling "int err = Marshal.GetLastWin32Error();" I have error number
> 1421, don't know what does it mean.
>
> --
> WWW: http://hardywang.1accesshost.com
> ICQ: 3359839
> yours Hardy
> "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com>[/color]
wrote[color=blue]
> in message news:eR3gh%23tdDHA.1752@TK2MSFTNGP11.phx.gbl...[color=green]
> > Hardy,
> >
> > Are you sure that this is the error that the call to CreateService[/color]
> gave?[color=green]
> > If you replace the "throw" part with a call to GetLastWin32Error, what[/color]
> does[color=green]
> > that return?
> >
> >
> > --
> > - Nicholas Paldino [.NET/C# MVP]
> > - nicholas.paldino@exisconsulting.com
> >
> > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > news:uuPxP2tdDHA.1712@tk2msftngp13.phx.gbl...[color=darkred]
> > > I tried your codes, got error message "Control ID not found".
> > >
> > > --
> > > WWW: http://hardywang.1accesshost.com
> > > ICQ: 3359839
> > > yours Hardy
> > > "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldino@exisconsulting.com>[/color]
> > wrote[color=darkred]
> > > in message news:e$jjjytdDHA.1448@TK2MSFTNGP12.phx.gbl...
> > > > Hardy,
> > > >
> > > > You should never call GetLastError yourself, as it is not[/color][/color]
> guaranteed[color=green][color=darkred]
> > > to
> > > > be accurate. The reason for this is that the runtime makes API[/color][/color][/color]
calls[color=blue][color=green][color=darkred]
> > > > between your API call and it might set the error state on its own.
> > > Because
> > > > of this, you should call the static GetLastWin32Error method on the
> > > Marshal
> > > > class.
> > > >
> > > > However, there is a class that does all of this, the[/color][/color]
> Win32Exception[color=green][color=darkred]
> > > > class in the System.ComponentModel namespace. You should make your[/color][/color]
> call[color=green][color=darkred]
> > > > like this:
> > > >
> > > > // Call CreateService.
> > > > IntPtr sv_handle = CreateService(sc_handle, serviceName,
> > > serviceDisplayName,
> > > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
> > > > SERVICE_ERROR_NORMAL, servicePath, null, 0, null, _UserName,[/color][/color]
> _Password);[color=green][color=darkred]
> > > >
> > > > // Throw an exception if there is no handle.
> > > > try
> > > > {
> > > > if (sv_handle == IntPtr.Zero)
> > > > // Throw an exception.
> > > > throw new Win32Exception();
> > > > }
> > > > finally
> > > > {
> > > > CloseServiceHandle(sc_handle);
> > > > }
> > > >
> > > > This should give you the description of the error. You can also[/color]
> > look[color=darkred]
> > > in
> > > > WINERROR.H to find the error the code represents.
> > > >
> > > >
> > > > --
> > > > - Nicholas Paldino [.NET/C# MVP]
> > > > - nicholas.paldino@exisconsulting.com
> > > >
> > > > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > > > news:%23MNLettdDHA.1460@TK2MSFTNGP10.phx.gbl...
> > > > > I adding some code like:
> > > > > sv_handle = CreateService(sc_handle, serviceName,[/color][/color]
> serviceDisplayName,[color=green][color=darkred]
> > > > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > > > > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > > > > servicePath, null, 0, null, _UserName, _Password);
> > > > >
> > > > > if (sv_handle.ToInt32() == 0) {
> > > > > CloseServiceHandle(sc_handle);
> > > > > int err = GetLastError();
> > > > > return false;
> > > > > }
> > > > >
> > > > > I tried to capture the error by reading return from[/color][/color][/color]
GetLastError(),[color=blue]
> I[color=green][color=darkred]
> > > got
> > > > > 997, where to find the definition of error code?
> > > > >
> > > > > --
> > > > > WWW: http://hardywang.1accesshost.com
> > > > > ICQ: 3359839
> > > > > yours Hardy
> > > > > "Nicholas Paldino [.NET/C# MVP]"[/color][/color]
> <nicholas.paldino@exisconsulting.com>[color=green][color=darkred]
> > > > wrote
> > > > > in message news:eWiePotdDHA.1532@TK2MSFTNGP10.phx.gbl...
> > > > > > Hardy,
> > > > > >
> > > > > > If the return value is IntPtr.Zero, then throw a new[/color][/color][/color]
instance[color=blue]
> of[color=green][color=darkred]
> > > the
> > > > > > Win32Exception class. It will detail what the error from the[/color][/color][/color]
call[color=blue][color=green]
> > to[color=darkred]
> > > > > > GetLastError is and it will give you more information about what[/color]
> > went[color=darkred]
> > > > > wrong
> > > > > > with the call.
> > > > > >
> > > > > > Hope this helps.
> > > > > >
> > > > > >
> > > > > > --
> > > > > > - Nicholas Paldino [.NET/C# MVP]
> > > > > > - nicholas.paldino@exisconsulting.com
> > > > > >
> > > > > > "Hardy Wang" <hardy_wang@marketrend.com> wrote in message
> > > > > > news:Oo$9vktdDHA.2804@TK2MSFTNGP11.phx.gbl...
> > > > > > > Hi all:
> > > > > > > I read an article from
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >[/color]
> >[/color]
>[/color] http://www.c-sharpcorner.com/Code/20...mmatically.asp[color=blue][color=green][color=darkred]
> > > > > > > about how to install a windows service programmatically.
> > > > > > > Based ont the code sample, it provides the feature to[/color][/color]
> install[color=green][color=darkred]
> > > > > service
> > > > > > > under LocalSystem account.
> > > > > > > What I need is to install service under some other certian
> > > > account.
> > > > > By
> > > > > > > further studying the code, and MSDN
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >[/color]
> >[/color]
>[/color]
( http://msdn.microsoft.com/library/de...-us/dllproc/ba[color=blue][color=green][color=darkred]
> > > > > > > se/createservice.asp) ,
> > > > > > > public static extern IntPtr CreateService(IntPtr
> > > SC_HANDLE,string
> > > > > > > lpSvcName,string lpDisplayName,
> > > > > > > int dwDesiredAccess,int dwServiceType,int[/color][/color]
> dwStartType,int[color=green][color=darkred]
> > > > > > > dwErrorControl,string lpPathName,
> > > > > > > string lpLoadOrderGroup,int lpdwTagId,string
> > > > > lpDependencies,string
> > > > > > > lpServiceStartName,string lpPassword);
> > > > > > > is the core API to create a service. And the last two[/color][/color][/color]
parameters[color=blue][color=green][color=darkred]
> > > > should
> > > > > be
> > > > > > > name and password of account under wihcih service should run.[/color][/color]
> But[color=green][color=darkred]
> > > when
> > > > I
> > > > > > > tried to pass values to these two parameters bu calling:
> > > > > > > sv_handle = CreateService(sc_handle, serviceName,
> > > serviceDisplayName,
> > > > > > > SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> > > > > > > SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> > > > > > > servicePath, null, 0, null, _UserName, _Password);
> > > > > > > This piece of code is just a bit different from original, but
> > > > sv_handle
> > > > > > > return is 0, which means failure.
> > > > > > >
> > > > > > > Anybody has idea how to install a service under some[/color][/color][/color]
certain[color=blue][color=green][color=darkred]
> > > > > account?
> > > > > > >
> > > > > > >
> > > > > > > Thanks for any suggestion.
> > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > > WWW: http://hardywang.1accesshost.com
> > > > > > > ICQ: 3359839
> > > > > > > yours Hardy
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >[/color]
> >
> >[/color]
>
>[/color] | 
November 15th, 2005, 10:57 AM
| | | | re: Installing a Windows Service Programmatically
Let me post all my codes here, what I like to do is to create a class
library, I can use it by other UI, then add a test Windows Application
Class Library:
----------------------------------------
using System;
using System.Runtime.InteropServices;
/// <summary>
/// Programmatically install / uninstall Windows Service
/// </summary>
/// <example>
/// <code>
/// string svcPath = @"C:\build\service\Debug\Service.exe";
/// string svcDispName="Service Display Name";
/// string svcName= "Service Name";
/// WindowsServiceInstaller c = new WindowsServiceInstaller();
/// c.InstallService(svcPath, svcName, svcDispName);
/// </code>
/// </example>
public class WindowsServiceInstaller {
/// <summary>
/// Default constructor to initialize instance
/// </summary>
public WindowsServiceInstaller() {
_UserName = "";
_Password = "";
}
/// <summary>
/// Username to run account
/// </summary>
private string _UserName;
/// <summary>
/// Password to run account
/// </summary>
private string _Password;
#region DLLImport
/// <summary>
/// Open SC Manager
/// </summary>
[DllImport("advapi32.dll")]
private static extern IntPtr OpenSCManager(string lpMachineName,string
lpSCDB, int scParameter);
/// <summary>
/// Create Service
/// </summary>
[DllImport("Advapi32.dll")]
private static extern IntPtr CreateService(IntPtr SC_HANDLE,string
lpSvcName,string lpDisplayName,
int dwDesiredAccess,int dwServiceType,int dwStartType,int
dwErrorControl,string lpPathName,
string lpLoadOrderGroup,int lpdwTagId,string lpDependencies,string
lpServiceStartName,string lpPassword);
/// <summary>
/// Close Service Handle
/// </summary>
[DllImport("advapi32.dll")]
private static extern void CloseServiceHandle(IntPtr SCHANDLE);
/// <summary>
/// Start Service
/// </summary>
[DllImport("advapi32.dll")]
private static extern int StartService(IntPtr SVHANDLE,int
dwNumServiceArgs,string lpServiceArgVectors);
/// <summary>
/// Open Service
/// </summary>
[DllImport("advapi32.dll",SetLastError=true)]
private static extern IntPtr OpenService(IntPtr SCHANDLE,string
lpSvcName,int dwNumServiceArgs);
/// <summary>
/// Delete Service
/// </summary>
[DllImport("advapi32.dll")]
private static extern int DeleteService(IntPtr SVHANDLE);
/// <summary>
/// Get last error
/// </summary>
[DllImport("kernel32.dll")]
private static extern int GetLastError();
#endregion DLLImport
//path to the service that you want to install
//servicePath = @"C:\build\service\Debug\Service.exe";
//serviceDisplayName="Service Display Name";
//serviceName= "Service Name";
/// <summary>
/// This method installs and runs the service in the service conrol manager
under LocalSystem account.
/// </summary>
/// <param name="servicePath">The complete path of the service.</param>
/// <param name="serviceName">Name of the service.</param>
/// <param name="serviceDisplayName">Display name of the service.</param>
/// <returns>True if the process went throgh successfully. False if there
was any error.</returns>
public bool InstallService(string servicePath, string serviceName, string
serviceDisplayName) {
#region Constants declaration.
int SC_MANAGER_CREATE_SERVICE = 0x0002;
int SERVICE_WIN32_OWN_PROCESS = 0x00000010;
int SERVICE_DEMAND_START = 0x00000003;
int SERVICE_ERROR_NORMAL = 0x00000001;
int STANDARD_RIGHTS_REQUIRED = 0xF0000;
int SERVICE_QUERY_CONFIG = 0x0001;
int SERVICE_CHANGE_CONFIG = 0x0002;
int SERVICE_QUERY_STATUS = 0x0004;
int SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
int SERVICE_START = 0x0010;
int SERVICE_STOP = 0x0020;
int SERVICE_PAUSE_CONTINUE =0x0040;
int SERVICE_INTERROGATE = 0x0080;
int SERVICE_USER_DEFINED_CONTROL = 0x0100;
int SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
SERVICE_QUERY_CONFIG |
SERVICE_CHANGE_CONFIG |
SERVICE_QUERY_STATUS |
SERVICE_ENUMERATE_DEPENDENTS |
SERVICE_START |
SERVICE_STOP |
SERVICE_PAUSE_CONTINUE |
SERVICE_INTERROGATE |
SERVICE_USER_DEFINED_CONTROL);
int SERVICE_AUTO_START = 0x00000002;
#endregion Constants declaration.
IntPtr sc_handle = OpenSCManager(null,null,SC_MANAGER_CREATE_SERVICE) ;
if (sc_handle.ToInt32() != 0) {
IntPtr sv_handle;
if (_UserName.Trim().Length > 0) {
sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
servicePath, null, 0, null, _UserName, _Password);
} else {
sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
servicePath, null, 0, null, null, null);
}
if (sv_handle.ToInt32() == 0) {
int err = Marshal.GetLastWin32Error();
CloseServiceHandle(sc_handle);
return false;
} else {
//now trying to start the service
int i = StartService(sv_handle, 0, null);
// If the value i is zero, then there was an error starting the service.
// note: error may arise if the service is already running or some other
problem.
if(i == 0) {
//Console.WriteLine("Couldnt start service");
return false;
}
//Console.WriteLine("Success");
CloseServiceHandle(sc_handle);
return true;
}
} else {
//Console.WriteLine("SCM not opened successfully");
return false;
}
}
/// <summary>
/// This method installs and runs the service in the service conrol manager
under specified account.
/// </summary>
/// <param name="servicePath">The complete path of the service.</param>
/// <param name="serviceName">Name of the service.</param>
/// <param name="serviceDisplayName">Display name of the service.</param>
/// <param name="serviceStartName">The name of the account under which the
service should run</param>
/// <param name="password">The password to the account name specified by
the serviceStartName parameter</param>
/// <returns>True if the process went throgh successfully. False if there
was any error.</returns>
public bool InstallService(string servicePath, string serviceName,
string serviceDisplayName, string serviceStartName, string password) {
_UserName = serviceStartName;
_Password = password;
return InstallService(servicePath, serviceName, serviceDisplayName);
}
/// <summary>
/// This method uninstalls the service from the service conrol manager.
/// </summary>
/// <param name="serviceName">Name of the service to uninstall.</param>
public bool UnInstallService(string serviceName) {
int GENERIC_WRITE = 0x40000000;
IntPtr sc_hndl = OpenSCManager(null,null,GENERIC_WRITE);
if (sc_hndl.ToInt32() !=0) {
int DELETE = 0x10000;
IntPtr svc_hndl = OpenService(sc_hndl,serviceName,DELETE);
//Console.WriteLine(svc_hndl.ToInt32());
if(svc_hndl.ToInt32() !=0) {
int i = DeleteService(svc_hndl);
if (i != 0) {
CloseServiceHandle(sc_hndl);
return true;
} else {
CloseServiceHandle(sc_hndl);
return false;
}
} else {
return false;
}
} else {
return false;
}
}
}
===========================
Test application:
-----------------------------
private void button1_Click(object sender, System.EventArgs e) {
WindowsServiceInstaller c = new WindowsServiceInstaller();
if
(c.InstallService(@"C:\Work\ReportScheduler\Report Scheduler\bin\Debug\Report
Scheduler.exe", "Report", "Report", @"domian\username", "password")) {
MessageBox.Show("OK");
} else {
MessageBox.Show("Bad");
}
}
===============================================
I tried the overload without username and password, the service is created,
but this one failed. | 
November 15th, 2005, 11:04 AM
| | | | re: Installing a Windows Service Programmatically
Why not simply use the sc.exe commandline utility (standard on XP and W2K3, resource kit utility for NT4).
Willy.
"Hardy Wang" <hardy_wang@marketrend.com> wrote in message news:Oo$9vktdDHA.2804@TK2MSFTNGP11.phx.gbl...[color=blue]
> Hi all:
> I read an article from
> http://www.c-sharpcorner.com/Code/20...mmatically.asp
> about how to install a windows service programmatically.
> Based ont the code sample, it provides the feature to install service
> under LocalSystem account.
> What I need is to install service under some other certian account. By
> further studying the code, and MSDN
> ( http://msdn.microsoft.com/library/de...-us/dllproc/ba
> se/createservice.asp) ,
> public static extern IntPtr CreateService(IntPtr SC_HANDLE,string
> lpSvcName,string lpDisplayName,
> int dwDesiredAccess,int dwServiceType,int dwStartType,int
> dwErrorControl,string lpPathName,
> string lpLoadOrderGroup,int lpdwTagId,string lpDependencies,string
> lpServiceStartName,string lpPassword);
> is the core API to create a service. And the last two parameters should be
> name and password of account under wihcih service should run. But when I
> tried to pass values to these two parameters bu calling:
> sv_handle = CreateService(sc_handle, serviceName, serviceDisplayName,
> SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
> SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
> servicePath, null, 0, null, _UserName, _Password);
> This piece of code is just a bit different from original, but sv_handle
> return is 0, which means failure.
>
> Anybody has idea how to install a service under some certain account?
>
>
> Thanks for any suggestion.
>
>
> --
> WWW: http://hardywang.1accesshost.com
> ICQ: 3359839
> yours Hardy
>
>[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,689 network members.
|