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 becauseWParam
type isIntPtr
(see MSDN[^]).
Since you are usingint
in yourcase
statements then you may safely write yourswitch
this way:
switch( M.WParam.ToInt32())
[修正了一个错字]
[fixed a typo]