RasConnectionNotification Function

  • Thread starter Thread starter Merlynx
  • Start date Start date
M

Merlynx

I am trying to use that function to monitor online connection. Using the
createEvent function and WaitForMultipleObjects,i managed to get it to work
in VB.
But i am having trouble to get it to work in C#. I am doing a direct
translation of the code from VB to C#. i am sure i am doing the wrong thing
as there is a WaitAll method that act like WaitForMultipleObjects which i
don't know how to use and there is the AutoResetEvent class too.
It would be nice if someone show me how to get that function to work :)
TIA
 
It would be nice if someone show me how to get that function to work :)

Please post your code.



Mattias
 
public class MonitorRAS
{
const UInt32 RASCN_Connection = 1;
const UInt32 RASCN_Disconnection = 2;
const UInt32 WAIT_OBJECT_0 = 0;
const UInt32 WAIT_FAILED = 4294967295;
const UInt32 WAIT_TIMEOUT = 258;

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;
}
}

[DllImport("rasapi32.dll")]
public static extern UInt32 RasConnectionNotification
(long hRasConn,IntPtr hEvents,UInt32 dwFlags);
[DllImport("kernel32")]
public static extern IntPtr CreateEvent
(ref SECURITY_ATTRIBUTES lpEventAttributes,bool bManualReset,bool
bInitialState,string lpName);

public void RasMonitor(){
IntPtr[] hEvents = new IntPtr[2];
long RasNotif,hRasConn = 0;
long WaitRet = 0;

SECURITY_ATTRIBUTES Sd = new SECURITY_ATTRIBUTES();
Sd.nLength = Marshal.SizeOf(Sd);
Sd.bInheritHandle = 0;
Sd.IpSecurityDescriptor = 0;

hEvents[0] = CreateEvent(ref Sd, true, false,
"RASStatusNotificationObject1");
hEvents[1] = CreateEvent(ref Sd, true, false,
"RASStatusNotificationObject2");
RasNotif =
RasConnectionNotification(hRasConn,hEvents[0],RASCN_Connection);
RasNotif =
RasConnectionNotification(hRasConn,hEvents[1],RASCN_Disconnection);
while(Global.Terminate != true){
WaitRet = RasNotif;
switch(WaitRet){
case
WAIT_OBJECT_0:MessageBox.Show("Connection");break;
case WAIT_OBJECT_0 +
1:MessageBox.Show("Disconnected");break;
case
WAIT_TIMEOUT:Application.DoEvents();break;
case
WAIT_FAILED:MessageBox.Show("Failed");break;
}
}


Previously i was using the win32 WaitForMultipleObjects but it did'nt do any
good :(
 
public struct SECURITY_ATTRIBUTES{
public long nLength;
public long IpSecurityDescriptor;
public long bInheritHandle;

Should be

public struct SECURITY_ATTRIBUTES {
public uint nLength;
public IntPtr IpSecurityDescriptor;
public bool bInheritHandle;

[DllImport("rasapi32.dll")]
public static extern UInt32 RasConnectionNotification
(long hRasConn,IntPtr hEvents,UInt32 dwFlags);

The hRasConn parameter should be an IntPtr



Mattias
 
Back
Top