It seems that my code doest catch any event. I dont know where the problem is so if someone can help it would nice.TIA
public class MonitorRasConn
{
public struct SECURITY_ATTRIBUTES{
public long nLength;
public long IpSecurityDescriptor;
public long bInheritHandle;
public SECURITY_ATTRIBUTES(long nLength,long IpSecurityDescriptor,long bInheritHandle){
this.nLength = nLength;
this.bInheritHandle = bInheritHandle;
this.IpSecurityDescriptor = IpSecurityDescriptor;
}
}
const UInt32 RASCN_Connection = 1;
const UInt32 RASCN_Disconnection = 2;
const UInt32 WAIT_OBJECT_0 = 0;
const UInt32 WAIT_FAILED = 0xFFFFFFFF;//4294967295;
const UInt32 WAIT_TIMEOUT = 0x102;//258;
[DllImport("kernel32.dll", EntryPoint="CreateEvent")]
static extern IntPtr CreateEvent(ref SECURITY_ATTRIBUTES eventAttributes,
bool manualReset, bool initialState, String name);
[DllImport("rasapi32.dll", EntryPoint="RasConnectionNotification")]
static extern UInt32 RasConnectionNotification
(long hRasConn,IntPtr hEvents,UInt32 dwFlags);
[DllImport("kernel32.dll", EntryPoint="WaitForMultipleObjects")]
static extern UInt32 WaitForMultipleObjects
(UInt32 nCount,ref IntPtr Handles,bool WaitAll,UInt32 time);
public void MonitorConn(){
long RasNotif,hRasConn = 0;long WaitRet;
SECURITY_ATTRIBUTES Sd = new SECURITY_ATTRIBUTES();
Sd.nLength = Marshal.SizeOf(Sd);
Sd.bInheritHandle = 0;
Sd.IpSecurityDescriptor = 0;
IntPtr[] hEvents = new IntPtr[2];
hEvents[0] = CreateEvent(ref Sd, false, false, null);
hEvents[1] = CreateEvent(ref Sd, false, false, null);
RasNotif = RasConnectionNotification(hRasConn,hEvents[0],RASCN_Connection);
if(RasNotif != 0){MessageBox.Show("Function failure");}
RasNotif = RasConnectionNotification(hRasConn,hEvents[1],RASCN_Disconnection);
if(RasNotif != 0){MessageBox.Show("Function failure");}
WaitRet = WaitForMultipleObjects(2,ref hEvents[0],false,20);
switch(WaitRet){
case WAIT_OBJECT_0:MessageBox.Show("Connected");break;
case WAIT_OBJECT_0 + 1:MessageBox.Show("Disconnected");break;
case WAIT_TIMEOUT:break;
}
}
}
public class MonitorRasConn
{
public struct SECURITY_ATTRIBUTES{
public long nLength;
public long IpSecurityDescriptor;
public long bInheritHandle;
public SECURITY_ATTRIBUTES(long nLength,long IpSecurityDescriptor,long bInheritHandle){
this.nLength = nLength;
this.bInheritHandle = bInheritHandle;
this.IpSecurityDescriptor = IpSecurityDescriptor;
}
}
const UInt32 RASCN_Connection = 1;
const UInt32 RASCN_Disconnection = 2;
const UInt32 WAIT_OBJECT_0 = 0;
const UInt32 WAIT_FAILED = 0xFFFFFFFF;//4294967295;
const UInt32 WAIT_TIMEOUT = 0x102;//258;
[DllImport("kernel32.dll", EntryPoint="CreateEvent")]
static extern IntPtr CreateEvent(ref SECURITY_ATTRIBUTES eventAttributes,
bool manualReset, bool initialState, String name);
[DllImport("rasapi32.dll", EntryPoint="RasConnectionNotification")]
static extern UInt32 RasConnectionNotification
(long hRasConn,IntPtr hEvents,UInt32 dwFlags);
[DllImport("kernel32.dll", EntryPoint="WaitForMultipleObjects")]
static extern UInt32 WaitForMultipleObjects
(UInt32 nCount,ref IntPtr Handles,bool WaitAll,UInt32 time);
public void MonitorConn(){
long RasNotif,hRasConn = 0;long WaitRet;
SECURITY_ATTRIBUTES Sd = new SECURITY_ATTRIBUTES();
Sd.nLength = Marshal.SizeOf(Sd);
Sd.bInheritHandle = 0;
Sd.IpSecurityDescriptor = 0;
IntPtr[] hEvents = new IntPtr[2];
hEvents[0] = CreateEvent(ref Sd, false, false, null);
hEvents[1] = CreateEvent(ref Sd, false, false, null);
RasNotif = RasConnectionNotification(hRasConn,hEvents[0],RASCN_Connection);
if(RasNotif != 0){MessageBox.Show("Function failure");}
RasNotif = RasConnectionNotification(hRasConn,hEvents[1],RASCN_Disconnection);
if(RasNotif != 0){MessageBox.Show("Function failure");}
WaitRet = WaitForMultipleObjects(2,ref hEvents[0],false,20);
switch(WaitRet){
case WAIT_OBJECT_0:MessageBox.Show("Connected");break;
case WAIT_OBJECT_0 + 1:MessageBox.Show("Disconnected");break;
case WAIT_TIMEOUT:break;
}
}
}