EDN Admin
Well-known member
Im trying to monitor a removal or arrival of a USB virtual COM port. I asked a question about how to do this asynchronously a couple of months ago, but this is as close as I could get.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; protected <span style="color:Blue; override <span style="color:Blue; void WndProc(<span style="color:Blue; ref System.Windows.Forms.Message m)
{
<span style="color:Blue; base.WndProc(<span style="color:Blue; ref m);
<span style="color:Blue; if (m.Msg == 0X219)
{
<span style="color:Blue; int WParam = m.WParam.ToInt32();
<span style="color:Blue; int device_type = 0;
<span style="color:Green; //int param1 = WParam & 0x8000;
<span style="color:Green; //if ((WParam & 0X8000) == 0X8007)
<span style="color:Blue; if (WParam == 0X0007)
{
<span style="color:Green; //int Len = Marshal.ReadInt32(m.LParam);
<span style="color:Blue; int Len = m.LParam.ToInt32();
<span style="color:Green; //if (Marshal.ReadInt32(m.LParam, 4) == 0)
<span style="color:Green; //if (Marshal.ReadInt32(m.LParam, 4) == 3)
device_type = Marshal.ReadInt32(m.LParam, 4);
<span style="color:Blue; if (device_type == 0)
{
StringBuilder SB = <span style="color:Blue; new StringBuilder();
<span style="color:Blue; for (<span style="color:Blue; int I = 12; I < Len; I += 2)
{
SB.Append((<span style="color:Blue; char)(Marshal.ReadByte(m.LParam, I)));
}
<span style="color:Blue; if ((SB.ToString()).Substring(0, 4) == portComboBox.Text)
{
<span style="color:Blue; int ArriveDepart = (WParam & 0XFFF);
<span style="color:Blue; if (ArriveDepart == 0)
{
com.OpenPort(); <span style="color:Green; //this is the portion that must be changed
portStatusLabel.Text = <span style="color:#A31515; "The port is alive";
}
<span style="color:Blue; else <span style="color:Blue; if (ArriveDepart == 4)
{
com.ClosePort(); <span style="color:Green; //this is the portion that must be changed
portStatusLabel.Text = <span style="color:#A31515; "The port is dead";
}
}
}
}
}
}
[/code]
<br/>
I had to comment out the int Len = Marshal.ReadInt32(m.LParam); line because it caused an error related to accessing write protected memory.
So right now I am continuously running into an error on the line device_type = Marshal.ReadInt32(m.LParam, 4);. How can I read this protected memory? Im not interested in running the program as unsafe code, too.
Any help would be greatly appreciated.
View the full article
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; protected <span style="color:Blue; override <span style="color:Blue; void WndProc(<span style="color:Blue; ref System.Windows.Forms.Message m)
{
<span style="color:Blue; base.WndProc(<span style="color:Blue; ref m);
<span style="color:Blue; if (m.Msg == 0X219)
{
<span style="color:Blue; int WParam = m.WParam.ToInt32();
<span style="color:Blue; int device_type = 0;
<span style="color:Green; //int param1 = WParam & 0x8000;
<span style="color:Green; //if ((WParam & 0X8000) == 0X8007)
<span style="color:Blue; if (WParam == 0X0007)
{
<span style="color:Green; //int Len = Marshal.ReadInt32(m.LParam);
<span style="color:Blue; int Len = m.LParam.ToInt32();
<span style="color:Green; //if (Marshal.ReadInt32(m.LParam, 4) == 0)
<span style="color:Green; //if (Marshal.ReadInt32(m.LParam, 4) == 3)
device_type = Marshal.ReadInt32(m.LParam, 4);
<span style="color:Blue; if (device_type == 0)
{
StringBuilder SB = <span style="color:Blue; new StringBuilder();
<span style="color:Blue; for (<span style="color:Blue; int I = 12; I < Len; I += 2)
{
SB.Append((<span style="color:Blue; char)(Marshal.ReadByte(m.LParam, I)));
}
<span style="color:Blue; if ((SB.ToString()).Substring(0, 4) == portComboBox.Text)
{
<span style="color:Blue; int ArriveDepart = (WParam & 0XFFF);
<span style="color:Blue; if (ArriveDepart == 0)
{
com.OpenPort(); <span style="color:Green; //this is the portion that must be changed
portStatusLabel.Text = <span style="color:#A31515; "The port is alive";
}
<span style="color:Blue; else <span style="color:Blue; if (ArriveDepart == 4)
{
com.ClosePort(); <span style="color:Green; //this is the portion that must be changed
portStatusLabel.Text = <span style="color:#A31515; "The port is dead";
}
}
}
}
}
}
[/code]
<br/>
I had to comment out the int Len = Marshal.ReadInt32(m.LParam); line because it caused an error related to accessing write protected memory.
So right now I am continuously running into an error on the line device_type = Marshal.ReadInt32(m.LParam, 4);. How can I read this protected memory? Im not interested in running the program as unsafe code, too.
Any help would be greatly appreciated.
View the full article