C#相当于VB DLL函数声明 [英] C# equivalent of VB DLL function declaration

查看:92
本文介绍了C#相当于VB DLL函数声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个VB代码的C#等价物是什么?



What is the C# equivalent of this VB code?

Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal msg As Integer, _
ByVal wParam As Integer, ByVal lParam As Integer) As Integer





以下代码转换为C#





Below code is converted to C#

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);





但我的开关盒仍然显示如下错误:



错误1:开关表达式或案例标签必须是bool,char,string,integral,enum或相应的可空类型



这是我检测插入/移除USB的完整代码..





but my switch case still show error like this :

Error 1 : A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type"

this is my full code to detect inserted/removed USB..

//Used to detected if any of the messages are any of these constants values.

        private const int WM_DEVICECHANGE = 0x219;

        private const int DBT_DEVICEARRIVAL = 0x8000;

        private const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
        //
        private const int DBT_DEVTYP_VOLUME = 0x2;
        //
        //Get the information about the detected volume.
        private struct DEV_BROADCAST_VOLUME
        {


            int Dbcv_Size;

            int Dbcv_Devicetype;

            int Dbcv_Reserved;

            int Dbcv_Unitmask;

            short Dbcv_Flags;
        }


        [DllImport("user32.dll")]
        static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); 

        protected override void WndProc(ref System.Windows.Forms.Message M)
        {
            //
            //These are the required subclassing codes for detecting device based removal and arrival.
            //

            if (M.Msg == WM_DEVICECHANGE)
            {
                switch (M.WParam) //  <---------------------------############## ERROR in HERE
                {
                    //
                    //Check if a device was added.
                    case DBT_DEVICEARRIVAL:

                        int DevType = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4);


                        if (DevType == DBT_DEVTYP_VOLUME)
                        {
                            DEV_BROADCAST_VOLUME Vol = new DEV_BROADCAST_VOLUME();

                            Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, typeof(DEV_BROADCAST_VOLUME));


                            if (Vol.Dbcv_Flags == 0)
                            {

                                for (int i = 0; i <= 20; i++)
                                {
                                    if (Math.Pow(2, i) == Vol.Dbcv_Unitmask)
                                    {
                                        string Usb = Chr(65 + i) + ":\\";
                                        // ini untuk mendeteksi flashdisk saat pertama kali dimasukan kedalam komputer dan membunuh autorun virus
                                        if (My.Computer.FileSystem.FileExists(Usb.ToString + "Autorun.inf"))
                                            Kill(Usb.ToString + "Autorun.inf");
                                        MsgBox("USB device was plugged in!" + vbNewLine + vbNewLine + "The drive letter is: " + Usb.ToString + vbNewLine + "If Autorun Virus Exist, it will Automatic Kill Autorun!", MsgBoxStyle.Information);
                                        //untuk menampilkan alamat flashdisk yang masuk kekomputer
                                        TextBox1.Text = Usb.ToString;
                                        break; // TODO: might not be correct. Was : Exit For
                                    }
                                }

                            }

                        }
                        break;
                    //
                    //Check if the message was for the removal of a device.
                    case DBT_DEVICEREMOVECOMPLETE:

                        int DevType = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4);


                        if (DevType == DBT_DEVTYP_VOLUME)
                        {
                            DEV_BROADCAST_VOLUME Vol = new DEV_BROADCAST_VOLUME();

                            Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, typeof(DEV_BROADCAST_VOLUME));


                            if (Vol.Dbcv_Flags == 0)
                            {

                                for (int i = 0; i <= 20; i++)
                                {

                                    if (Math.Pow(2, i) == Vol.Dbcv_Unitmask)
                                    {
                                        string Usb = Chr(65 + i) + ":\\";

                                        MsgBox("Looks like a volume device was removed!" + vbNewLine + vbNewLine + "The drive letter is: " + Usb.ToString, MsgBoxStyle.Information);

                                        break; // TODO: might not be correct. Was : Exit For

                                    }

                                }

                            }

                        }

                        break;
                }

            }

            base.WndProc(ref M);

        }









some one可以帮我修复它吗?





some one can help me to fix it?

推荐答案

M.WParam [ ^ ]是一个IntPtr。尝试将其转换为int。
M.WParam[^] is an IntPtr. Try casting it to int.


发生错误是因为 WParam 类型是 IntPtr (参见 MSDN [ ^ ])。

由于您在 case 语句中使用 int ,因此您可以安全地编写以这种方式切换

The error occurs because WParam type is IntPtr (see MSDN[^]).
Since you are using int in your case statements then you may safely write your switch this way:
switch( M.WParam.ToInt32())





[修正了一个错字]



[fixed a typo]


这篇关于C#相当于VB DLL函数声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆