2012-09-19 40 views
1

我已經使用C#在Win7中創建了一個服務。在該服務中,我想使用BlockInput函數,它在Win7中似乎需要管理員。C#窗口7塊輸入

我在代碼中將serviceprocessorinstaller的帳戶,用戶名和密碼設置爲admin帳戶,並且BlockInput不起作用。注意:在服務屬性/登錄選項卡下,它已正確設置。

然後我爲我的服務添加了一個清單,並將requestedExecutionLevel設置爲「requireAdministrator」。然而BlockInput仍然不想工作。

我試着將BlockInput向下移動到客戶端級別,然後移動到客戶端包裝但仍然不想工作。

用盡想法...有什麼建議嗎?

開始編輯在這裏:

這裏是我的清單(或它的一部分,對不起格式是可怕的) <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); 
      } 
      } 
     } 
    } 

 
+0

你可以顯示你有什麼代碼..? – MethodMan

+0

不是我目前所擁有的,因爲它是爲了工作。我將重新創建一個簡單的服務來展示我所看到的(或者在這種情況下不會看到)。 – JSolberg

+0

我的服務處理WCF消息,所以我不認爲它運行在會話0,我不能看到它是問題。 – JSolberg

回答

0

我繼續前進,並使用SetWindowsHookEx來捕獲WH_KEYBOARD_LL和WH_MOUSE_LL事件。

我試過所有我能想到的BlockInput工作。我仍然認爲我只是配置我的服務錯了什麼,但我沒有時間再擔心它了。

謝謝。