471,627 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

Starting servive but how to set proper priviledges

Hello,

I'm trying to start a monitor Windows server in my standalone Window PC.
I use code like following:

handle = ServiceWin32.OpenServiceA(SCManagerHandle, "MonitorServive",

ServiceWin32.ACCESS_TYPE.SERVICE_ALL_ACCESS);

if (handle < 1) // If not Succeeded, throw Exception. {

throw new Exception("Can't open the Service for modification.");

}

But i Get allways Exception that is in if clause. Handle is allways 0.

How should I set more priviledges? Admin privoíledges are ok because I run
PC in

standalone.

Cheers,


Jan 24 '06 #1
2 1509
Benny,

Well, it seems that the user account that you are running this for
doesn't have the appropriate privledges.

However, to be sure, you should declare your call to OpenService like
so:

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName,
[MarshalAs(UnmanagedType.U4)] dwDesiredAccess);

There is no need to be aware of the specific charset version on your
machine. The interop infrastructure will take care of that for you.

Additionally, you can get more information from the error code that is
returned, by making the call like this (assuming you have the declaration
above):

// Get the service handle.
IntPtr serviceHandle = ServiceWin32.OpenService(SCManagerHandle,
"MonitorServive", ServiceWin32.ACCESS_TYPE.SERVICE_ALL_ACCESS);

// Check the handle. < 1 is not a correct check.
if (serviceHandle == IntPtr.Zero)
{
// Throw an exception that will cull the information from GetLastError.
throw new System.ComponentModel.Win32Exception();
}

That will give you more details about the error.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Benny W" <bw@yahoo.com> wrote in message
news:dr**********@nyytiset.pp.htv.fi...
Hello,

I'm trying to start a monitor Windows server in my standalone Window PC.
I use code like following:

handle = ServiceWin32.OpenServiceA(SCManagerHandle, "MonitorServive",
ServiceWin32.ACCESS_TYPE.SERVICE_ALL_ACCESS);

if (handle < 1) // If not Succeeded, throw Exception. {

throw new Exception("Can't open the Service for modification.");

}

But i Get allways Exception that is in if clause. Handle is allways 0.

How should I set more priviledges? Admin privoíledges are ok because I run
PC in

standalone.

Cheers,


Jan 24 '06 #2
Thanks for your information!

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:et**************@TK2MSFTNGP11.phx.gbl...
Benny,

Well, it seems that the user account that you are running this for
doesn't have the appropriate privledges.

However, to be sure, you should declare your call to OpenService like
so:

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName,
[MarshalAs(UnmanagedType.U4)] dwDesiredAccess);

There is no need to be aware of the specific charset version on your
machine. The interop infrastructure will take care of that for you.

Additionally, you can get more information from the error code that is
returned, by making the call like this (assuming you have the declaration
above):

// Get the service handle.
IntPtr serviceHandle = ServiceWin32.OpenService(SCManagerHandle,
"MonitorServive", ServiceWin32.ACCESS_TYPE.SERVICE_ALL_ACCESS);

// Check the handle. < 1 is not a correct check.
if (serviceHandle == IntPtr.Zero)
{
// Throw an exception that will cull the information from GetLastError.
throw new System.ComponentModel.Win32Exception();
}

That will give you more details about the error.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Benny W" <bw@yahoo.com> wrote in message
news:dr**********@nyytiset.pp.htv.fi...
Hello,

I'm trying to start a monitor Windows server in my standalone Window PC.
I use code like following:

handle = ServiceWin32.OpenServiceA(SCManagerHandle, "MonitorServive",
ServiceWin32.ACCESS_TYPE.SERVICE_ALL_ACCESS);

if (handle < 1) // If not Succeeded, throw Exception. {

throw new Exception("Can't open the Service for modification.");

}

But i Get allways Exception that is in if clause. Handle is allways 0.

How should I set more priviledges? Admin privoíledges are ok because I
run PC in

standalone.

Cheers,



Jan 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

33 posts views Thread by Jim Cobban | last post: by
24 posts views Thread by tom pester | last post: by
1 post views Thread by M. Simioni | last post: by
1 post views Thread by deathbydisco | last post: by
12 posts views Thread by Prabu Subroto | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.