Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
What is the C# equivalent of this VB code?

VB
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


Below code is converted to C#

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


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..

C#
//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 can help me to fix it?
Posted
Updated 23-May-13 23:24pm
v2
Comments
Mahesh Bailwal 24-May-13 5:39am    
Can you assign M.WParam into int variable and then use it in switch and try. Like below

int tempWparam = (int)M.WParam;
switch(tempWparam)
{
..
}
walterhevedeich 24-May-13 5:41am    
Beat me to it. You should have posted this as a solution. :)
Mahesh Bailwal 24-May-13 5:52am    
I was not sure :)
Gun Gun Febrianza 24-May-13 6:24am    
found 25 Error........

Error 3 'Anti_Copy_Media_Version_2._0.Form1.DEV_BROADCAST_VOLUME.Dbcv_Flags' is inaccessible due to its protection level C:\Users\Kaizer\Desktop\Anti Copy Media Version 2.0\Anti Copy Media Version 2.0\Form1.cs 288 37 Anti Copy Media Version 2.0
walterhevedeich 24-May-13 6:53am    
I think you have to declare your DEV_BROADCAST_VOLUME struct and its variables public if you want to access them.

M.WParam[^] is an IntPtr. Try casting it to int.
 
Share this answer
 
Comments
Gun Gun Febrianza 24-May-13 6:30am    
iam tried use this code to casting it into integer but still not working ..
below is my code .. hmm, any suggest sir?
switch((int)m.WParam))
walterhevedeich 24-May-13 6:34am    
What's the error this time?
Gun Gun Febrianza 24-May-13 6:38am    
boss. below code is the error :

Error 6 'Anti_Copy_Media_Version_2._0.Form1.DEV_BROADCAST_VOLUME.Dbcv_Flags' is inaccessible due to its protection level C:\Users\Kaizer\Desktop\Anti Copy Media Version 2.0\Anti Copy Media Version 2.0\Form1.cs 287 37 Anti Copy Media Version 2.0

god i am stuck :(
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:
C#
switch( M.WParam.ToInt32())


[fixed a typo]
 
Share this answer
 
v3
Comments
Gun Gun Febrianza 24-May-13 6:30am    
i am tried but still found error :)
Gun Gun Febrianza 24-May-13 6:32am    
Error 1 'System.IntPtr' does not contain a definition for 'toInt32' and no extension method 'toInt32' accepting a first argument of type 'System.IntPtr' could be found (are you missing a using directive or an assembly reference?) C:\Users\Kaizer\Desktop\Anti Copy Media Version 2.0\Anti Copy Media Version 2.0\Form1.cs 271 34 Anti Copy Media Version 2.0
CPallini 24-May-13 6:59am    
It is ToInt32() (there was a typo in my answer)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900