简体   繁体   中英

Invoking a .exe referenced to the c# windows application

I created a sample project to disable the keyboard shortcuts of windows. Then I included the .exe of this sample project to the reference of my main project where I want to implement this feature.

Problem is, it is not working in my main project. But working perfectly in my sample project.

Am I missing something like invoking the referenced .exe ??

I don't want to implement the code of sample project to the main project I just want to reference the .exe to the main project.

How?

namespace BlockShortcuts
{
    public class DisableKeys
    {

        private delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);


        [DllImport("user32.dll", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi)]
        private static extern int SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, int hMod, int dwThreadId);

        [DllImport("user32.dll")]
        private static extern int UnhookWindowsHookEx(int hHook);

        [DllImport("user32.dll", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi)]
        private static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);

        const int WH_KEYBOARD_LL = 13;

        private int intLLKey;

        private struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            int scanCode;
            public int flags;
            int time;
            int dwExtraInfo;
        }
        private int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
        {
            bool blnEat = false; switch (wParam)
            {
                case 256:
                case 257:
                case 260:
                case 261:
                    //Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key                           
                    if (((lParam.vkCode == 9) && (lParam.flags == 32)) ||
                        ((lParam.vkCode == 27) && (lParam.flags == 32)) ||
                        ((lParam.vkCode == 27) && (lParam.flags == 0)) ||
                        ((lParam.vkCode == 91) && (lParam.flags == 1)) ||
                        ((lParam.vkCode == 92) && (lParam.flags == 1)) ||
                        ((true) && (lParam.flags == 32)))
                    {
                        blnEat = true;
                    }
                    break;
            } if (blnEat) return 1; else return CallNextHookEx(0, nCode, wParam, ref lParam);
        }
        public void DisableKeyboardHook()
        {
            intLLKey = SetWindowsHookEx(WH_KEYBOARD_LL, new LowLevelKeyboardProcDelegate(LowLevelKeyboardProc), System.Runtime.InteropServices.Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0);
        }
        private void ReleaseKeyboardHook()
        {
            intLLKey = UnhookWindowsHookEx(intLLKey);
        }
        #endregion  
    }
}

Referencing the project just tells visual studio (or what ever IDE you're using) that you might want to use some code from that library, it will not include anything or execute anything on it's own. If you wish to execute code from the sample project you can either build it as a dll (if you have the source code) or you can execute the program from you main program (you will not need to include it as a referance in the latter case).

From one of your comments I get the idea that you are just trying to instantiate the code and run methods on it. Have you tried:

namespace test
{
   using BlockShortcuts;
   class MyTest
   {
        public static void Main(string[] args)
        {
           DisableKeys dk = new DisableKeys();
           dk.DisableKeyboardHook();
        }

   }
 }

Also have you included all the required dlls into you main project on just the one?

I'm guessing your code is initiated via a static constructor for a class. The static constructor for the class won't be called until you make some access to the class (create an object, access a public static field or property, or call a static method). You have a couple options:

  1. Create an object when your application starts up, where the object is responsible for the feature. (Such as this code in your Application's main class: private readonly KeyboardDisabler = new KeyboardDisabler(); )
  2. The other 2 options (attributes or reflection) I know are bad. Don't do them, so I'm not even writing about them.

Also, I'm pretty sure the CLI won't even load a referenced assembly if no code calls into it, so your original hope falls somewhere between "not possible" and "highly undesirable".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM