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
Below code is converted to 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..
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;
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)
{
if (M.Msg == WM_DEVICECHANGE)
{
switch (M.WParam)
{
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) + ":\\";
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);
TextBox1.Text = Usb.ToString;
break;
}
}
}
}
break;
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;
}
}
}
}
break;
}
}
base.WndProc(ref M);
}
some one can help me to fix it?