I have created a service in Win7 using C#. Within that service I want to use the BlockInput function, which in Win7 it seems requires admin.
I set the serviceprocessorinstaller’s, account, username, and password to an admin account, within the code and the BlockInput doesn’t work. Note: under the service properties/logon tab it is set properly.
I then added a manifest for my service and set the requestedExecutionLevel to “requireAdministrator”. Yet the BlockInput still doesn’t want to work.
I tried moving the BlockInput down to the client level and then to a client wrapper but still it doesn’t want to work.
Running out of ideas…Any suggestions?
Start Edit Here:
Here is my manifest (or part of it, sorry formatting is terrible)
<assemblyIdentity version="1.0.0.0" name="BlockInputService.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
namespace BlockInputService
{
[ServiceContract(Namespace = "http://BlockInputService")]
public interface IBlockInputTest
{
[OperationContract]
void BlockInputMethod();
}
public class BlockInputTest : IBlockInputTest
{
private const string SOURCE = "BlockInputService";
private const string LOGNAME = "Application";
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool BlockInput([In, MarshalAs(UnmanagedType.Bool)] bool fBlockIt);
public void BlockInputMethod()
{
if (!EventLog.SourceExists(SOURCE))
{
EventLog.CreateEventSource(SOURCE, LOGNAME);
}
try
{
bool flag = BlockInput(true);
EventLog.WriteEntry(SOURCE, "BlockInput(true) returned: " + flag);
EventLog.WriteEntry(SOURCE, "Sleep for 5 sec");
Thread.Sleep(5000);
}
catch (Exception ex)
{
EventLog.WriteEntry(SOURCE, ex.Message);
}
finally
{
bool flag = BlockInput(false);
EventLog.WriteEntry(SOURCE, "BlockInput(false) returned: " + flag);
}
}
}
}
I moved on and used SetWindowsHookEx to capture WH_KEYBOARD_LL and WH_MOUSE_LL events.
I tried everything I could think of to get the BlockInput to work. I still think I was just configuring my service wrong or something but I dont have time to worry about it anymore.
Thanks.