2009-08-26 64 views
0

我在我的應用程序中使用了一個名爲Kiosk的方法,它可以禁用某些鍵盤鍵。我做這樣的...從c#應用程序全局調用.dll的方法

using Kiosk; 
----- 

public static Kiosk.Kiosk KIOSK = new Kiosk.Kiosk(); 

----- 

static void Main() 
{ 
    KIOSK.Disable(); 
} 

我這樣調用Program.cs中,我的表格的所有其他頁面加載。我想在全球範圍內只調用一次該函數。在哪裏調用,以通過我的應用程序禁用我的鍵盤鍵。

我以爲Program.cs是全局調用方法的正確位置。但如果我只是在那裏調用方法,那麼劑量工作。

請幫助。謝謝。

Kiosk.dll包含此VB代碼: 此代碼工作正常。我在上面的應用程序中調用Disable()方法。

Option Explicit On 
Option Strict On 

Imports Microsoft.Win32 
Imports System.Runtime.InteropServices 

Public Class Kiosk 
    Implements IDisposable 

#Region "IDisposable" 

    ' Implementing IDisposable since it might be possible for 
    ' someone to forget to cause the unhook to occur. I didn't really 
    ' see any problems with this in testing, but since the SDK says 
    ' you should do it, then here's a way to make sure it will happen. 

    Public Overloads Sub Dispose() Implements IDisposable.Dispose 
     Dispose(True) 
     GC.SuppressFinalize(Me) 
    End Sub 

    Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean) 
     If disposing Then 
      ' Free other state (managed objects). 
     End If 
     If m_hookHandle <> 0 Then 
      UnhookWindowsHookEx(m_hookHandle) 
      m_hookHandle = 0 
     End If 
     If m_taskManagerValue > -1 Then 
      EnableTaskManager() 
     End If 
    End Sub 

    Protected Overrides Sub Finalize() 
     Dispose(False) 
    End Sub 

#End Region 

    Private Delegate Function LowLevelHookDelegate(ByVal code As Integer, ByVal wParam As Integer, ByRef lParam As KeyboardLowLevelHookStruct) As Integer 

    Private Const Hc_Action As Integer = 0 
    Private Const WindowsHookKeyboardLowLevel As Integer = 13 
    Private Const LowLevelKeyboardHfAltDown As Integer = &H20 

    Private Enum WindowsMessage 
     KeyDown = &H100 
     KeyUp = &H101 
     SystemKeyDown = &H104 
     SystemKeyUp = &H105 
    End Enum 

    Private Enum Vk 
     Tab = &H9 
     Escape = &H1B 
     Shift = &H10 
     Control = &H11 
     Menu = &H12   ' ALT key. 
     Alt = &H12 
     Pause = &H13 
     LeftWindows = &H5B ' Left Windows key (Microsoft® Natural® keyboard). 
     RightWindows = &H5C ' Right Windows key (Natural keyboard). 
     Applications = &H5D ' Applications key (Natural keyboard). 
    End Enum 

    Private Structure KeyboardLowLevelHookStruct 
     Public VirtualKeyCode As Integer 
     Public ScanCode As Integer 
     Public Flags As Integer 
     Public Time As Integer 
     Public ExtraInfo As UInt32 
    End Structure 

    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal hook As Integer, ByVal address As LowLevelHookDelegate, ByVal [mod] As Integer, ByVal threadId As Integer) As Integer 
    Private Declare Function CallNextHookEx Lib "user32" (ByVal handle As Integer, ByVal code As Integer, ByVal wParam As Integer, ByVal lParam As KeyboardLowLevelHookStruct) As Integer 
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal handle As Integer) As Integer 
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal virtualKey As Integer) As Integer 

    Private m_hookHandle As Integer 

    Private Function LowLevelHook(ByVal code As Integer, ByVal wParam As Integer, ByRef lParam As KeyboardLowLevelHookStruct) As Integer 

     If code = Hc_Action Then 

      If (wParam = WindowsMessage.KeyDown) OrElse _ 
       (wParam = WindowsMessage.SystemKeyDown) OrElse _ 
       (wParam = WindowsMessage.KeyUp) OrElse _ 
       (wParam = WindowsMessage.SystemKeyUp) Then 

       'Dim alt As Boolean = (GetAsyncKeyState(Vk.Alt) And &H8000) = &H8000 
       'Dim shift As Boolean = (GetAsyncKeyState(Vk.Shift) And &H8000) = &H8000 
       Dim control As Boolean = (GetAsyncKeyState(Vk.Control) And &H8000) = &H8000 

       Dim suppress As Boolean 

       ' CTRL+ESC 
       If control AndAlso lParam.VirtualKeyCode = Vk.Escape Then 
        suppress = True 
       End If 

       ' ALT+TAB 
       'If (lParam.Flags And LowLevelKeyboardHfAltDown) = LowLevelKeyboardHfAltDown AndAlso lParam.VirtualKeyCode = Vk.Tab Then 
       ' suppress = True 
       'End If 

       ' ALT+ESC 
       If (lParam.Flags And LowLevelKeyboardHfAltDown) = LowLevelKeyboardHfAltDown AndAlso lParam.VirtualKeyCode = Vk.Escape Then 
        suppress = True 
       End If 

       ' Left Windows button. 
       If lParam.VirtualKeyCode = Vk.LeftWindows Then 
        suppress = True 
       End If 

       ' Right Windows button. 
       If lParam.VirtualKeyCode = Vk.RightWindows Then 
        suppress = True 
       End If 

       ' Applications button. 
       If lParam.VirtualKeyCode = Vk.Applications Then 
        suppress = True 
       End If 

       If suppress Then 
        Return 1 
       End If 

      End If 

      Return CallNextHookEx(m_hookHandle, code, wParam, lParam) 

     End If 

    End Function 

    Public Sub Disable() 
     If m_hookHandle = 0 Then 
      m_hookHandle = SetWindowsHookEx(WindowsHookKeyboardLowLevel, AddressOf LowLevelHook, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0) 
     End If 
    End Sub 

    Public Sub Enable() 
     If m_hookHandle <> 0 Then 
      UnhookWindowsHookEx(m_hookHandle) 
      m_hookHandle = 0 
     End If 
    End Sub 

End Class 
+0

你是什麼意思,它不起作用? –

+0

您是否在編寫ASP.NET應用程序Win Forms? WPF? Kiosk類是什麼樣的?是禁用()標記爲靜態? – jscharf

+0

也許發佈一些更多的代碼將幫助你得到答案。我假設你在談論WinForms或WPF應用程序? – JohannesH

回答

0

如果你只想調用它一次,「全球」,你能做到像這樣:

class SomeMainClassThatAlwaysIsUsed 
{ 
    static SomeMainClassThatAlwaysIsUsed() { 
     new Kiosk.Kiosk().Disable(); 
    } 
} 

使用靜態初始化器,這將運行一次,每個AppDomain。

+0

如果他從'Main'調用它,它應該仍然可以工作。不知道那裏出了什麼問題。 –

+0

我也想知道;我認爲他暗示他並不總是叫Main()。希望我們能獲得更多信息。 –

0

是否被「.dll」引用?