Solved

Disable the Alt+Tab,Alt+Esc,Ctrl+Esc,Ctrl+Alt+del through C# program.....Please help me!!!!!!!!!

Posted on 2003-08-07
1,334 Views
Last Modified: 2007-12-19
Dear Friends,
How do I Disable the Alt+Tab,Alt+Esc,Ctrl+Esc,Ctrl+Alt+del through C# program?
I am new to C# programming...If any one knows please let me know.... code or tips are always welcome

Advance thanks...


Thanks,
Sharmili


I have pasted vb.Net code...i don't know how to convert vb.net to C#..If any one know please help me .. This is not included (Ctrl+Alt+Del)

Public Class frmMain
    Inherits System.Windows.Forms.Form

    Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProcDelegate, ByVal hMod As Integer, ByVal dwThreadId As Integer) As Integer
    Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookEx" (ByVal hHook As Integer) As Integer
    Delegate Function LowLevelKeyboardProcDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookEx" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer

    Const WH_KEYBOARD_LL = 13

    Structure KBDLLHOOKSTRUCT
        Dim vkCode As Integer
        Dim scanCode As Integer
        Dim flags As Integer
        Dim time As Integer
        Dim dwExtraInfo As Integer
    End Structure

    Dim intLLKey As Integer

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf LowLevelKeyboardProc, System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32(), 0)
    End Sub

    Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        UnhookWindowsHookEx(intLLKey)
    End Sub

    Private Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) As Integer
        Dim blnEat As Boolean = False

        Select Case wParam
            Case 256, 257, 260, 261
                'Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
                blnEat = ((lParam.vkCode = 9) AndAlso (lParam.flags = 32)) Or _
                ((lParam.vkCode = 27) AndAlso (lParam.flags = 32)) Or _
                ((lParam.vkCode = 27) AndAlso (lParam.flags = 0)) Or _
                ((lParam.vkCode = 91) AndAlso (lParam.flags = 1)) Or _
                ((lParam.vkCode = 92) AndAlso (lParam.flags = 1))
        End Select

        If blnEat = True Then
            Return 1
        Else
            Return CallNextHookEx(0, nCode, wParam, lParam)
        End If
    End Function
    Friend WithEvents lblInfo As System.Windows.Forms.Label
End Class


Good Question?
0
Question by:sharmilicsharp
    2 Solutions
    Best Solution bylyrix1999
    sharmilicsharp, is your vb code actually working? I am researching the similar staff in C#, but I find this disclaimer in MSDN: "Global Hook Is Not Supported in .NET Framework You cannot implement
    3 Comments
     
    2 Solutions

    Accepted Solution

    by:
    sharmilicsharp, is your vb code actually working? I am researching the similar staff in C#, but I find this disclaimer in MSDN:


    "Global Hook Is Not Supported in .NET Framework

    You cannot implement global hooks in Microsoft .NET Framework. To install a global hook, a hook must have a native dynamic-link library (DLL) export to inject itself in another process that requires a valid, consistent function to call into. This requires a DLL export, which .NET Framework does not support. Managed code has no concept of a consistent value for a function pointer because these function pointers are proxies that are built dynamically."
    Good Solution?
    0
     
    50 Solutions

    Assisted Solution

    by:c_myers
    Yep, he's right.

    http://support.microsoft.com/?kbid=318804

    I converted the C# code, but I get error 1429 from SetWindowsHookEx which is ERROR_GLOBAL_ONLY_HOOK or "This hook procedure can only be set globally."

    Sorry man :(

    Maybe you could have a C++MEC DLL that used remoting to send key hook events to you?
    Good Solution?
    0
     
    749 Solutions

    Assisted Solution

    by:TheAvenger
    Answers to both your questions:

    1. How you can convert a VB code to C#: I found a very nice decompilor, with which you can compile the VB code and then decompile to C# or VB. So, compile the code given and decompile using the Reflector application from http://www.aisto.com/roeder/dotnet/. Unfortunately it decompiles only the functions so some declarations you have to do manually.

    2. How to disable special key combinations: see http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20691493.html. The topic will give you an answer for some keys like Alt+F4 and will (unfortunately) tell you that it's difficult to catch the other ones (like Ctrl+Alt+Del). Also you will find a link to a commercial library with some drivers that can help you filter the keys.

    I hope I helped.
    The Avenger
    Good Solution?
    0

    Add a Comment

    Join our community to follow up on this question and 4 million more solutions.

    Join & Write a Comment

    Featured Post

    Course: AWS Professional Certification

    These classes are designed to help you pass the AWS Certified Solutions Architect – Associate, AWS Certified Developer, and AWS Certified SysOps Administrator Associate exams—all essential to a career as an AWS professional.

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    772 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    32 Experts available now in Live!

    Get 1:1 Help Now