简体   繁体   English

用于C#的C库,用于从操作系统队列中删除消息

[英]C library for C# to remove message from operating system queue

I have been developing an application in c# and I want to do some validation when a USB mass storage is inserted. 我一直在用c#开发一个应用程序,我想在插入USB大容量存储器时进行一些验证。

The problem is that during that validation I want to pull off that message of the mass storage from the message queue out. 问题是在验证期间我想从消息队列中取出大容量存储的消息。

A guy told me that you cannot do that in C# but only in C using Assembly language. 一个人告诉我,你不能在C#中这样做,但只能在C语言中使用汇编语言。

Can you guys help me find a C library that I can use from C# for pulling the messages from the operating system message queue? 你能帮我找一个C库,我可以用C#从操作系统消息队列中提取消息吗?

protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {

                case Win32.WM_DEVICECHANGE: 


                    //OnDeviceChange(ref m);                

                    break;
            }
            base.WndProc(ref m);

        }

        void OnDeviceChange(ref Message msg)
        {
            int wParam = (int)msg.WParam;

            if (wParam == Win32.DBT_DEVICEARRIVAL)
            {
                label1.Text = "Arrival";
                //MessageBox.Show("" + wParam);
                //msg = Message.Create(new IntPtr(),1,new IntPtr(),new IntPtr());
            }
            else if (wParam == Win32.DBT_DEVICEREMOVECOMPLETE) label1.Text =
             "Remove";
        }

I have done this but it just tells you that what happened. 我这样做了,但它只是告诉你发生了什么。

I want to pull the message off so that the operating system won't know abut the device and then put the message on again if the validation is validated. 我想关闭消息,以便操作系统不知道是否与设备相邻,然后在验证验证时再次打开消息。

Not going to happen, man. 不会发生,伙计。 By the time you get a Win32 WM_DEVICECHANGE message, the device is already very much loaded. 当您收到Win32 WM_DEVICECHANGE消息时,设备已经非常负载。 The kernel has become aware of the device, loaded drivers, and in this case as a mass storage device, already created a volume. 内核已经知道设备,加载的驱动程序,在这种情况下作为大容量存储设备,已经创建了一个卷。 That's all (mostly) kernel-mode activity. 这是所有(大多数)内核模式活动。 Then Win32 (user-mode) broadcasts this message to all top-level windows. 然后Win32(用户模式)将此消息广播到所有顶级窗口。 You simply can't do it in any language at the Win32 level, and this WM_DEVICECHANGE mechanism. 您根本无法在Win32级别的任何语言中执行此操作,以及此WM_DEVICECHANGE机制。

To do this, you're going to have to write a device driver. 为此,您将不得不编写设备驱动程序。 These are written in C, run in kernel-mode, and are much, much trickier to implement than a simple C# app. 它们是用C语言编写的,在内核模式下运行,并且比简单的C#应用​​程序实现起来要复杂得多。 I recommend you look for some other solution that accomplishes what you are trying to do. 我建议您寻找其他解决方案,以实现您的目标。 Which is... what, by the way? 那是什么,顺便问一下?

"else it discards the message so that the operating system won't know about it" “否则它会丢弃该消息,以便操作系统不会知道它”

Sorry, but you're completely missing the point. 对不起,但你完全忽略了这一点。 WM_DEVICECHANGE is sent from the OS (namely the Win32 subsystem) to all top-level windows . WM_DEVICECHANGE 从OS (即Win32子系统)发送到所有顶级窗口 It is just a notification, there is no control associated with this message. 它只是一个通知,没有与此消息相关的控件。 WM_DEVICECHANGE does not help in your goals of preventing access to the device. WM_DEVICECHANGE对您阻止访问设备的目标没有帮助。 Like I said, for this you would need a device driver, quite possibly a filesystem filter driver. 就像我说的,为此你需要一个设备驱动程序,很可能是一个文件系统过滤器驱动程序。 To that driver you would send an IOCTL (from user-mode) which would allow access to the device. 对于该驱动程序,您将发送一个允许访问该设备的IOCTL(来自用户模式)。

The OS does not depend on WM_DEVICECHANGE to learn about a device, so blocking the message won't do anything useful. 操作系统不依赖于WM_DEVICECHANGE来了解设备,因此阻止消息不会有用。

What you are asking to do is very difficult, because the OS drivers and filesystem have to be loaded in order for you to access the device and perform your validation, but it sounds like you want to prevent any other programs from using it. 您要求做的事情非常困难,因为必须加载操作系统驱动程序和文件系统才能访问设备并执行验证,但听起来您希望阻止任何其他程序使用它。 Even antivirus software implemented in the kernel doesn't prevent a device from appearing before it is scanned. 即使内核中实现的防病毒软件也不会阻止设备在扫描之前出现。

You could possibly implement a file system filter driver that reports that there are no files on the device, until the scan completes, but this will badly confuse software that sees the device appear, but the files aren't available until later. 您可以实现一个文件系统过滤器驱动程序,该驱动程序报告设备上没有文件,直到扫描完成,但这会严重混淆看到设备出现的软件,但文件在以后才可用。 And C# is not useful for implementing Windows drivers. 而C#对于实现Windows驱动程序没有用。 Not even with a "library of C to be used in C#" (I assume you mean p/invoke). 甚至没有“在C#中使用的C库”(我假设你的意思是p / invoke)。 C# code just isn't compatible with the environment inside the kernel. C#代码与内核中的环境不兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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