Disabling screen saver for non-admins

I wrote a program in C# that disables the screen saver for a certain amount of time by changing registry values. It works fine when ran by a machine admin but gets an permissions error if its ran by a normal user.

My first question is if I use the Windows API (SystemParametersInfo function, in user32.dll) would that be any differnt in allowing it to work for non-admin users?

If not, is there is any other way to get this to work for non-admins either changing registry keys or some other way?

thanks.

[533 byte] By [CLProgrammer2] at [2008-1-10]
# 1
you need to consider impersonation in this thread so that the program can access the registry as admin when the user cannot.

some helper class info from that thread

Windows Impersonation using C#

Impersonation class, C# Source

H.Tony at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

I would consider an application which tries to do things outside of its domain (affecting the whole machine instead of things only the app should be concerned about, like -- disabling the system screen saver!) a bad practice. So rather than "fix" this, I'd just advise that you're doing something which you shouldn't do in the first place. Your application is not the whole system; if the user wants a screen saver and set one up, he would be understandably annoyed that this single application pulled the rug out from under him.

Your application should really not touch things outside of itself. The screensaver is definitely outside.

eradicator at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
Eradicator while your advice is good to consider, you should realize that some people do actually have the need to modify parts of the system, in fact I had to do exactly this task that the OP is asking about, it was required as a part of my job. Although in my case my application is the whole system (Kiosk), so maybe I'm the exception to the rule, but I'm sure there is a valid reason for why this is needed.

You can't change the screensaver time without Admin rights however.

Here is the code I used
if (SystemParametersInfo(SETTIMEOUT,time,0,0) == 0)
{
int eCode = Marshal.GetLastWin32Error();
if (eCode != 0)
{
throw new SSException(
util.getErrorMessage(eCode));
}
}

the util.getErrorMessage looks like this

/// <summary>
/// Take in a Win32 Error Code from Marshal.GetLastWin32Error() and
/// return the relevant string.
/// </summary>
/// <param name="eCode">The error code to get a string for.</param>
/// <returns>Error message or null if failure to retrieve string.</returns>
public static string getErrorMessage(int eCode)
{
IntPtr lpMsgBuf= IntPtr.Zero;
uint dwChars= FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
IntPtr.Zero,
(uint)eCode,
0, // Default language
ref lpMsgBuf,
0,
IntPtr.Zero);
if (dwChars==0)
{
// Handle the error.
int le= Marshal.GetLastWin32Error();
return null;
}

string sRet= Marshal.PtrToStringAnsi(lpMsgBuf);

// Free the buffer.
lpMsgBuf= IntPtr.Zero;
return sRet;

}

I'll let you figure out the P/Invoke yourself it's not hard.

CalvinR at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 4
This app is used to disable the screen saver during power point presentations only, then when the program is closed it turns it back on. But since the registry keys changed are under current user is doesnt look like that windows impersonation will work either.
CLProgrammer2 at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
you can impersonate admin and have access to everything in registry, that way you can change setting for current user as any other user

CLProgrammer2 wrote:
This app is used to disable the screen saver during power point presentations only, then when the program is closed it turns it back on. But since the registry keys changed are under current user is doesnt look like that windows impersonation will work either.
H.Tony at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...